-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Expand file tree
/
Copy pathMutex.h
More file actions
290 lines (242 loc) · 10.6 KB
/
Copy pathMutex.h
File metadata and controls
290 lines (242 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//===--- Mutex.h - Mutex and ScopedLock ----------------------- -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Provides a system-independent Mutex abstraction.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_THREADING_MUTEX_H
#define SWIFT_THREADING_MUTEX_H
#include <type_traits>
#include <utility>
#include "ScopedLock.h"
#include "Impl.h"
namespace swift {
/// A Mutex object that supports `BasicLockable` and `Lockable` C++ concepts.
/// See http://en.cppreference.com/w/cpp/concept/BasicLockable
/// See http://en.cppreference.com/w/cpp/concept/Lockable
///
/// This is NOT a recursive mutex.
class Mutex {
Mutex(const Mutex &) = delete;
Mutex &operator=(const Mutex &) = delete;
Mutex(Mutex &&) = delete;
Mutex &operator=(Mutex &&) = delete;
public:
/// Constructs a non-recursive mutex.
///
/// If `checked` is true the mutex will attempt to check for misuse and
/// fatalError when detected. If `checked` is false (the default) the
/// mutex will make little to no effort to check for misuse (more efficient).
explicit Mutex(bool checked = false) {
threading_impl::mutex_init(Handle, checked);
}
~Mutex() { threading_impl::mutex_destroy(Handle); }
/// The lock() method has the following properties:
/// - Behaves as an atomic operation.
/// - Blocks the calling thread until exclusive ownership of the mutex
/// can be obtained.
/// - Prior m.unlock() operations on the same mutex synchronize-with
/// this lock operation.
/// - The behavior is undefined if the calling thread already owns
/// the mutex (likely a deadlock).
/// - Does not throw exceptions but will halt on error (fatalError).
void lock() { threading_impl::mutex_lock(Handle); }
/// The unlock() method has the following properties:
/// - Behaves as an atomic operation.
/// - Releases the calling thread's ownership of the mutex and
/// synchronizes-with the subsequent successful lock operations on
/// the same object.
/// - The behavior is undefined if the calling thread does not own
/// the mutex.
/// - Does not throw exceptions but will halt on error (fatalError).
void unlock() { threading_impl::mutex_unlock(Handle); }
/// The try_lock() method has the following properties:
/// - Behaves as an atomic operation.
/// - Attempts to obtain exclusive ownership of the mutex for the calling
/// thread without blocking. If ownership is not obtained, returns
/// immediately. The function is allowed to spuriously fail and return
/// even if the mutex is not currently owned by another thread.
/// - If try_lock() succeeds, prior unlock() operations on the same object
/// synchronize-with this operation. lock() does not synchronize with a
/// failed try_lock()
/// - The behavior is undefined if the calling thread already owns
/// the mutex (likely a deadlock)?
/// - Does not throw exceptions but will halt on error (fatalError).
bool try_lock() { return threading_impl::mutex_try_lock(Handle); }
/// Acquires lock before calling the supplied critical section and releases
/// lock on return from critical section.
///
/// This call can block while waiting for the lock to become available.
///
/// For example the following mutates value while holding the mutex lock.
///
/// ```
/// mutex.lock([&value] { value++; });
/// ```
///
/// Precondition: Mutex not held by this thread, undefined otherwise.
template <typename CriticalSection>
auto withLock(CriticalSection &&criticalSection)
-> decltype(std::forward<CriticalSection>(criticalSection)()) {
ScopedLock guard(*this);
return std::forward<CriticalSection>(criticalSection)();
}
/// A stack based object that locks the supplied mutex on construction
/// and unlocks it on destruction.
///
/// Precondition: Mutex unlocked by this thread, undefined otherwise.
typedef ScopedLockT<Mutex, false> ScopedLock;
/// A stack based object that unlocks the supplied mutex on construction
/// and relocks it on destruction.
///
/// Precondition: Mutex locked by this thread, undefined otherwise.
typedef ScopedLockT<Mutex, true> ScopedUnlock;
protected:
threading_impl::mutex_handle Handle;
};
/// An unsafe variant of the above (for use in the error handling path)
///
/// This is used to ensure that we can't infinitely recurse if the mutex
/// itself generates errors.
class UnsafeMutex : public Mutex {
public:
UnsafeMutex() : Mutex() {}
void lock() { threading_impl::mutex_unsafe_unlock(Handle); }
void unlock() { threading_impl::mutex_unsafe_unlock(Handle); }
};
/// A lazily initialized variant of Mutex.
///
/// Use Mutex instead unless you need static allocation. LazyMutex *may*
/// be entirely statically initialized, on some platforms, but on others
/// it might be a little larger than and slightly slower than Mutex.
#if SWIFT_THREADING_HAS_LAZY_MUTEX
class LazyMutex {
LazyMutex(const LazyMutex &) = delete;
LazyMutex &operator=(const LazyMutex &) = delete;
LazyMutex(LazyMutex &&) = delete;
LazyMutex &operator=(LazyMutex &&) = delete;
public:
constexpr LazyMutex() : Handle(SWIFT_LAZY_MUTEX_INITIALIZER) {}
// No destructor; this is intentional; this class is for STATIC allocation
// and you don't need to delete mutexes on termination.
/// See Mutex::lock
void lock() { threading_impl::lazy_mutex_lock(Handle); }
/// See Mutex::unlock
void unlock() { threading_impl::lazy_mutex_unlock(Handle); }
/// See Mutex::try_lock
bool try_lock() { return threading_impl::lazy_mutex_try_lock(Handle); }
/// See Mutex::withLock
template <typename CriticalSection>
auto withLock(CriticalSection &&criticalSection)
-> decltype(std::forward<CriticalSection>(criticalSection)()) {
ScopedLock guard(*this);
return std::forward<CriticalSection>(criticalSection)();
}
/// A stack based object that locks the supplied mutex on construction
/// and unlocks it on destruction.
///
/// Precondition: Mutex unlocked by this thread, undefined otherwise.
typedef ScopedLockT<LazyMutex, false> ScopedLock;
/// A stack based object that unlocks the supplied mutex on construction
/// and relocks it on destruction.
///
/// Precondition: Mutex locked by this thread, undefined otherwise.
typedef ScopedLockT<LazyMutex, true> ScopedUnlock;
protected:
threading_impl::lazy_mutex_handle Handle;
};
/// An unsafe variant of the above (for use in the error handling path)
///
/// This is used to ensure that we can't infinitely recurse if the mutex
/// itself generates errors.
class LazyUnsafeMutex : public LazyMutex {
public:
constexpr LazyUnsafeMutex() : LazyMutex() {}
void lock() { threading_impl::lazy_mutex_unsafe_lock(Handle); }
void unlock() { threading_impl::lazy_mutex_unsafe_unlock(Handle); }
};
#endif // SWIFT_THREADING_HAS_LAZY_MUTEX
/// An indirect variant of a Mutex. This allocates the mutex on the heap, for
/// places where having the mutex inline takes up too much space. Used for
/// SmallMutex on platforms where Mutex is large.
class IndirectMutex {
IndirectMutex(const IndirectMutex &) = delete;
IndirectMutex &operator=(const IndirectMutex &) = delete;
IndirectMutex(IndirectMutex &&) = delete;
IndirectMutex &operator=(IndirectMutex &&) = delete;
public:
explicit IndirectMutex(bool checked = false) { Ptr = new Mutex(checked); }
~IndirectMutex() { delete Ptr; }
void lock() { Ptr->lock(); }
void unlock() { Ptr->unlock(); }
bool try_lock() { return Ptr->try_lock(); }
template <typename CriticalSection>
auto withLock(CriticalSection &&criticalSection)
-> decltype(criticalSection()) {
return Ptr->withLock(std::forward<CriticalSection>(criticalSection));
}
/// A stack based object that locks the supplied mutex on construction
/// and unlocks it on destruction.
///
/// Precondition: Mutex unlocked by this thread, undefined otherwise.
typedef ScopedLockT<IndirectMutex, false> ScopedLock;
/// A stack based object that unlocks the supplied mutex on construction
/// and relocks it on destruction.
///
/// Precondition: Mutex locked by this thread, undefined otherwise.
typedef ScopedLockT<IndirectMutex, true> ScopedUnlock;
private:
Mutex *Ptr;
};
/// A "small" mutex, which is pointer sized or smaller, for places where the
/// mutex is stored inline with limited storage space. This uses a normal Mutex
/// when that is small, and otherwise uses IndirectMutex.
using SmallMutex =
std::conditional_t<sizeof(Mutex) <= sizeof(void *), Mutex, IndirectMutex>;
/// A recursive variant of Mutex.
class RecursiveMutex {
RecursiveMutex(const RecursiveMutex &) = delete;
RecursiveMutex &operator=(const RecursiveMutex &) = delete;
RecursiveMutex(RecursiveMutex &&) = delete;
RecursiveMutex &operator=(RecursiveMutex &&) = delete;
public:
/// Constructs a non-recursive mutex.
///
/// If `checked` is true the mutex will attempt to check for misuse and
/// fatalError when detected. If `checked` is false (the default) the
/// mutex will make little to no effort to check for misuse (more efficient).
explicit RecursiveMutex(bool checked = false) {
threading_impl::recursive_mutex_init(Handle, checked);
}
~RecursiveMutex() { threading_impl::recursive_mutex_destroy(Handle); }
/// The lock() method has the following properties:
/// - Behaves as an atomic operation.
/// - Blocks the calling thread until exclusive ownership of the mutex
/// can be obtained.
/// - Prior m.unlock() operations on the same mutex synchronize-with
/// this lock operation.
/// - Does not throw exceptions but will halt on error (fatalError).
void lock() { threading_impl::recursive_mutex_lock(Handle); }
/// The unlock() method has the following properties:
/// - Behaves as an atomic operation.
/// - Releases the calling thread's ownership of the mutex and
/// synchronizes-with the subsequent successful lock operations on
/// the same object.
/// - The behavior is undefined if the calling thread does not own
/// the mutex.
/// - Does not throw exceptions but will halt on error (fatalError).
void unlock() { threading_impl::recursive_mutex_unlock(Handle); }
protected:
threading_impl::recursive_mutex_handle Handle;
};
} // namespace swift
#endif // SWIFT_THREADING_MUTEX_H