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