| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "rlz/lib/recursive_cross_process_lock_posix.h" | 5 #include "rlz/lib/recursive_cross_process_lock_posix.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/file.h> | 9 #include <sys/file.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 while ((flock_result = | 52 while ((flock_result = |
| 53 HANDLE_EINTR(flock(file_lock_, LOCK_EX | LOCK_NB))) == -1 && | 53 HANDLE_EINTR(flock(file_lock_, LOCK_EX | LOCK_NB))) == -1 && |
| 54 errno == EWOULDBLOCK && | 54 errno == EWOULDBLOCK && |
| 55 elapsed_ms < kMaxTimeoutMS) { | 55 elapsed_ms < kMaxTimeoutMS) { |
| 56 usleep(kSleepPerTryMS * 1000); | 56 usleep(kSleepPerTryMS * 1000); |
| 57 elapsed_ms += kSleepPerTryMS; | 57 elapsed_ms += kSleepPerTryMS; |
| 58 } | 58 } |
| 59 | 59 |
| 60 if (flock_result == -1) { | 60 if (flock_result == -1) { |
| 61 perror("flock"); | 61 perror("flock"); |
| 62 ignore_result(HANDLE_EINTR(close(file_lock_))); | 62 close(file_lock_); |
| 63 file_lock_ = -1; | 63 file_lock_ = -1; |
| 64 return false; | 64 return false; |
| 65 } | 65 } |
| 66 return true; | 66 return true; |
| 67 } else { | 67 } else { |
| 68 return file_lock_ != -1; | 68 return file_lock_ != -1; |
| 69 } | 69 } |
| 70 } | 70 } |
| 71 | 71 |
| 72 void RecursiveCrossProcessLock::ReleaseLock() { | 72 void RecursiveCrossProcessLock::ReleaseLock() { |
| 73 if (file_lock_ != -1) { | 73 if (file_lock_ != -1) { |
| 74 ignore_result(HANDLE_EINTR(flock(file_lock_, LOCK_UN))); | 74 ignore_result(HANDLE_EINTR(flock(file_lock_, LOCK_UN))); |
| 75 ignore_result(HANDLE_EINTR(close(file_lock_))); | 75 close(file_lock_); |
| 76 file_lock_ = -1; | 76 file_lock_ = -1; |
| 77 } | 77 } |
| 78 | 78 |
| 79 locking_thread_ = 0; | 79 locking_thread_ = 0; |
| 80 pthread_mutex_unlock(&recursive_lock_); | 80 pthread_mutex_unlock(&recursive_lock_); |
| 81 } | 81 } |
| 82 | 82 |
| 83 } // namespace rlz_lib | 83 } // namespace rlz_lib |
| OLD | NEW |