Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / rlz / lib / recursive_cross_process_lock_posix.h
blob69a2c7a136b6e04a0f8095bb61704220caf653b4
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
3 // found in the LICENSE file.
5 #ifndef RLZ_LIB_RECURSIVE_CROSS_PROCESS_LOCK_POSIX_H_
6 #define RLZ_LIB_RECURSIVE_CROSS_PROCESS_LOCK_POSIX_H_
8 #include <pthread.h>
10 namespace base {
11 class FilePath;
14 namespace rlz_lib {
16 // Creating a recursive cross-process mutex on Windows is one line. On POSIX,
17 // there's no primitive for that, so this lock is emulated by an in-process
18 // mutex to get the recursive part, followed by a cross-process lock for the
19 // cross-process part.
20 // This is a struct so that it doesn't need a static initializer.
21 struct RecursiveCrossProcessLock {
22 // Tries to acquire a recursive cross-process lock. Note that this _always_
23 // acquires the in-process lock (if it wasn't already acquired). The parent
24 // directory of |lock_file| must exist.
25 bool TryGetCrossProcessLock(const base::FilePath& lock_filename);
27 // Releases the lock. Should always be called, even if
28 // TryGetCrossProcessLock() returned |false|.
29 void ReleaseLock();
31 pthread_mutex_t recursive_lock_;
32 pthread_t locking_thread_;
34 int file_lock_;
37 // On Mac, PTHREAD_RECURSIVE_MUTEX_INITIALIZER doesn't exist before 10.7 and
38 // is buggy on 10.7 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51906#c34),
39 // so emulate recursive locking with a normal non-recursive mutex.
40 #define RECURSIVE_CROSS_PROCESS_LOCK_INITIALIZER \
41 { PTHREAD_MUTEX_INITIALIZER, 0, -1 }
43 } // namespace rlz_lib
45 #endif // RLZ_LIB_RECURSIVE_CROSS_PROCESS_LOCK_POSIX_H_