Index: rlz/mac/lib/rlz_value_store_mac.mm |
diff --git a/rlz/mac/lib/rlz_value_store_mac.mm b/rlz/mac/lib/rlz_value_store_mac.mm |
index 5313a3013a9b80a016434799a01b475891bb8877..6ac38923e173ac6dbac81826f13bfa291b6fd1c2 100644 |
--- a/rlz/mac/lib/rlz_value_store_mac.mm |
+++ b/rlz/mac/lib/rlz_value_store_mac.mm |
@@ -235,7 +235,7 @@ struct RecursiveCrossProcessLock { |
pthread_mutex_t recursive_lock_; |
pthread_t locking_thread_; |
- NSDistributedLock* file_lock_; |
+ int file_lock_; |
Scott Hess - ex-Googler
2012/08/15 00:44:38
ScopedFD?
Nico
2012/08/15 03:24:12
It needs to be closed while the mutex is still hel
Scott Hess - ex-Googler
2012/08/15 19:55:04
That's what reset() is for.
|
} g_recursive_lock = { |
// PTHREAD_RECURSIVE_MUTEX_INITIALIZER doesn't exist before 10.7 and is buggy |
// on 10.7 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51906#c34), so emulate |
@@ -267,32 +267,42 @@ bool RecursiveCrossProcessLock::TryGetCrossProcessLock( |
const int kSleepPerTryMS = 200; |
CHECK(!file_lock_); |
- file_lock_ = [[NSDistributedLock alloc] initWithPath:lock_filename]; |
+ file_lock_ = open([lock_filename fileSystemRepresentation], O_CREAT); |
+ if (file_lock_ == -1) { |
+ file_lock_ = 0; |
Scott Hess - ex-Googler
2012/08/15 00:44:38
0 is a valid fd. Suggest using -1 for invalid, ju
Mark Mentovai
2012/08/15 00:50:54
But 0 is a valid FD. You should rewrite things to
Nico
2012/08/15 03:24:12
Done. (But in practice 0 is stdin, so in practice
Mark Mentovai
2012/08/15 13:36:28
Nico wrote:
|
+ return false; |
+ } |
- BOOL got_file_lock = NO; |
+ int flock_result = -1; |
int elapsedMS = 0; |
Mark Mentovai
2012/08/15 00:50:54
elapsed_ms
Nico
2012/08/15 03:24:12
Done.
|
- while (!(got_file_lock = [file_lock_ tryLock]) && |
+ while ((flock_result = flock(file_lock_, LOCK_EX | LOCK_NB)) == -1 && |
Scott Hess - ex-Googler
2012/08/15 00:44:38
errno == EWOULDBLOCK &&
Then you can lift the if(
Mark Mentovai
2012/08/15 00:50:54
HANDLE_EINTR
Nico
2012/08/15 03:24:12
Done.
|
elapsedMS < kMaxTimeoutMS) { |
- usleep(kSleepPerTryMS * 1000); |
- elapsedMS += kSleepPerTryMS; |
+ if (errno == EWOULDBLOCK) { |
+ usleep(kSleepPerTryMS * 1000); |
+ elapsedMS += kSleepPerTryMS; |
Scott Hess - ex-Googler
2012/08/15 00:44:38
usleep() could be interrupted by a signal, in whic
Nico
2012/08/15 03:24:12
Added HANDLE_EINTR here too.
Mark Mentovai
2012/08/15 13:36:28
HANDLE_EINTR doesn’t really help here because the
Nico
2012/08/15 14:52:50
Good (pathological) point! Removed it again.
Scott Hess - ex-Googler
2012/08/15 19:55:04
I think the POSIXy way is something like:
int e
Mark Mentovai
2012/08/15 20:18:39
shess wrote:
|
+ } else { |
+ close(file_lock_); |
+ file_lock_ = 0; |
+ return false; |
+ } |
} |
- if (!got_file_lock) { |
- [file_lock_ release]; |
- file_lock_ = nil; |
+ if (flock_result == -1) { |
+ close(file_lock_); |
Mark Mentovai
2012/08/15 00:50:54
This duplicates what’s in the loop. I think you on
Nico
2012/08/15 03:24:12
Done, together with shess's suggestion above.
|
+ file_lock_ = 0; |
return false; |
} |
return true; |
} else { |
- return file_lock_ != nil; |
+ return file_lock_ != 0; |
} |
} |
void RecursiveCrossProcessLock::ReleaseLock() { |
if (file_lock_) { |
- [file_lock_ unlock]; |
- [file_lock_ release]; |
- file_lock_ = nil; |
+ flock(file_lock_, LOCK_UN); |
+ close(file_lock_); |
+ file_lock_ = 0; |
} |
locking_thread_ = 0; |
@@ -347,7 +357,7 @@ NSString* RlzPlistFilename() { |
// Returns the path of the rlz lock file, also creates the parent directory |
// path if it doesn't exist. |
NSString* RlzLockFilename() { |
- NSString* const kRlzFile = @"lockfile"; |
+ NSString* const kRlzFile = @"flockfile"; |
return [CreateRlzDirectory() stringByAppendingPathComponent:kRlzFile]; |
} |