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