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#include <__assert> // all public C++ headers provide the assertion handler
190#include <__chrono/steady_clock.h>
191#include <__chrono/time_point.h>
192#include <__condition_variable/condition_variable.h>
193#include <__config>
194#include <__memory/shared_ptr.h>
195#include <__mutex/lock_guard.h>
196#include <__mutex/mutex.h>
197#include <__mutex/tag_types.h>
198#include <__mutex/unique_lock.h>
199#include <__thread/id.h>
200#include <__threading_support>
201#include <__utility/forward.h>
202#include <cstdint>
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
217_LIBCPP_BEGIN_NAMESPACE_STD
218
219#ifndef _LIBCPP_HAS_NO_THREADS
220
221class _LIBCPP_EXPORTED_FROM_ABI recursive_mutex
222{
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    bool try_lock() _NOEXCEPT;
234    void unlock()  _NOEXCEPT;
235
236    typedef __libcpp_recursive_mutex_t* native_handle_type;
237
238    _LIBCPP_INLINE_VISIBILITY
239    native_handle_type native_handle() {return &__m_;}
240};
241
242class _LIBCPP_EXPORTED_FROM_ABI timed_mutex
243{
244    mutex              __m_;
245    condition_variable __cv_;
246    bool               __locked_;
247public:
248     timed_mutex();
249     ~timed_mutex();
250
251    timed_mutex(const timed_mutex&) = delete;
252    timed_mutex& operator=(const timed_mutex&) = delete;
253
254public:
255    void lock();
256    bool try_lock() _NOEXCEPT;
257    template <class _Rep, class _Period>
258        _LIBCPP_INLINE_VISIBILITY
259        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
260            {return try_lock_until(chrono::steady_clock::now() + __d);}
261    template <class _Clock, class _Duration>
262        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
263        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
264    void unlock() _NOEXCEPT;
265};
266
267template <class _Clock, class _Duration>
268bool
269timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
270{
271    using namespace chrono;
272    unique_lock<mutex> __lk(__m_);
273    bool __no_timeout = _Clock::now() < __t;
274    while (__no_timeout && __locked_)
275        __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
276    if (!__locked_)
277    {
278        __locked_ = true;
279        return true;
280    }
281    return false;
282}
283
284class _LIBCPP_EXPORTED_FROM_ABI recursive_timed_mutex
285{
286    mutex              __m_;
287    condition_variable __cv_;
288    size_t             __count_;
289    __thread_id        __id_;
290public:
291    recursive_timed_mutex();
292    ~recursive_timed_mutex();
293
294    recursive_timed_mutex(const recursive_timed_mutex&) = delete;
295    recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
296
297    void lock();
298    bool try_lock() _NOEXCEPT;
299    template <class _Rep, class _Period>
300        _LIBCPP_INLINE_VISIBILITY
301        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
302            {return try_lock_until(chrono::steady_clock::now() + __d);}
303    template <class _Clock, class _Duration>
304        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
305        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
306    void unlock() _NOEXCEPT;
307};
308
309template <class _Clock, class _Duration>
310bool
311recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
312{
313    using namespace chrono;
314    __thread_id __id = this_thread::get_id();
315    unique_lock<mutex> __lk(__m_);
316    if (__id == __id_)
317    {
318        if (__count_ == numeric_limits<size_t>::max())
319            return false;
320        ++__count_;
321        return true;
322    }
323    bool __no_timeout = _Clock::now() < __t;
324    while (__no_timeout && __count_ != 0)
325        __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
326    if (__count_ == 0)
327    {
328        __count_ = 1;
329        __id_ = __id;
330        return true;
331    }
332    return false;
333}
334
335template <class _L0, class _L1>
336_LIBCPP_HIDE_FROM_ABI int
337try_lock(_L0& __l0, _L1& __l1)
338{
339    unique_lock<_L0> __u0(__l0, try_to_lock_t());
340    if (__u0.owns_lock())
341    {
342        if (__l1.try_lock())
343        {
344            __u0.release();
345            return -1;
346        }
347        else
348            return 1;
349    }
350    return 0;
351}
352
353#ifndef _LIBCPP_CXX03_LANG
354
355template <class _L0, class _L1, class _L2, class... _L3>
356_LIBCPP_HIDE_FROM_ABI int
357try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
358{
359    int __r = 0;
360    unique_lock<_L0> __u0(__l0, try_to_lock);
361    if (__u0.owns_lock())
362    {
363        __r = std::try_lock(__l1, __l2, __l3...);
364        if (__r == -1)
365            __u0.release();
366        else
367            ++__r;
368    }
369    return __r;
370}
371
372#endif // _LIBCPP_CXX03_LANG
373
374template <class _L0, class _L1>
375_LIBCPP_HIDE_FROM_ABI void
376lock(_L0& __l0, _L1& __l1)
377{
378    while (true)
379    {
380        {
381            unique_lock<_L0> __u0(__l0);
382            if (__l1.try_lock())
383            {
384                __u0.release();
385                break;
386            }
387        }
388        __libcpp_thread_yield();
389        {
390            unique_lock<_L1> __u1(__l1);
391            if (__l0.try_lock())
392            {
393                __u1.release();
394                break;
395            }
396        }
397        __libcpp_thread_yield();
398    }
399}
400
401#ifndef _LIBCPP_CXX03_LANG
402
403template <class _L0, class _L1, class _L2, class ..._L3>
404void
405__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
406{
407    while (true)
408    {
409        switch (__i)
410        {
411        case 0:
412            {
413                unique_lock<_L0> __u0(__l0);
414                __i = std::try_lock(__l1, __l2, __l3...);
415                if (__i == -1)
416                {
417                    __u0.release();
418                    return;
419                }
420            }
421            ++__i;
422            __libcpp_thread_yield();
423            break;
424        case 1:
425            {
426                unique_lock<_L1> __u1(__l1);
427                __i = std::try_lock(__l2, __l3..., __l0);
428                if (__i == -1)
429                {
430                    __u1.release();
431                    return;
432                }
433            }
434            if (__i == sizeof...(_L3) + 1)
435                __i = 0;
436            else
437                __i += 2;
438            __libcpp_thread_yield();
439            break;
440        default:
441            std::__lock_first(__i - 2, __l2, __l3..., __l0, __l1);
442            return;
443        }
444    }
445}
446
447template <class _L0, class _L1, class _L2, class ..._L3>
448inline _LIBCPP_INLINE_VISIBILITY
449void
450lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
451{
452    std::__lock_first(0, __l0, __l1, __l2, __l3...);
453}
454
455template <class _L0>
456inline _LIBCPP_INLINE_VISIBILITY
457void __unlock(_L0& __l0) {
458    __l0.unlock();
459}
460
461template <class _L0, class _L1>
462inline _LIBCPP_INLINE_VISIBILITY
463void __unlock(_L0& __l0, _L1& __l1) {
464    __l0.unlock();
465    __l1.unlock();
466}
467
468template <class _L0, class _L1, class _L2, class ..._L3>
469inline _LIBCPP_INLINE_VISIBILITY
470void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
471    __l0.unlock();
472    __l1.unlock();
473    _VSTD::__unlock(__l2, __l3...);
474}
475
476#endif // _LIBCPP_CXX03_LANG
477
478#if _LIBCPP_STD_VER >= 17
479template <class ..._Mutexes>
480class _LIBCPP_TEMPLATE_VIS scoped_lock;
481
482template <>
483class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
484public:
485    explicit scoped_lock() {}
486    ~scoped_lock() = default;
487
488    _LIBCPP_INLINE_VISIBILITY
489    explicit scoped_lock(adopt_lock_t) {}
490
491    scoped_lock(scoped_lock const&) = delete;
492    scoped_lock& operator=(scoped_lock const&) = delete;
493};
494
495template <class _Mutex>
496class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
497public:
498    typedef _Mutex  mutex_type;
499private:
500    mutex_type& __m_;
501public:
502    explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
503        : __m_(__m) {__m_.lock();}
504
505    ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
506
507    _LIBCPP_INLINE_VISIBILITY
508    explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
509        : __m_(__m) {}
510
511    scoped_lock(scoped_lock const&) = delete;
512    scoped_lock& operator=(scoped_lock const&) = delete;
513};
514
515template <class ..._MArgs>
516class _LIBCPP_TEMPLATE_VIS scoped_lock
517{
518    static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
519    typedef tuple<_MArgs&...> _MutexTuple;
520
521public:
522    _LIBCPP_INLINE_VISIBILITY
523    explicit scoped_lock(_MArgs&... __margs)
524      : __t_(__margs...)
525    {
526        _VSTD::lock(__margs...);
527    }
528
529    _LIBCPP_INLINE_VISIBILITY
530    scoped_lock(adopt_lock_t, _MArgs&... __margs)
531        : __t_(__margs...)
532    {
533    }
534
535    _LIBCPP_INLINE_VISIBILITY
536    ~scoped_lock() {
537        typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
538        __unlock_unpack(_Indices{}, __t_);
539    }
540
541    scoped_lock(scoped_lock const&) = delete;
542    scoped_lock& operator=(scoped_lock const&) = delete;
543
544private:
545    template <size_t ..._Indx>
546    _LIBCPP_INLINE_VISIBILITY
547    static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
548        _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
549    }
550
551    _MutexTuple __t_;
552};
553_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scoped_lock);
554
555#endif // _LIBCPP_STD_VER >= 17
556#endif // !_LIBCPP_HAS_NO_THREADS
557
558struct _LIBCPP_TEMPLATE_VIS once_flag;
559
560#ifndef _LIBCPP_CXX03_LANG
561
562template<class _Callable, class... _Args>
563_LIBCPP_INLINE_VISIBILITY
564void call_once(once_flag&, _Callable&&, _Args&&...);
565
566#else  // _LIBCPP_CXX03_LANG
567
568template<class _Callable>
569_LIBCPP_INLINE_VISIBILITY
570void call_once(once_flag&, _Callable&);
571
572template<class _Callable>
573_LIBCPP_INLINE_VISIBILITY
574void call_once(once_flag&, const _Callable&);
575
576#endif // _LIBCPP_CXX03_LANG
577
578struct _LIBCPP_TEMPLATE_VIS once_flag
579{
580    _LIBCPP_INLINE_VISIBILITY
581    _LIBCPP_CONSTEXPR
582        once_flag() _NOEXCEPT : __state_(0) {}
583    once_flag(const once_flag&) = delete;
584    once_flag& operator=(const once_flag&) = delete;
585
586#if defined(_LIBCPP_ABI_MICROSOFT)
587   typedef uintptr_t _State_type;
588#else
589   typedef unsigned long _State_type;
590#endif
591
592private:
593    _State_type __state_;
594
595#ifndef _LIBCPP_CXX03_LANG
596    template<class _Callable, class... _Args>
597    friend
598    void call_once(once_flag&, _Callable&&, _Args&&...);
599#else  // _LIBCPP_CXX03_LANG
600    template<class _Callable>
601    friend
602    void call_once(once_flag&, _Callable&);
603
604    template<class _Callable>
605    friend
606    void call_once(once_flag&, const _Callable&);
607#endif // _LIBCPP_CXX03_LANG
608};
609
610#ifndef _LIBCPP_CXX03_LANG
611
612template <class _Fp>
613class __call_once_param
614{
615    _Fp& __f_;
616public:
617    _LIBCPP_INLINE_VISIBILITY
618    explicit __call_once_param(_Fp& __f) : __f_(__f) {}
619
620    _LIBCPP_INLINE_VISIBILITY
621    void operator()()
622    {
623        typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
624        __execute(_Index());
625    }
626
627private:
628    template <size_t ..._Indices>
629    _LIBCPP_INLINE_VISIBILITY
630    void __execute(__tuple_indices<_Indices...>)
631    {
632        _VSTD::__invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
633    }
634};
635
636#else
637
638template <class _Fp>
639class __call_once_param
640{
641    _Fp& __f_;
642public:
643    _LIBCPP_INLINE_VISIBILITY
644    explicit __call_once_param(_Fp& __f) : __f_(__f) {}
645
646    _LIBCPP_INLINE_VISIBILITY
647    void operator()()
648    {
649        __f_();
650    }
651};
652
653#endif
654
655template <class _Fp>
656void _LIBCPP_INLINE_VISIBILITY
657__call_once_proxy(void* __vp)
658{
659    __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
660    (*__p)();
661}
662
663_LIBCPP_EXPORTED_FROM_ABI void __call_once(volatile once_flag::_State_type&, void*, void (*)(void*));
664
665#ifndef _LIBCPP_CXX03_LANG
666
667template<class _Callable, class... _Args>
668inline _LIBCPP_INLINE_VISIBILITY
669void
670call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
671{
672    if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
673    {
674        typedef tuple<_Callable&&, _Args&&...> _Gp;
675        _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
676        __call_once_param<_Gp> __p(__f);
677        std::__call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
678    }
679}
680
681#else  // _LIBCPP_CXX03_LANG
682
683template<class _Callable>
684inline _LIBCPP_INLINE_VISIBILITY
685void
686call_once(once_flag& __flag, _Callable& __func)
687{
688    if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
689    {
690        __call_once_param<_Callable> __p(__func);
691        std::__call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
692    }
693}
694
695template<class _Callable>
696inline _LIBCPP_INLINE_VISIBILITY
697void
698call_once(once_flag& __flag, const _Callable& __func)
699{
700    if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
701    {
702        __call_once_param<const _Callable> __p(__func);
703        std::__call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
704    }
705}
706
707#endif // _LIBCPP_CXX03_LANG
708
709_LIBCPP_END_NAMESPACE_STD
710
711_LIBCPP_POP_MACROS
712
713#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
714#  include <atomic>
715#  include <concepts>
716#  include <cstdlib>
717#  include <cstring>
718#  include <ctime>
719#  include <initializer_list>
720#  include <new>
721#  include <stdexcept>
722#  include <system_error>
723#  include <type_traits>
724#  include <typeinfo>
725#endif
726
727#endif // _LIBCPP_MUTEX
728