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"
23 bool llvm_is_multithreaded();
27 /// @brief Platform agnostic Mutex class.
30 /// @name Constructors
34 /// Initializes the lock but doesn't acquire it. if \p recursive is set
35 /// to false, the lock will not be recursive which makes it cheaper but
36 /// also more likely to deadlock (same thread can't acquire more than
38 /// @brief Default Constructor.
39 explicit MutexImpl(bool recursive = true);
41 /// Releases and removes the lock
50 /// Attempts to unconditionally acquire the lock. If the lock is held by
51 /// another thread, this method will wait until it can acquire the lock.
52 /// @returns false if any kind of error occurs, true otherwise.
53 /// @brief Unconditionally acquire the lock.
56 /// Attempts to release the lock. If the lock is held by the current
57 /// thread, the lock is released allowing other threads to acquire the
59 /// @returns false if any kind of error occurs, true otherwise.
60 /// @brief Unconditionally release the lock.
63 /// Attempts to acquire the lock without blocking. If the lock is not
64 /// available, this function returns false quickly (without blocking). If
65 /// the lock is available, it is acquired.
66 /// @returns false if any kind of error occurs or the lock is not
67 /// available, true otherwise.
68 /// @brief Try to acquire the lock.
72 /// @name Platform Dependent Data
75 void* data_; ///< We don't know what the data will be
78 /// @name Do Not Implement
81 MutexImpl(const MutexImpl &) LLVM_DELETED_FUNCTION;
82 void operator=(const MutexImpl &) LLVM_DELETED_FUNCTION;
87 /// SmartMutex - A mutex with a compile time constant parameter that
88 /// indicates whether this mutex should become a no-op when we're not
89 /// running in multithreaded mode.
90 template<bool mt_only>
91 class SmartMutex : public MutexImpl {
95 explicit SmartMutex(bool rec = true) :
96 MutexImpl(rec), acquired(0), recursive(rec) { }
99 if (!mt_only || llvm_is_multithreaded()) {
100 return MutexImpl::acquire();
102 // Single-threaded debugging code. This would be racy in
103 // multithreaded mode, but provides not sanity checks in single
105 assert((recursive || acquired == 0) && "Lock already acquired!!");
112 if (!mt_only || llvm_is_multithreaded()) {
113 return MutexImpl::release();
115 // Single-threaded debugging code. This would be racy in
116 // multithreaded mode, but provides not sanity checks in single
118 assert(((recursive && acquired) || (acquired == 1)) &&
119 "Lock not acquired before release!");
126 if (!mt_only || llvm_is_multithreaded())
127 return MutexImpl::tryacquire();
132 SmartMutex(const SmartMutex<mt_only> & original);
133 void operator=(const SmartMutex<mt_only> &);
136 /// Mutex - A standard, always enforced mutex.
137 typedef SmartMutex<false> Mutex;
139 template<bool mt_only>
140 class SmartScopedLock {
141 SmartMutex<mt_only>& mtx;
144 SmartScopedLock(SmartMutex<mt_only>& m) : mtx(m) {
153 typedef SmartScopedLock<false> ScopedLock;