1 //===- llvm/System/Mutex.h - Mutex Operating System Concept -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the llvm::sys::Mutex class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SYSTEM_MUTEX_H
15 #define LLVM_SYSTEM_MUTEX_H
17 #include "llvm/System/Threading.h"
23 /// @brief Platform agnostic Mutex class.
26 /// @name Constructors
30 /// Initializes the lock but doesn't acquire it. if \p recursive is set
31 /// to false, the lock will not be recursive which makes it cheaper but
32 /// also more likely to deadlock (same thread can't acquire more than
34 /// @brief Default Constructor.
35 explicit MutexImpl(bool recursive = true);
37 /// Releases and removes the lock
46 /// Attempts to unconditionally acquire the lock. If the lock is held by
47 /// another thread, this method will wait until it can acquire the lock.
48 /// @returns false if any kind of error occurs, true otherwise.
49 /// @brief Unconditionally acquire the lock.
52 /// Attempts to release the lock. If the lock is held by the current
53 /// thread, the lock is released allowing other threads to acquire the
55 /// @returns false if any kind of error occurs, true otherwise.
56 /// @brief Unconditionally release the lock.
59 /// Attempts to acquire the lock without blocking. If the lock is not
60 /// available, this function returns false quickly (without blocking). If
61 /// the lock is available, it is acquired.
62 /// @returns false if any kind of error occurs or the lock is not
63 /// available, true otherwise.
64 /// @brief Try to acquire the lock.
68 /// @name Platform Dependent Data
72 void* data_; ///< We don't know what the data will be
76 /// @name Do Not Implement
79 MutexImpl(const MutexImpl & original);
80 void operator=(const MutexImpl &);
85 /// SmartMutex - A mutex with a compile time constant parameter that
86 /// indicates whether this mutex should become a no-op when we're not
87 /// running in multithreaded mode.
88 template<bool mt_only>
89 class SmartMutex : public MutexImpl {
91 explicit SmartMutex(bool recursive = true) : MutexImpl(recursive) { }
94 if (!mt_only && llvm_is_multithreaded())
95 return MutexImpl::acquire();
100 if (!mt_only || llvm_is_multithreaded())
101 return MutexImpl::release();
106 if (!mt_only || llvm_is_multithreaded())
107 return MutexImpl::tryacquire();
112 SmartMutex(const SmartMutex<mt_only> & original);
113 void operator=(const SmartMutex<mt_only> &);
116 /// Mutex - A standard, always enforced mutex.
117 typedef SmartMutex<false> Mutex;
119 template<bool mt_only>
120 class SmartScopedLock {
121 SmartMutex<mt_only>* mtx;
124 SmartScopedLock(SmartMutex<mt_only>* m) : mtx(m) {
133 typedef SmartScopedLock<false> ScopedLock;