LEFT | RIGHT |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // Use of this source code is governed by an Apache-style license that can be | 2 // Use of this source code is governed by an Apache-style license that can be |
3 // found in the COPYING file. | 3 // found in the COPYING file. |
4 | 4 |
5 #include "rlz/mac/lib/rlz_value_store_mac.h" | 5 #include "rlz/mac/lib/rlz_value_store_mac.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 } | 214 } |
215 | 215 |
216 | 216 |
217 namespace { | 217 namespace { |
218 | 218 |
219 // Creating a recursive cross-process mutex on windows is one line. On mac, | 219 // Creating a recursive cross-process mutex on windows is one line. On mac, |
220 // there's no primitve for that, so this lock is emulated by an in-process | 220 // there's no primitve for that, so this lock is emulated by an in-process |
221 // mutex to get the recursive part, followed by a cross-process lock for the | 221 // mutex to get the recursive part, followed by a cross-process lock for the |
222 // cross-process part. | 222 // cross-process part. |
223 | 223 |
224 // PTHREAD_RECURSIVE_MUTEX_INITIALIZER doesn't exist before 10.7 and is buggy | 224 // This is a struct so that it doesn't need a static initializer. |
225 // on 10.7 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51906#c34), so emulate | 225 struct RecursiveCrossProcessLock { |
226 // recursive locking with a normal non-recursive mutex. | 226 // Tries to acquire a recursive cross-process lock. Note that this _always_ |
227 pthread_mutex_t g_recursive_lock = PTHREAD_MUTEX_INITIALIZER; | 227 // acquires the in-process lock (if it wasn't already acquired). The parent |
228 pthread_t g_locking_thread; | 228 // directory of |lock_file| must exist. |
229 | 229 bool TryGetCrossProcessLock(NSString* lock_filename); |
230 NSDistributedLock* g_file_lock; | 230 |
| 231 // Releases the lock. Should always be called, even if |
| 232 // TryGetCrossProcessLock() returns false. |
| 233 void ReleaseLock(); |
| 234 |
| 235 pthread_mutex_t recursive_lock_; |
| 236 pthread_t locking_thread_; |
| 237 |
| 238 NSDistributedLock* file_lock_; |
| 239 } g_recursive_lock = { |
| 240 // PTHREAD_RECURSIVE_MUTEX_INITIALIZER doesn't exist before 10.7 and is buggy |
| 241 // on 10.7 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51906#c34), so emulate |
| 242 // recursive locking with a normal non-recursive mutex. |
| 243 PTHREAD_MUTEX_INITIALIZER |
| 244 }; |
| 245 |
| 246 bool RecursiveCrossProcessLock::TryGetCrossProcessLock( |
| 247 NSString* lock_filename) { |
| 248 bool just_got_lock = false; |
| 249 |
| 250 // Emulate a recursive mutex with a non-recursive one. |
| 251 if (pthread_mutex_trylock(&recursive_lock_) == EBUSY) { |
| 252 if (pthread_equal(pthread_self(), locking_thread_) == 0) { |
| 253 // Some other thread has the lock, wait for it. |
| 254 pthread_mutex_lock(&recursive_lock_); |
| 255 CHECK(locking_thread_ == 0); |
| 256 just_got_lock = true; |
| 257 } |
| 258 } else { |
| 259 just_got_lock = true; |
| 260 } |
| 261 |
| 262 locking_thread_ = pthread_self(); |
| 263 |
| 264 // Try to acquire file lock. |
| 265 if (just_got_lock) { |
| 266 const int kMaxTimeoutMS = 5000; // Matches windows. |
| 267 const int kSleepPerTryMS = 200; |
| 268 |
| 269 CHECK(!file_lock_); |
| 270 file_lock_ = [[NSDistributedLock alloc] initWithPath:lock_filename]; |
| 271 |
| 272 BOOL got_file_lock = NO; |
| 273 int elapsedMS = 0; |
| 274 while (!(got_file_lock = [file_lock_ tryLock]) && |
| 275 elapsedMS < kMaxTimeoutMS) { |
| 276 usleep(kSleepPerTryMS * 1000); |
| 277 elapsedMS += kSleepPerTryMS; |
| 278 } |
| 279 |
| 280 if (!got_file_lock) { |
| 281 [file_lock_ release]; |
| 282 file_lock_ = nil; |
| 283 return false; |
| 284 } |
| 285 return true; |
| 286 } else { |
| 287 return file_lock_ != nil; |
| 288 } |
| 289 } |
| 290 |
| 291 void RecursiveCrossProcessLock::ReleaseLock() { |
| 292 if (file_lock_) { |
| 293 [file_lock_ unlock]; |
| 294 [file_lock_ release]; |
| 295 file_lock_ = nil; |
| 296 } |
| 297 |
| 298 locking_thread_ = 0; |
| 299 pthread_mutex_unlock(&recursive_lock_); |
| 300 } |
| 301 |
231 | 302 |
232 // This is set during test execution, to write RLZ files into a temporary | 303 // This is set during test execution, to write RLZ files into a temporary |
233 // directory instead of the user's Application Support folder. | 304 // directory instead of the user's Application Support folder. |
234 NSString* g_test_folder; | 305 NSString* g_test_folder; |
235 | 306 |
236 // RlzValueStoreMac keeps its data in memory and only writes it to disk when | 307 // RlzValueStoreMac keeps its data in memory and only writes it to disk when |
237 // ScopedRlzValueStoreLock goes out of scope. Hence, if several | 308 // ScopedRlzValueStoreLock goes out of scope. Hence, if several |
238 // ScopedRlzValueStoreLocks are nested, they all need to use the same store | 309 // ScopedRlzValueStoreLocks are nested, they all need to use the same store |
239 // object. | 310 // object. |
240 | 311 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 // Returns the path of the rlz lock file, also creates the parent directory | 347 // Returns the path of the rlz lock file, also creates the parent directory |
277 // path if it doesn't exist. | 348 // path if it doesn't exist. |
278 NSString* RlzLockFilename() { | 349 NSString* RlzLockFilename() { |
279 NSString* const kRlzFile = @"lockfile"; | 350 NSString* const kRlzFile = @"lockfile"; |
280 return [CreateRlzDirectory() stringByAppendingPathComponent:kRlzFile]; | 351 return [CreateRlzDirectory() stringByAppendingPathComponent:kRlzFile]; |
281 } | 352 } |
282 | 353 |
283 } // namespace | 354 } // namespace |
284 | 355 |
285 ScopedRlzValueStoreLock::ScopedRlzValueStoreLock() { | 356 ScopedRlzValueStoreLock::ScopedRlzValueStoreLock() { |
286 // TODO(thakis): Try to take a recursive cross-process lock for 5 minutes, | 357 bool got_distributed_lock = |
287 // set store_ to NULL if that fails and return. | 358 g_recursive_lock.TryGetCrossProcessLock(RlzLockFilename()); |
288 | 359 // At this point, we hold the in-process lock, no matter the value of |
289 // NSRecursiveLock (or posix: | 360 // |got_distributed_lock|. |
290 // static pthread_mutex_t mtx = PTHREAD_RECURSIVE_MUTEX_INITIALIZER | |
291 //pthread_mutex_lock(&mtx) | |
292 //pthread_mutex_timedlock_np(&g_recursive_lock); | |
293 // pthread_mutex_unlock(&mtx) | |
294 // ) | |
295 | |
296 bool just_got_lock = false; | |
297 if (pthread_mutex_trylock(&g_recursive_lock) == EBUSY) { | |
298 if (pthread_equal(pthread_self(), g_locking_thread) == 0) { | |
299 // Some other thread has the lock, wait for it. | |
300 pthread_mutex_lock(&g_recursive_lock); | |
301 CHECK(g_locking_thread == 0); | |
302 just_got_lock = true; | |
303 } | |
304 } else { | |
305 just_got_lock = true; | |
306 } | |
307 | |
308 g_locking_thread = pthread_self(); | |
309 | |
310 // NSDistributedLock* for the file lock. | |
311 | |
312 // Timeout after waiting 5 min (XXX on which lock?) | |
313 | |
314 // Think about failing to take the lock and then a nested lock. | |
315 // Should never happen in practice as everything errors out early if | |
316 // failing to take a lock, so just CHECK() I guess. | |
317 | |
318 | 361 |
319 ++g_lock_depth; | 362 ++g_lock_depth; |
320 if (g_lock_depth > 1) { | 363 if (g_lock_depth > 1) { |
321 // Reuse the already existing store object. | 364 // Reuse the already existing store object. |
322 // All user code early-exits when a lock fails, so a recursive lock will | 365 // All user code early-exits when a lock fails, so a recursive lock will |
323 // never end up with |g_store_object| that is NULL. | 366 // never end up with |g_store_object| that is NULL. |
324 CHECK(!just_got_lock); | 367 CHECK(got_distributed_lock); |
325 CHECK(g_store_object); | 368 CHECK(g_store_object); |
326 store_.reset(g_store_object); | 369 store_.reset(g_store_object); |
327 return; | 370 return; |
328 } | 371 } |
329 | 372 |
330 if (just_got_lock) { | 373 if (!got_distributed_lock) { |
331 const int kMaxTimeoutMS = 5000; // Matches windows. | 374 // Give up. |store_| isn't set, which signals to callers that acquiring |
332 const int kSleepPerTryMS = 200; | 375 // the lock failed. |g_recursive_lock| will be released by the |
333 | 376 // destructor. |
334 CHECK(!g_file_lock); | 377 return; |
335 g_file_lock = [[NSDistributedLock alloc] initWithPath:RlzLockFilename()]; | |
336 | |
337 BOOL got_file_lock = NO; | |
338 int elapsedMS = 0; | |
339 while (!(got_file_lock = [g_file_lock tryLock]) && | |
340 elapsedMS < kMaxTimeoutMS) { | |
341 usleep(kSleepPerTryMS * 1000); | |
342 elapsedMS += kSleepPerTryMS; | |
343 } | |
344 | |
345 if (!got_file_lock) { | |
346 [g_file_lock release]; | |
347 g_file_lock = nil; | |
348 // Give up. |store_| isn't set, which signals to callers that acquiring | |
349 // the lock failed. |g_recursive_lock| will be released by the | |
350 // destructor. | |
351 return; | |
352 } | |
353 } | 378 } |
354 | 379 |
355 CHECK(!g_store_object); | 380 CHECK(!g_store_object); |
356 | 381 |
357 NSString* plist = RlzPlistFilename(); | 382 NSString* plist = RlzPlistFilename(); |
358 | 383 |
359 // Create an empty file if none exists yet. | 384 // Create an empty file if none exists yet. |
360 NSFileManager* manager = [NSFileManager defaultManager]; | 385 NSFileManager* manager = [NSFileManager defaultManager]; |
361 if (![manager fileExistsAtPath:plist isDirectory:NULL]) | 386 if (![manager fileExistsAtPath:plist isDirectory:NULL]) |
362 [[NSDictionary dictionary] writeToFile:plist atomically:YES]; | 387 [[NSDictionary dictionary] writeToFile:plist atomically:YES]; |
(...skipping 12 matching lines...) Expand all Loading... |
375 --g_lock_depth; | 400 --g_lock_depth; |
376 CHECK(g_lock_depth >= 0); | 401 CHECK(g_lock_depth >= 0); |
377 | 402 |
378 if (g_lock_depth > 0) { | 403 if (g_lock_depth > 0) { |
379 // Other locks are still using store_, don't free it yet. | 404 // Other locks are still using store_, don't free it yet. |
380 ignore_result(store_.release()); | 405 ignore_result(store_.release()); |
381 return; | 406 return; |
382 } | 407 } |
383 | 408 |
384 if (store_.get()) { | 409 if (store_.get()) { |
385 CHECK(g_file_lock); | |
386 g_store_object = NULL; | 410 g_store_object = NULL; |
387 | 411 |
388 NSString* folder = CreateRlzDirectory(); | 412 NSString* folder = CreateRlzDirectory(); |
389 NSDictionary* dict = | 413 NSDictionary* dict = |
390 static_cast<RlzValueStoreMac*>(store_.get())->dictionary(); | 414 static_cast<RlzValueStoreMac*>(store_.get())->dictionary(); |
391 VERIFY([dict writeToFile:RlzPlistFilename() atomically:YES]); | 415 VERIFY([dict writeToFile:RlzPlistFilename() atomically:YES]); |
392 } | 416 } |
393 | 417 |
394 if (g_file_lock) { | 418 // Check that "store_ set" => "file_lock acquired". The converse isn't true, |
395 [g_file_lock unlock]; | 419 // for example if the rlz data file can't be read. |
396 [g_file_lock release]; | 420 if (store_.get()) |
397 g_file_lock = nil; | 421 CHECK(g_recursive_lock.file_lock_); |
398 } else { | 422 if (!g_recursive_lock.file_lock_) |
399 CHECK(!store_.get()); | 423 CHECK(!store_.get()); |
400 } | 424 |
401 | 425 g_recursive_lock.ReleaseLock(); |
402 g_locking_thread = 0; | |
403 pthread_mutex_unlock(&g_recursive_lock); | |
404 } | 426 } |
405 | 427 |
406 RlzValueStore* ScopedRlzValueStoreLock::GetStore() { | 428 RlzValueStore* ScopedRlzValueStoreLock::GetStore() { |
407 return store_.get(); | 429 return store_.get(); |
408 } | 430 } |
409 | 431 |
410 namespace testing { | 432 namespace testing { |
411 | 433 |
412 void SetRlzStoreDirectory(const FilePath& directory) { | 434 void SetRlzStoreDirectory(const FilePath& directory) { |
413 base::mac::ScopedNSAutoreleasePool pool; | 435 base::mac::ScopedNSAutoreleasePool pool; |
414 | 436 |
415 [g_test_folder release]; | 437 [g_test_folder release]; |
416 if (directory.empty()) { | 438 if (directory.empty()) { |
417 g_test_folder = nil; | 439 g_test_folder = nil; |
418 } else { | 440 } else { |
419 // Not Unsafe on OS X. | 441 // Not Unsafe on OS X. |
420 g_test_folder = | 442 g_test_folder = |
421 [[NSString alloc] initWithUTF8String:directory.AsUTF8Unsafe().c_str()]; | 443 [[NSString alloc] initWithUTF8String:directory.AsUTF8Unsafe().c_str()]; |
422 } | 444 } |
423 } | 445 } |
424 | 446 |
425 } // namespace testing | 447 } // namespace testing |
426 | 448 |
427 } // namespace rlz_lib | 449 } // namespace rlz_lib |
LEFT | RIGHT |