| 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/mac/lib/rlz_value_store_mac.h" | 5 #include "rlz/mac/lib/rlz_value_store_mac.h" |
| 6 | 6 |
| 7 #include "base/eintr_wrapper.h" | 7 #include "base/eintr_wrapper.h" |
| 8 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 } | 263 } |
| 264 | 264 |
| 265 locking_thread_ = pthread_self(); | 265 locking_thread_ = pthread_self(); |
| 266 | 266 |
| 267 // Try to acquire file lock. | 267 // Try to acquire file lock. |
| 268 if (just_got_lock) { | 268 if (just_got_lock) { |
| 269 const int kMaxTimeoutMS = 5000; // Matches windows. | 269 const int kMaxTimeoutMS = 5000; // Matches windows. |
| 270 const int kSleepPerTryMS = 200; | 270 const int kSleepPerTryMS = 200; |
| 271 | 271 |
| 272 CHECK(file_lock_ == -1); | 272 CHECK(file_lock_ == -1); |
| 273 file_lock_ = open([lock_filename fileSystemRepresentation], O_CREAT, 0666); | 273 file_lock_ = open([lock_filename fileSystemRepresentation], |
| 274 O_RDWR | O_CREAT, |
| 275 0666); |
| 274 if (file_lock_ == -1) | 276 if (file_lock_ == -1) |
| 275 return false; | 277 return false; |
| 276 | 278 |
| 277 int flock_result = -1; | 279 int flock_result = -1; |
| 278 int elapsed_ms = 0; | 280 int elapsed_ms = 0; |
| 279 while ((flock_result = | 281 while ((flock_result = |
| 280 HANDLE_EINTR(flock(file_lock_, LOCK_EX | LOCK_NB))) == -1 && | 282 HANDLE_EINTR(flock(file_lock_, LOCK_EX | LOCK_NB))) == -1 && |
| 281 errno == EWOULDBLOCK && | 283 errno == EWOULDBLOCK && |
| 282 elapsed_ms < kMaxTimeoutMS) { | 284 elapsed_ms < kMaxTimeoutMS) { |
| 283 usleep(kSleepPerTryMS * 1000); | 285 usleep(kSleepPerTryMS * 1000); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 // Returns the path of the rlz plist store, also creates the parent directory | 349 // Returns the path of the rlz plist store, also creates the parent directory |
| 348 // path if it doesn't exist. | 350 // path if it doesn't exist. |
| 349 NSString* RlzPlistFilename() { | 351 NSString* RlzPlistFilename() { |
| 350 NSString* const kRlzFile = @"RlzStore.plist"; | 352 NSString* const kRlzFile = @"RlzStore.plist"; |
| 351 return [CreateRlzDirectory() stringByAppendingPathComponent:kRlzFile]; | 353 return [CreateRlzDirectory() stringByAppendingPathComponent:kRlzFile]; |
| 352 } | 354 } |
| 353 | 355 |
| 354 // Returns the path of the rlz lock file, also creates the parent directory | 356 // Returns the path of the rlz lock file, also creates the parent directory |
| 355 // path if it doesn't exist. | 357 // path if it doesn't exist. |
| 356 NSString* RlzLockFilename() { | 358 NSString* RlzLockFilename() { |
| 357 NSString* const kRlzFile = @"flockfile"; | 359 NSString* const kRlzLockfile = @"flockfile"; |
| 358 return [CreateRlzDirectory() stringByAppendingPathComponent:kRlzFile]; | 360 return [CreateRlzDirectory() stringByAppendingPathComponent:kRlzLockfile]; |
| 359 } | 361 } |
| 360 | 362 |
| 361 } // namespace | 363 } // namespace |
| 362 | 364 |
| 363 ScopedRlzValueStoreLock::ScopedRlzValueStoreLock() { | 365 ScopedRlzValueStoreLock::ScopedRlzValueStoreLock() { |
| 364 bool got_distributed_lock = | 366 bool got_distributed_lock = |
| 365 g_recursive_lock.TryGetCrossProcessLock(RlzLockFilename()); | 367 g_recursive_lock.TryGetCrossProcessLock(RlzLockFilename()); |
| 366 // At this point, we hold the in-process lock, no matter the value of | 368 // At this point, we hold the in-process lock, no matter the value of |
| 367 // |got_distributed_lock|. | 369 // |got_distributed_lock|. |
| 368 | 370 |
| 369 ++g_lock_depth; | 371 ++g_lock_depth; |
| 370 | 372 |
| 371 if (!got_distributed_lock) { | 373 if (!got_distributed_lock) { |
| 372 // Give up. |store_| isn't set, which signals to callers that acquiring | 374 // Give up. |store_| isn't set, which signals to callers that acquiring |
| 373 // the lock failed. |g_recursive_lock| will be released by the | 375 // the lock failed. |g_recursive_lock| will be released by the |
| 374 // destructor. | 376 // destructor. |
| 375 CHECK(!g_store_object); | 377 CHECK(!g_store_object); |
| 376 return; | 378 return; |
| 377 } | 379 } |
| 378 | 380 |
| 379 if (g_lock_depth > 1) { | 381 if (g_lock_depth > 1) { |
| 380 // Reuse the already existing store object. | 382 // Reuse the already existing store object. Note that it can be NULL when |
| 381 CHECK(g_store_object); | 383 // lock acquisition succeeded but the rlz data file couldn't be read. |
| 382 store_.reset(g_store_object); | 384 store_.reset(g_store_object); |
| 383 return; | 385 return; |
| 384 } | 386 } |
| 385 | 387 |
| 386 CHECK(!g_store_object); | 388 CHECK(!g_store_object); |
| 387 | 389 |
| 388 NSString* plist = RlzPlistFilename(); | 390 NSString* plist = RlzPlistFilename(); |
| 389 | 391 |
| 390 // Create an empty file if none exists yet. | 392 // Create an empty file if none exists yet. |
| 391 NSFileManager* manager = [NSFileManager defaultManager]; | 393 NSFileManager* manager = [NSFileManager defaultManager]; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 [g_test_folder release]; | 444 [g_test_folder release]; |
| 443 if (directory.empty()) { | 445 if (directory.empty()) { |
| 444 g_test_folder = nil; | 446 g_test_folder = nil; |
| 445 } else { | 447 } else { |
| 446 // Not Unsafe on OS X. | 448 // Not Unsafe on OS X. |
| 447 g_test_folder = | 449 g_test_folder = |
| 448 [[NSString alloc] initWithUTF8String:directory.AsUTF8Unsafe().c_str()]; | 450 [[NSString alloc] initWithUTF8String:directory.AsUTF8Unsafe().c_str()]; |
| 449 } | 451 } |
| 450 } | 452 } |
| 451 | 453 |
| 454 std::string RlzPlistFilenameStr() { |
| 455 @autoreleasepool { |
| 456 return std::string([RlzPlistFilename() fileSystemRepresentation]); |
| 457 } |
| 458 } |
| 459 |
| 452 } // namespace testing | 460 } // namespace testing |
| 453 | 461 |
| 454 } // namespace rlz_lib | 462 } // namespace rlz_lib |
| OLD | NEW |