File indexing completed on 2026-05-10 08:44:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_SUPPORT_MUTEX_H
0014 #define LLVM_SUPPORT_MUTEX_H
0015
0016 #include "llvm/Support/Threading.h"
0017 #include <cassert>
0018 #include <mutex>
0019
0020 namespace llvm
0021 {
0022 namespace sys
0023 {
0024
0025
0026
0027 template<bool mt_only>
0028 class SmartMutex {
0029 std::recursive_mutex impl;
0030 unsigned acquired = 0;
0031
0032 public:
0033 bool lock() {
0034 if (!mt_only || llvm_is_multithreaded()) {
0035 impl.lock();
0036 return true;
0037 }
0038
0039
0040
0041 ++acquired;
0042 return true;
0043 }
0044
0045 bool unlock() {
0046 if (!mt_only || llvm_is_multithreaded()) {
0047 impl.unlock();
0048 return true;
0049 }
0050
0051
0052
0053 assert(acquired && "Lock not acquired before release!");
0054 --acquired;
0055 return true;
0056 }
0057
0058 bool try_lock() {
0059 if (!mt_only || llvm_is_multithreaded())
0060 return impl.try_lock();
0061 return true;
0062 }
0063 };
0064
0065
0066 typedef SmartMutex<false> Mutex;
0067
0068 template <bool mt_only>
0069 using SmartScopedLock = std::lock_guard<SmartMutex<mt_only>>;
0070
0071 typedef SmartScopedLock<false> ScopedLock;
0072 }
0073 }
0074
0075 #endif