1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MUTEX
11#define _LIBCPP_MUTEX
12
13/*
14 mutex synopsis
15
16namespace std
17{
18
19class mutex
20{
21public:
22 constexpr mutex() noexcept;
23 ~mutex();
24
25 mutex(const mutex&) = delete;
26 mutex& operator=(const mutex&) = delete;
27
28 void lock();
29 bool try_lock();
30 void unlock();
31
32 typedef pthread_mutex_t* native_handle_type;
33 native_handle_type native_handle();
34};
35
36class recursive_mutex
37{
38public:
39 recursive_mutex();
40 ~recursive_mutex();
41
42 recursive_mutex(const recursive_mutex&) = delete;
43 recursive_mutex& operator=(const recursive_mutex&) = delete;
44
45 void lock();
46 bool try_lock() noexcept;
47 void unlock();
48
49 typedef pthread_mutex_t* native_handle_type;
50 native_handle_type native_handle();
51};
52
53class timed_mutex
54{
55public:
56 timed_mutex();
57 ~timed_mutex();
58
59 timed_mutex(const timed_mutex&) = delete;
60 timed_mutex& operator=(const timed_mutex&) = delete;
61
62 void lock();
63 bool try_lock();
64 template <class Rep, class Period>
65 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
66 template <class Clock, class Duration>
67 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
68 void unlock();
69};
70
71class recursive_timed_mutex
72{
73public:
74 recursive_timed_mutex();
75 ~recursive_timed_mutex();
76
77 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
78 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
79
80 void lock();
81 bool try_lock() noexcept;
82 template <class Rep, class Period>
83 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
84 template <class Clock, class Duration>
85 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
86 void unlock();
87};
88
89struct defer_lock_t { explicit defer_lock_t() = default; };
90struct try_to_lock_t { explicit try_to_lock_t() = default; };
91struct adopt_lock_t { explicit adopt_lock_t() = default; };
92
93inline constexpr defer_lock_t defer_lock{};
94inline constexpr try_to_lock_t try_to_lock{};
95inline constexpr adopt_lock_t adopt_lock{};
96
97template <class Mutex>
98class lock_guard
99{
100public:
101 typedef Mutex mutex_type;
102
103 explicit lock_guard(mutex_type& m);
104 lock_guard(mutex_type& m, adopt_lock_t);
105 ~lock_guard();
106
107 lock_guard(lock_guard const&) = delete;
108 lock_guard& operator=(lock_guard const&) = delete;
109};
110
111template <class... MutexTypes>
112class scoped_lock // C++17
113{
114public:
115 using mutex_type = Mutex; // Only if sizeof...(MutexTypes) == 1
116
117 explicit scoped_lock(MutexTypes&... m);
118 scoped_lock(adopt_lock_t, MutexTypes&... m);
119 ~scoped_lock();
120 scoped_lock(scoped_lock const&) = delete;
121 scoped_lock& operator=(scoped_lock const&) = delete;
122private:
123 tuple<MutexTypes&...> pm; // exposition only
124};
125
126template <class Mutex>
127class unique_lock
128{
129public:
130 typedef Mutex mutex_type;
131 unique_lock() noexcept;
132 explicit unique_lock(mutex_type& m);
133 unique_lock(mutex_type& m, defer_lock_t) noexcept;
134 unique_lock(mutex_type& m, try_to_lock_t);
135 unique_lock(mutex_type& m, adopt_lock_t);
136 template <class Clock, class Duration>
137 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
138 template <class Rep, class Period>
139 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
140 ~unique_lock();
141
142 unique_lock(unique_lock const&) = delete;
143 unique_lock& operator=(unique_lock const&) = delete;
144
145 unique_lock(unique_lock&& u) noexcept;
146 unique_lock& operator=(unique_lock&& u) noexcept;
147
148 void lock();
149 bool try_lock();
150
151 template <class Rep, class Period>
152 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
153 template <class Clock, class Duration>
154 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
155
156 void unlock();
157
158 void swap(unique_lock& u) noexcept;
159 mutex_type* release() noexcept;
160
161 bool owns_lock() const noexcept;
162 explicit operator bool () const noexcept;
163 mutex_type* mutex() const noexcept;
164};
165
166template <class Mutex>
167 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
168
169template <class L1, class L2, class... L3>
170 int try_lock(L1&, L2&, L3&...);
171template <class L1, class L2, class... L3>
172 void lock(L1&, L2&, L3&...);
173
174struct once_flag
175{
176 constexpr once_flag() noexcept;
177
178 once_flag(const once_flag&) = delete;
179 once_flag& operator=(const once_flag&) = delete;
180};
181
182template<class Callable, class ...Args>
183 void call_once(once_flag& flag, Callable&& func, Args&&... args);
184
185} // std
186
187*/
188
189#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
190# include <__cxx03/mutex>
191#else
192# include <__chrono/steady_clock.h>
193# include <__chrono/time_point.h>
194# include <__condition_variable/condition_variable.h>
195# include <__config>
196# include <__mutex/lock_guard.h>
197# include <__mutex/mutex.h>
198# include <__mutex/once_flag.h>
199# include <__mutex/tag_types.h>
200# include <__mutex/unique_lock.h>
201# include <__thread/id.h>
202# include <__thread/support.h>
203# include <__utility/forward.h>
204# include <limits>
205# ifndef _LIBCPP_CXX03_LANG
206# include <tuple>
207# endif
208# include <version>
209
210# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
211# pragma GCC system_header
212# endif
213
214_LIBCPP_PUSH_MACROS
215# include <__undef_macros>
216
217# if _LIBCPP_HAS_THREADS
218
219_LIBCPP_BEGIN_NAMESPACE_STD
220_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
221
222class _LIBCPP_EXPORTED_FROM_ABI recursive_mutex {
223 __libcpp_recursive_mutex_t __m_;
224
225public:
226 recursive_mutex();
227 ~recursive_mutex();
228
229 recursive_mutex(const recursive_mutex&) = delete;
230 recursive_mutex& operator=(const recursive_mutex&) = delete;
231
232 void lock();
233 [[__nodiscard__]] bool try_lock() _NOEXCEPT;
234 void unlock() _NOEXCEPT;
235
236 typedef __libcpp_recursive_mutex_t* native_handle_type;
237
238 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
239};
240
241class _LIBCPP_EXPORTED_FROM_ABI timed_mutex {
242 mutex __m_;
243 condition_variable __cv_;
244 bool __locked_;
245
246public:
247 timed_mutex();
248 ~timed_mutex();
249
250 timed_mutex(const timed_mutex&) = delete;
251 timed_mutex& operator=(const timed_mutex&) = delete;
252
253public:
254 void lock();
255 [[__nodiscard__]] bool try_lock() _NOEXCEPT;
256 template <class _Rep, class _Period>
257 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
258 return try_lock_until(chrono::steady_clock::now() + __d);
259 }
260
261 template <class _Clock, class _Duration>
262 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
263 using namespace chrono;
264 unique_lock<mutex> __lk(__m_);
265 bool __no_timeout = _Clock::now() < __t;
266 while (__no_timeout && __locked_)
267 __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
268 if (!__locked_) {
269 __locked_ = true;
270 return true;
271 }
272 return false;
273 }
274
275 void unlock() _NOEXCEPT;
276};
277
278class _LIBCPP_EXPORTED_FROM_ABI recursive_timed_mutex {
279 mutex __m_;
280 condition_variable __cv_;
281 size_t __count_;
282 __thread_id __id_;
283
284public:
285 recursive_timed_mutex();
286 ~recursive_timed_mutex();
287
288 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
289 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
290
291 void lock();
292 [[__nodiscard__]] bool try_lock() _NOEXCEPT;
293 template <class _Rep, class _Period>
294 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
295 return try_lock_until(chrono::steady_clock::now() + __d);
296 }
297
298 template <class _Clock, class _Duration>
299 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
300 using namespace chrono;
301 __thread_id __id = this_thread::get_id();
302 unique_lock<mutex> __lk(__m_);
303 if (__id == __id_) {
304 if (__count_ == numeric_limits<size_t>::max())
305 return false;
306 ++__count_;
307 return true;
308 }
309 bool __no_timeout = _Clock::now() < __t;
310 while (__no_timeout && __count_ != 0)
311 __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
312 if (__count_ == 0) {
313 __count_ = 1;
314 __id_ = __id;
315 return true;
316 }
317 return false;
318 }
319
320 void unlock() _NOEXCEPT;
321};
322
323template <class _L0, class _L1>
324[[__nodiscard__]] _LIBCPP_NO_THREAD_SAFETY_ANALYSIS _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1) {
325 unique_lock<_L0> __u0(__l0, try_to_lock_t());
326 if (__u0.owns_lock()) {
327 if (__l1.try_lock()) {
328 __u0.release();
329 return -1;
330 } else
331 return 1;
332 }
333 return 0;
334}
335
336# ifndef _LIBCPP_CXX03_LANG
337
338template <class _L0, class _L1, class _L2, class... _L3>
339[[__nodiscard__]] _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
340 _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
341 int __r = 0;
342 unique_lock<_L0> __u0(__l0, try_to_lock);
343 if (__u0.owns_lock()) {
344 __r = std::try_lock(__l1, __l2, __l3...);
345 if (__r == -1)
346 __u0.release();
347 else
348 ++__r;
349 }
350 return __r;
351}
352
353# endif // _LIBCPP_CXX03_LANG
354
355// We're using unique_lock to implement the functions, which thread annotations don't support. So we have to disable
356// the analysis inside the function.
357template <class _L0, class _L1>
358_LIBCPP_NO_THREAD_SAFETY_ANALYSIS _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1)
359 _LIBCPP_ACQUIRE_CAPABILITY(__l0, __l1) {
360 while (true) {
361 {
362 unique_lock<_L0> __u0(__l0);
363 if (__l1.try_lock()) {
364 __u0.release();
365 break;
366 }
367 }
368 __libcpp_thread_yield();
369 {
370 unique_lock<_L1> __u1(__l1);
371 if (__l0.try_lock()) {
372 __u1.release();
373 break;
374 }
375 }
376 __libcpp_thread_yield();
377 }
378}
379
380# ifndef _LIBCPP_CXX03_LANG
381
382template <class _L0, class _L1, class _L2, class... _L3>
383_LIBCPP_NO_THREAD_SAFETY_ANALYSIS void __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
384 while (true) {
385 switch (__i) {
386 case 0: {
387 unique_lock<_L0> __u0(__l0);
388 __i = std::try_lock(__l1, __l2, __l3...);
389 if (__i == -1) {
390 __u0.release();
391 return;
392 }
393 }
394 ++__i;
395 __libcpp_thread_yield();
396 break;
397 case 1: {
398 unique_lock<_L1> __u1(__l1);
399 __i = std::try_lock(__l2, __l3..., __l0);
400 if (__i == -1) {
401 __u1.release();
402 return;
403 }
404 }
405 if (__i == sizeof...(_L3) + 1)
406 __i = 0;
407 else
408 __i += 2;
409 __libcpp_thread_yield();
410 break;
411 default:
412 std::__lock_first(__i - 2, __l2, __l3..., __l0, __l1);
413 return;
414 }
415 }
416}
417
418// We're using unique_lock to implement the functions, which thread annotations don't support. So we have to disable
419// the analysis inside the function.
420template <class _L0, class _L1, class _L2, class... _L3>
421_LIBCPP_NO_THREAD_SAFETY_ANALYSIS inline _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
422# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
423 _LIBCPP_ACQUIRE_CAPABILITY(__l0, __l1, __l2, __l3...)
424# endif
425{
426 std::__lock_first(0, __l0, __l1, __l2, __l3...);
427}
428
429# endif // _LIBCPP_CXX03_LANG
430
431# if _LIBCPP_STD_VER >= 17
432template <class... _Mutexes>
433class scoped_lock;
434
435template <>
436class scoped_lock<> {
437public:
438 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock() {}
439 ~scoped_lock() = default;
440
441 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t) {}
442
443 scoped_lock(scoped_lock const&) = delete;
444 scoped_lock& operator=(scoped_lock const&) = delete;
445};
446
447template <class _Mutex>
448class _LIBCPP_SCOPED_LOCKABLE scoped_lock<_Mutex> {
449public:
450 typedef _Mutex mutex_type;
451
452private:
453 mutex_type& __m_;
454
455public:
456 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(mutex_type& __m) _LIBCPP_ACQUIRE_CAPABILITY(__m)
457 : __m_(__m) {
458 __m_.lock();
459 }
460
461 _LIBCPP_RELEASE_CAPABILITY _LIBCPP_HIDE_FROM_ABI ~scoped_lock() { __m_.unlock(); }
462
463 [[nodiscard]]
464 _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_REQUIRES_CAPABILITY(__m)
465 : __m_(__m) {}
466
467 scoped_lock(scoped_lock const&) = delete;
468 scoped_lock& operator=(scoped_lock const&) = delete;
469};
470
471template <class... _MArgs>
472class scoped_lock {
473 static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
474 typedef tuple<_MArgs&...> _MutexTuple;
475
476public:
477 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(_MArgs&... __margs) : __t_(__margs...) {
478 std::lock(__margs...);
479 }
480
481 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI scoped_lock(adopt_lock_t, _MArgs&... __margs) : __t_(__margs...) {}
482
483 _LIBCPP_HIDE_FROM_ABI ~scoped_lock() { __unlock_unpack(make_index_sequence<sizeof...(_MArgs)>(), __t_); }
484
485 scoped_lock(scoped_lock const&) = delete;
486 scoped_lock& operator=(scoped_lock const&) = delete;
487
488private:
489 template <size_t... _Indx>
490 _LIBCPP_HIDE_FROM_ABI static void __unlock_unpack(index_sequence<_Indx...>, _MutexTuple& __mt) {
491 (std::get<_Indx>(__mt).unlock(), ...);
492 }
493
494 _MutexTuple __t_;
495};
496_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scoped_lock);
497
498# endif // _LIBCPP_STD_VER >= 17
499
500_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
501_LIBCPP_END_NAMESPACE_STD
502
503# endif // _LIBCPP_HAS_THREADS
504
505_LIBCPP_POP_MACROS
506
507# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 23
508# include <typeinfo>
509# endif
510
511# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 20
512# include <atomic>
513# include <concepts>
514# include <cstdlib>
515# include <cstring>
516# include <ctime>
517# include <initializer_list>
518# include <iosfwd>
519# include <new>
520# include <optional>
521# include <stdexcept>
522# include <system_error>
523# include <type_traits>
524# endif
525#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
526
527#endif // _LIBCPP_MUTEX
528