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"
24
/// @brief Platform agnostic Mutex class.
27
/// @name Constructors
31
/// Initializes the lock but doesn't acquire it. if \p recursive is set
32
/// to false, the lock will not be recursive which makes it cheaper but
33
/// also more likely to deadlock (same thread can't acquire more than
35
/// @brief Default Constructor.
36
explicit MutexImpl(bool recursive = true);
38
/// Releases and removes the lock
47
/// Attempts to unconditionally acquire the lock. If the lock is held by
48
/// another thread, this method will wait until it can acquire the lock.
49
/// @returns false if any kind of error occurs, true otherwise.
50
/// @brief Unconditionally acquire the lock.
53
/// Attempts to release the lock. If the lock is held by the current
54
/// thread, the lock is released allowing other threads to acquire the
56
/// @returns false if any kind of error occurs, true otherwise.
57
/// @brief Unconditionally release the lock.
60
/// Attempts to acquire the lock without blocking. If the lock is not
61
/// available, this function returns false quickly (without blocking). If
62
/// the lock is available, it is acquired.
63
/// @returns false if any kind of error occurs or the lock is not
64
/// available, true otherwise.
65
/// @brief Try to acquire the lock.
69
/// @name Platform Dependent Data
72
void* data_; ///< We don't know what the data will be
75
/// @name Do Not Implement
78
MutexImpl(const MutexImpl & original);
79
void operator=(const MutexImpl &);
84
/// SmartMutex - A mutex with a compile time constant parameter that
85
/// indicates whether this mutex should become a no-op when we're not
86
/// running in multithreaded mode.
87
template<bool mt_only>
88
class SmartMutex : public MutexImpl {
92
explicit SmartMutex(bool rec = true) :
93
MutexImpl(rec), acquired(0), recursive(rec) { }
96
if (!mt_only || llvm_is_multithreaded()) {
97
return MutexImpl::acquire();
99
// Single-threaded debugging code. This would be racy in
100
// multithreaded mode, but provides not sanity checks in single
102
assert((recursive || acquired == 0) && "Lock already acquired!!");
109
if (!mt_only || llvm_is_multithreaded()) {
110
return MutexImpl::release();
112
// Single-threaded debugging code. This would be racy in
113
// multithreaded mode, but provides not sanity checks in single
115
assert(((recursive && acquired) || (acquired == 1)) &&
116
"Lock not acquired before release!");
123
if (!mt_only || llvm_is_multithreaded())
124
return MutexImpl::tryacquire();
129
SmartMutex(const SmartMutex<mt_only> & original);
130
void operator=(const SmartMutex<mt_only> &);
133
/// Mutex - A standard, always enforced mutex.
134
typedef SmartMutex<false> Mutex;
136
template<bool mt_only>
137
class SmartScopedLock {
138
SmartMutex<mt_only>& mtx;
141
SmartScopedLock(SmartMutex<mt_only>& m) : mtx(m) {
150
typedef SmartScopedLock<false> ScopedLock;