1 //===- llvm/Support/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_SUPPORT_MUTEX_H
15 #define LLVM_SUPPORT_MUTEX_H
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Threading.h"
25 /// @brief Platform agnostic Mutex class.
28 /// @name Constructors
32 /// Initializes the lock but doesn't acquire it. if \p recursive is set
33 /// to false, the lock will not be recursive which makes it cheaper but
34 /// also more likely to deadlock (same thread can't acquire more than
36 /// @brief Default Constructor.
37 explicit MutexImpl(bool recursive = true);
39 /// Releases and removes the lock
48 /// Attempts to unconditionally acquire the lock. If the lock is held by
49 /// another thread, this method will wait until it can acquire the lock.
50 /// @returns false if any kind of error occurs, true otherwise.
51 /// @brief Unconditionally acquire the lock.
54 /// Attempts to release the lock. If the lock is held by the current
55 /// thread, the lock is released allowing other threads to acquire the
57 /// @returns false if any kind of error occurs, true otherwise.
58 /// @brief Unconditionally release the lock.
61 /// Attempts to acquire the lock without blocking. If the lock is not
62 /// available, this function returns false quickly (without blocking). If
63 /// the lock is available, it is acquired.
64 /// @returns false if any kind of error occurs or the lock is not
65 /// available, true otherwise.
66 /// @brief Try to acquire the lock.
70 /// @name Platform Dependent Data
73 void* data_; ///< We don't know what the data will be
76 /// @name Do Not Implement
79 MutexImpl(const MutexImpl &) LLVM_DELETED_FUNCTION;
80 void operator=(const MutexImpl &) LLVM_DELETED_FUNCTION;
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 {
93 explicit SmartMutex(bool rec = true) :
94 MutexImpl(rec), acquired(0), recursive(rec) { }
97 if (!mt_only || llvm_is_multithreaded()) {
98 return MutexImpl::acquire();
100 // Single-threaded debugging code. This would be racy in
101 // multithreaded mode, but provides not sanity checks in single
103 assert((recursive || acquired == 0) && "Lock already acquired!!");
110 if (!mt_only || llvm_is_multithreaded()) {
111 return MutexImpl::release();
113 // Single-threaded debugging code. This would be racy in
114 // multithreaded mode, but provides not sanity checks in single
116 assert(((recursive && acquired) || (acquired == 1)) &&
117 "Lock not acquired before release!");
124 if (!mt_only || llvm_is_multithreaded())
125 return MutexImpl::tryacquire();
130 SmartMutex(const SmartMutex<mt_only> & original);
131 void operator=(const SmartMutex<mt_only> &);
134 /// Mutex - A standard, always enforced mutex.
135 typedef SmartMutex<false> Mutex;
137 template<bool mt_only>
138 class SmartScopedLock {
139 SmartMutex<mt_only>& mtx;
142 SmartScopedLock(SmartMutex<mt_only>& m) : mtx(m) {
151 typedef SmartScopedLock<false> ScopedLock;