recursive_cross_process_lock_posix.h 1.6 KB

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