diff options
author | ivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-29 14:00:12 +0000 |
---|---|---|
committer | ivankr@chromium.org <ivankr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-29 14:00:12 +0000 |
commit | 95b42e2745a2380a16112a059bd0e842d81f0c0a (patch) | |
tree | 8715285e587b94807bcc70cf135d99bdbec662fb /rlz/mac | |
parent | 1b14a45ac508a066cc3c060dd37327c3a13a6fda (diff) | |
download | chromium_src-95b42e2745a2380a16112a059bd0e842d81f0c0a.zip chromium_src-95b42e2745a2380a16112a059bd0e842d81f0c0a.tar.gz chromium_src-95b42e2745a2380a16112a059bd0e842d81f0c0a.tar.bz2 |
[cros] RlzValueStore made protected by a cross-process lock and not persisted over browser lifetime (like on Mac).
*) Moved RecursiveCrossProcessLock out of .mm file to a common _posix file.
*) Added static method to ImportantFileWriter that does blocking write on the current thread.
*) Dedicated RLZ thread gone, replaced back with shutdown-blocking worker pool.
BUG=157348,62328
Review URL: https://chromiumcodereview.appspot.com/11308196
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@170179 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'rlz/mac')
-rw-r--r-- | rlz/mac/lib/rlz_value_store_mac.mm | 101 |
1 files changed, 6 insertions, 95 deletions
diff --git a/rlz/mac/lib/rlz_value_store_mac.mm b/rlz/mac/lib/rlz_value_store_mac.mm index 6148e54..6b8e6f4 100644 --- a/rlz/mac/lib/rlz_value_store_mac.mm +++ b/rlz/mac/lib/rlz_value_store_mac.mm @@ -7,11 +7,11 @@ #include "base/mac/foundation_util.h" #include "base/file_path.h" #include "base/logging.h" -#include "base/posix/eintr_wrapper.h" #include "base/sys_string_conversions.h" #include "rlz/lib/assert.h" #include "rlz/lib/lib_values.h" #include "rlz/lib/rlz_lib.h" +#include "rlz/lib/recursive_cross_process_lock_posix.h" #import <Foundation/Foundation.h> #include <pthread.h> @@ -217,97 +217,8 @@ NSMutableDictionary* RlzValueStoreMac::ProductDict(Product p) { namespace { -// Creating a recursive cross-process mutex on windows is one line. On mac, -// there's no primitve for that, so this lock is emulated by an in-process -// mutex to get the recursive part, followed by a cross-process lock for the -// cross-process part. - -// This is a struct so that it doesn't need a static initializer. -struct RecursiveCrossProcessLock { - // Tries to acquire a recursive cross-process lock. Note that this _always_ - // acquires the in-process lock (if it wasn't already acquired). The parent - // directory of |lock_file| must exist. - bool TryGetCrossProcessLock(NSString* lock_filename); - - // Releases the lock. Should always be called, even if - // TryGetCrossProcessLock() returns false. - void ReleaseLock(); - - pthread_mutex_t recursive_lock_; - pthread_t locking_thread_; - - int file_lock_; -} 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 - // recursive locking with a normal non-recursive mutex. - PTHREAD_MUTEX_INITIALIZER, - 0, - -1 -}; - -bool RecursiveCrossProcessLock::TryGetCrossProcessLock( - NSString* lock_filename) { - bool just_got_lock = false; - - // Emulate a recursive mutex with a non-recursive one. - if (pthread_mutex_trylock(&recursive_lock_) == EBUSY) { - if (pthread_equal(pthread_self(), locking_thread_) == 0) { - // Some other thread has the lock, wait for it. - pthread_mutex_lock(&recursive_lock_); - CHECK(locking_thread_ == 0); - just_got_lock = true; - } - } else { - just_got_lock = true; - } - - locking_thread_ = pthread_self(); - - // Try to acquire file lock. - if (just_got_lock) { - const int kMaxTimeoutMS = 5000; // Matches windows. - const int kSleepPerTryMS = 200; - - CHECK(file_lock_ == -1); - file_lock_ = open([lock_filename fileSystemRepresentation], - O_RDWR | O_CREAT, - 0666); - if (file_lock_ == -1) - return false; - - int flock_result = -1; - int elapsed_ms = 0; - while ((flock_result = - HANDLE_EINTR(flock(file_lock_, LOCK_EX | LOCK_NB))) == -1 && - errno == EWOULDBLOCK && - elapsed_ms < kMaxTimeoutMS) { - usleep(kSleepPerTryMS * 1000); - elapsed_ms += kSleepPerTryMS; - } - - if (flock_result == -1) { - ignore_result(HANDLE_EINTR(close(file_lock_))); - file_lock_ = -1; - return false; - } - return true; - } else { - return file_lock_ != -1; - } -} - -void RecursiveCrossProcessLock::ReleaseLock() { - if (file_lock_ != -1) { - ignore_result(HANDLE_EINTR(flock(file_lock_, LOCK_UN))); - ignore_result(HANDLE_EINTR(close(file_lock_))); - file_lock_ = -1; - } - - locking_thread_ = 0; - pthread_mutex_unlock(&recursive_lock_); -} - +RecursiveCrossProcessLock g_recursive_lock = + RECURSIVE_CROSS_PROCESS_LOCK_INITIALIZER; // This is set during test execution, to write RLZ files into a temporary // directory instead of the user's Application Support folder. @@ -363,8 +274,8 @@ NSString* RlzLockFilename() { } // namespace ScopedRlzValueStoreLock::ScopedRlzValueStoreLock() { - bool got_distributed_lock = - g_recursive_lock.TryGetCrossProcessLock(RlzLockFilename()); + bool got_distributed_lock = g_recursive_lock.TryGetCrossProcessLock( + FilePath([RlzLockFilename() fileSystemRepresentation])); // At this point, we hold the in-process lock, no matter the value of // |got_distributed_lock|. @@ -451,7 +362,7 @@ void SetRlzStoreDirectory(const FilePath& directory) { } } -std::string RlzPlistFilenameStr() { +std::string RlzStoreFilenameStr() { @autoreleasepool { return std::string([RlzPlistFilename() fileSystemRepresentation]); } |