• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef RLZ_LIB_RECURSIVE_CROSS_PROCESS_LOCK_POSIX_H_
6 #define RLZ_LIB_RECURSIVE_CROSS_PROCESS_LOCK_POSIX_H_
7 
8 #include <pthread.h>
9 
10 namespace base {
11 class FilePath;
12 }
13 
14 namespace rlz_lib {
15 
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);
26 
27   // Releases the lock. Should always be called, even if
28   // TryGetCrossProcessLock() returned |false|.
29   void ReleaseLock();
30 
31   pthread_mutex_t recursive_lock_;
32   pthread_t locking_thread_;
33 
34   int file_lock_;
35 };
36 
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 }
42 
43 }  // namespace rlz_lib
44 
45 #endif  // RLZ_LIB_RECURSIVE_CROSS_PROCESS_LOCK_POSIX_H_
46