1 //===- llvm/Support/Mutex.h - Mutex Operating System Concept -----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file declares the llvm::sys::Mutex class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_SUPPORT_MUTEX_H
14 #define LLVM_SUPPORT_MUTEX_H
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Threading.h"
25 /// 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 /// 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 /// 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 /// 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 /// Try to acquire the lock.
70 /// @name Platform Dependent Data
73 #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0
74 void* data_
; ///< We don't know what the data will be
78 /// @name Do Not Implement
81 MutexImpl(const MutexImpl
&) = delete;
82 void operator=(const MutexImpl
&) = delete;
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
>
96 explicit SmartMutex(bool rec
= true) :
97 impl(rec
), acquired(0), recursive(rec
) { }
100 if (!mt_only
|| llvm_is_multithreaded()) {
101 return impl
.acquire();
103 // Single-threaded debugging code. This would be racy in
104 // multithreaded mode, but provides not sanity checks in single
106 assert((recursive
|| acquired
== 0) && "Lock already acquired!!");
113 if (!mt_only
|| llvm_is_multithreaded()) {
114 return impl
.release();
116 // Single-threaded debugging code. This would be racy in
117 // multithreaded mode, but provides not sanity checks in single
119 assert(((recursive
&& acquired
) || (acquired
== 1)) &&
120 "Lock not acquired before release!");
127 if (!mt_only
|| llvm_is_multithreaded())
128 return impl
.tryacquire();
133 SmartMutex(const SmartMutex
<mt_only
> & original
);
134 void operator=(const SmartMutex
<mt_only
> &);
137 /// Mutex - A standard, always enforced mutex.
138 typedef SmartMutex
<false> Mutex
;
140 template<bool mt_only
>
141 class SmartScopedLock
{
142 SmartMutex
<mt_only
>& mtx
;
145 SmartScopedLock(SmartMutex
<mt_only
>& m
) : mtx(m
) {
154 typedef SmartScopedLock
<false> ScopedLock
;