xref: /freebsd-12.1/contrib/libc++/include/mutex (revision 540d2a8b)
1// -*- C++ -*-
2//===--------------------------- mutex ------------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MUTEX
12#define _LIBCPP_MUTEX
13
14/*
15    mutex synopsis
16
17namespace std
18{
19
20class mutex
21{
22public:
23     constexpr mutex() noexcept;
24     ~mutex();
25
26    mutex(const mutex&) = delete;
27    mutex& operator=(const mutex&) = delete;
28
29    void lock();
30    bool try_lock();
31    void unlock();
32
33    typedef pthread_mutex_t* native_handle_type;
34    native_handle_type native_handle();
35};
36
37class recursive_mutex
38{
39public:
40     recursive_mutex();
41     ~recursive_mutex();
42
43    recursive_mutex(const recursive_mutex&) = delete;
44    recursive_mutex& operator=(const recursive_mutex&) = delete;
45
46    void lock();
47    bool try_lock() noexcept;
48    void unlock();
49
50    typedef pthread_mutex_t* native_handle_type;
51    native_handle_type native_handle();
52};
53
54class timed_mutex
55{
56public:
57     timed_mutex();
58     ~timed_mutex();
59
60    timed_mutex(const timed_mutex&) = delete;
61    timed_mutex& operator=(const timed_mutex&) = delete;
62
63    void lock();
64    bool try_lock();
65    template <class Rep, class Period>
66        bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
67    template <class Clock, class Duration>
68        bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
69    void unlock();
70};
71
72class recursive_timed_mutex
73{
74public:
75     recursive_timed_mutex();
76     ~recursive_timed_mutex();
77
78    recursive_timed_mutex(const recursive_timed_mutex&) = delete;
79    recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
80
81    void lock();
82    bool try_lock() noexcept;
83    template <class Rep, class Period>
84        bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
85    template <class Clock, class Duration>
86        bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
87    void unlock();
88};
89
90struct defer_lock_t {};
91struct try_to_lock_t {};
92struct adopt_lock_t {};
93
94constexpr defer_lock_t  defer_lock{};
95constexpr try_to_lock_t try_to_lock{};
96constexpr adopt_lock_t  adopt_lock{};
97
98template <class Mutex>
99class lock_guard
100{
101public:
102    typedef Mutex mutex_type;
103
104    explicit lock_guard(mutex_type& m);
105    lock_guard(mutex_type& m, adopt_lock_t);
106    ~lock_guard();
107
108    lock_guard(lock_guard const&) = delete;
109    lock_guard& operator=(lock_guard const&) = delete;
110};
111
112template <class... MutexTypes>
113class scoped_lock // C++17
114{
115public:
116    using mutex_type = Mutex;  // If MutexTypes... consists of the single type Mutex
117
118    explicit scoped_lock(MutexTypes&... m);
119    scoped_lock(MutexTypes&... m, adopt_lock_t);
120    ~scoped_lock();
121    scoped_lock(scoped_lock const&) = delete;
122    scoped_lock& operator=(scoped_lock const&) = delete;
123private:
124    tuple<MutexTypes&...> pm; // exposition only
125};
126
127template <class Mutex>
128class unique_lock
129{
130public:
131    typedef Mutex mutex_type;
132    unique_lock() noexcept;
133    explicit unique_lock(mutex_type& m);
134    unique_lock(mutex_type& m, defer_lock_t) noexcept;
135    unique_lock(mutex_type& m, try_to_lock_t);
136    unique_lock(mutex_type& m, adopt_lock_t);
137    template <class Clock, class Duration>
138        unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
139    template <class Rep, class Period>
140        unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
141    ~unique_lock();
142
143    unique_lock(unique_lock const&) = delete;
144    unique_lock& operator=(unique_lock const&) = delete;
145
146    unique_lock(unique_lock&& u) noexcept;
147    unique_lock& operator=(unique_lock&& u) noexcept;
148
149    void lock();
150    bool try_lock();
151
152    template <class Rep, class Period>
153        bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
154    template <class Clock, class Duration>
155        bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
156
157    void unlock();
158
159    void swap(unique_lock& u) noexcept;
160    mutex_type* release() noexcept;
161
162    bool owns_lock() const noexcept;
163    explicit operator bool () const noexcept;
164    mutex_type* mutex() const noexcept;
165};
166
167template <class Mutex>
168  void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
169
170template <class L1, class L2, class... L3>
171  int try_lock(L1&, L2&, L3&...);
172template <class L1, class L2, class... L3>
173  void lock(L1&, L2&, L3&...);
174
175struct once_flag
176{
177    constexpr once_flag() noexcept;
178
179    once_flag(const once_flag&) = delete;
180    once_flag& operator=(const once_flag&) = delete;
181};
182
183template<class Callable, class ...Args>
184  void call_once(once_flag& flag, Callable&& func, Args&&... args);
185
186}  // std
187
188*/
189
190#include <__config>
191#include <__mutex_base>
192#include <functional>
193#include <memory>
194#ifndef _LIBCPP_CXX03_LANG
195#include <tuple>
196#endif
197#include <__threading_support>
198
199#include <__undef_min_max>
200
201#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
202#pragma GCC system_header
203#endif
204
205_LIBCPP_BEGIN_NAMESPACE_STD
206
207#ifndef _LIBCPP_HAS_NO_THREADS
208
209class _LIBCPP_TYPE_VIS recursive_mutex
210{
211    __libcpp_recursive_mutex_t __m_;
212
213public:
214     recursive_mutex();
215     ~recursive_mutex();
216
217private:
218    recursive_mutex(const recursive_mutex&); // = delete;
219    recursive_mutex& operator=(const recursive_mutex&); // = delete;
220
221public:
222    void lock();
223    bool try_lock() _NOEXCEPT;
224    void unlock()  _NOEXCEPT;
225
226    typedef __libcpp_recursive_mutex_t* native_handle_type;
227
228    _LIBCPP_INLINE_VISIBILITY
229    native_handle_type native_handle() {return &__m_;}
230};
231
232class _LIBCPP_TYPE_VIS timed_mutex
233{
234    mutex              __m_;
235    condition_variable __cv_;
236    bool               __locked_;
237public:
238     timed_mutex();
239     ~timed_mutex();
240
241private:
242    timed_mutex(const timed_mutex&); // = delete;
243    timed_mutex& operator=(const timed_mutex&); // = delete;
244
245public:
246    void lock();
247    bool try_lock() _NOEXCEPT;
248    template <class _Rep, class _Period>
249        _LIBCPP_INLINE_VISIBILITY
250        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
251            {return try_lock_until(chrono::steady_clock::now() + __d);}
252    template <class _Clock, class _Duration>
253        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
254        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
255    void unlock() _NOEXCEPT;
256};
257
258template <class _Clock, class _Duration>
259bool
260timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
261{
262    using namespace chrono;
263    unique_lock<mutex> __lk(__m_);
264    bool no_timeout = _Clock::now() < __t;
265    while (no_timeout && __locked_)
266        no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
267    if (!__locked_)
268    {
269        __locked_ = true;
270        return true;
271    }
272    return false;
273}
274
275class _LIBCPP_TYPE_VIS recursive_timed_mutex
276{
277    mutex              __m_;
278    condition_variable __cv_;
279    size_t             __count_;
280    __libcpp_thread_id __id_;
281public:
282     recursive_timed_mutex();
283     ~recursive_timed_mutex();
284
285private:
286    recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
287    recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
288
289public:
290    void lock();
291    bool try_lock() _NOEXCEPT;
292    template <class _Rep, class _Period>
293        _LIBCPP_INLINE_VISIBILITY
294        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
295            {return try_lock_until(chrono::steady_clock::now() + __d);}
296    template <class _Clock, class _Duration>
297        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
298        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
299    void unlock() _NOEXCEPT;
300};
301
302template <class _Clock, class _Duration>
303bool
304recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
305{
306    using namespace chrono;
307    __libcpp_thread_id __id = __libcpp_thread_get_current_id();
308    unique_lock<mutex> lk(__m_);
309    if (__libcpp_thread_id_equal(__id, __id_))
310    {
311        if (__count_ == numeric_limits<size_t>::max())
312            return false;
313        ++__count_;
314        return true;
315    }
316    bool no_timeout = _Clock::now() < __t;
317    while (no_timeout && __count_ != 0)
318        no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
319    if (__count_ == 0)
320    {
321        __count_ = 1;
322        __id_ = __id;
323        return true;
324    }
325    return false;
326}
327
328template <class _L0, class _L1>
329int
330try_lock(_L0& __l0, _L1& __l1)
331{
332    unique_lock<_L0> __u0(__l0, try_to_lock);
333    if (__u0.owns_lock())
334    {
335        if (__l1.try_lock())
336        {
337            __u0.release();
338            return -1;
339        }
340        else
341            return 1;
342    }
343    return 0;
344}
345
346#ifndef _LIBCPP_CXX03_LANG
347
348template <class _L0, class _L1, class _L2, class... _L3>
349int
350try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
351{
352    int __r = 0;
353    unique_lock<_L0> __u0(__l0, try_to_lock);
354    if (__u0.owns_lock())
355    {
356        __r = try_lock(__l1, __l2, __l3...);
357        if (__r == -1)
358            __u0.release();
359        else
360            ++__r;
361    }
362    return __r;
363}
364
365#endif  // _LIBCPP_CXX03_LANG
366
367template <class _L0, class _L1>
368void
369lock(_L0& __l0, _L1& __l1)
370{
371    while (true)
372    {
373        {
374            unique_lock<_L0> __u0(__l0);
375            if (__l1.try_lock())
376            {
377                __u0.release();
378                break;
379            }
380        }
381        __libcpp_thread_yield();
382        {
383            unique_lock<_L1> __u1(__l1);
384            if (__l0.try_lock())
385            {
386                __u1.release();
387                break;
388            }
389        }
390        __libcpp_thread_yield();
391    }
392}
393
394#ifndef _LIBCPP_CXX03_LANG
395
396template <class _L0, class _L1, class _L2, class ..._L3>
397void
398__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
399{
400    while (true)
401    {
402        switch (__i)
403        {
404        case 0:
405            {
406                unique_lock<_L0> __u0(__l0);
407                __i = try_lock(__l1, __l2, __l3...);
408                if (__i == -1)
409                {
410                    __u0.release();
411                    return;
412                }
413            }
414            ++__i;
415            __libcpp_thread_yield();
416            break;
417        case 1:
418            {
419                unique_lock<_L1> __u1(__l1);
420                __i = try_lock(__l2, __l3..., __l0);
421                if (__i == -1)
422                {
423                    __u1.release();
424                    return;
425                }
426            }
427            if (__i == sizeof...(_L3) + 1)
428                __i = 0;
429            else
430                __i += 2;
431            __libcpp_thread_yield();
432            break;
433        default:
434            __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
435            return;
436        }
437    }
438}
439
440template <class _L0, class _L1, class _L2, class ..._L3>
441inline _LIBCPP_INLINE_VISIBILITY
442void
443lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
444{
445    __lock_first(0, __l0, __l1, __l2, __l3...);
446}
447
448template <class _L0>
449inline _LIBCPP_INLINE_VISIBILITY
450void __unlock(_L0& __l0) {
451    __l0.unlock();
452}
453
454template <class _L0, class _L1>
455inline _LIBCPP_INLINE_VISIBILITY
456void __unlock(_L0& __l0, _L1& __l1) {
457    __l0.unlock();
458    __l1.unlock();
459}
460
461template <class _L0, class _L1, class _L2, class ..._L3>
462inline _LIBCPP_INLINE_VISIBILITY
463void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
464    __l0.unlock();
465    __l1.unlock();
466    _VSTD::__unlock(__l2, __l3...);
467}
468
469#endif  // _LIBCPP_CXX03_LANG
470
471#if _LIBCPP_STD_VER > 14
472template <class ..._Mutexes>
473class _LIBCPP_TEMPLATE_VIS scoped_lock;
474
475template <>
476class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
477public:
478    explicit scoped_lock() {}
479    ~scoped_lock() = default;
480
481    _LIBCPP_INLINE_VISIBILITY
482    explicit scoped_lock(adopt_lock_t) {}
483
484    scoped_lock(scoped_lock const&) = delete;
485    scoped_lock& operator=(scoped_lock const&) = delete;
486};
487
488template <class _Mutex>
489class _LIBCPP_TEMPLATE_VIS scoped_lock<_Mutex> {
490public:
491    typedef _Mutex  mutex_type;
492private:
493    mutex_type& __m_;
494public:
495    explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
496        : __m_(__m) {__m_.lock();}
497
498    ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
499
500    _LIBCPP_INLINE_VISIBILITY
501    explicit scoped_lock(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
502        : __m_(__m) {}
503
504
505    scoped_lock(scoped_lock const&) = delete;
506    scoped_lock& operator=(scoped_lock const&) = delete;
507};
508
509template <class ..._MArgs>
510class _LIBCPP_TEMPLATE_VIS scoped_lock
511{
512    static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
513    typedef tuple<_MArgs&...> _MutexTuple;
514
515public:
516    _LIBCPP_INLINE_VISIBILITY
517    explicit scoped_lock(_MArgs&... __margs)
518      : __t_(__margs...)
519    {
520        _VSTD::lock(__margs...);
521    }
522
523    _LIBCPP_INLINE_VISIBILITY
524    scoped_lock(_MArgs&... __margs, adopt_lock_t)
525        : __t_(__margs...)
526    {
527    }
528
529    _LIBCPP_INLINE_VISIBILITY
530    ~scoped_lock() {
531        typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
532        __unlock_unpack(_Indices{}, __t_);
533    }
534
535    scoped_lock(scoped_lock const&) = delete;
536    scoped_lock& operator=(scoped_lock const&) = delete;
537
538private:
539    template <size_t ..._Indx>
540    _LIBCPP_INLINE_VISIBILITY
541    static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
542        _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
543    }
544
545    _MutexTuple __t_;
546};
547
548#endif // _LIBCPP_STD_VER > 14
549#endif // !_LIBCPP_HAS_NO_THREADS
550
551struct _LIBCPP_TEMPLATE_VIS once_flag;
552
553#ifndef _LIBCPP_CXX03_LANG
554
555template<class _Callable, class... _Args>
556_LIBCPP_INLINE_VISIBILITY
557void call_once(once_flag&, _Callable&&, _Args&&...);
558
559#else  // _LIBCPP_CXX03_LANG
560
561template<class _Callable>
562_LIBCPP_INLINE_VISIBILITY
563void call_once(once_flag&, _Callable&);
564
565template<class _Callable>
566_LIBCPP_INLINE_VISIBILITY
567void call_once(once_flag&, const _Callable&);
568
569#endif  // _LIBCPP_CXX03_LANG
570
571struct _LIBCPP_TEMPLATE_VIS once_flag
572{
573    _LIBCPP_INLINE_VISIBILITY
574    _LIBCPP_CONSTEXPR
575        once_flag() _NOEXCEPT : __state_(0) {}
576
577private:
578    once_flag(const once_flag&); // = delete;
579    once_flag& operator=(const once_flag&); // = delete;
580
581    unsigned long __state_;
582
583#ifndef _LIBCPP_CXX03_LANG
584    template<class _Callable, class... _Args>
585    friend
586    void call_once(once_flag&, _Callable&&, _Args&&...);
587#else  // _LIBCPP_CXX03_LANG
588    template<class _Callable>
589    friend
590    void call_once(once_flag&, _Callable&);
591
592    template<class _Callable>
593    friend
594    void call_once(once_flag&, const _Callable&);
595#endif  // _LIBCPP_CXX03_LANG
596};
597
598#ifndef _LIBCPP_CXX03_LANG
599
600template <class _Fp>
601class __call_once_param
602{
603    _Fp& __f_;
604public:
605    _LIBCPP_INLINE_VISIBILITY
606    explicit __call_once_param(_Fp& __f) : __f_(__f) {}
607
608    _LIBCPP_INLINE_VISIBILITY
609    void operator()()
610    {
611        typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
612        __execute(_Index());
613    }
614
615private:
616    template <size_t ..._Indices>
617    _LIBCPP_INLINE_VISIBILITY
618    void __execute(__tuple_indices<_Indices...>)
619    {
620        __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
621    }
622};
623
624#else
625
626template <class _Fp>
627class __call_once_param
628{
629    _Fp& __f_;
630public:
631    _LIBCPP_INLINE_VISIBILITY
632    explicit __call_once_param(_Fp& __f) : __f_(__f) {}
633
634    _LIBCPP_INLINE_VISIBILITY
635    void operator()()
636    {
637        __f_();
638    }
639};
640
641#endif
642
643template <class _Fp>
644void
645__call_once_proxy(void* __vp)
646{
647    __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
648    (*__p)();
649}
650
651_LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
652
653#ifndef _LIBCPP_CXX03_LANG
654
655template<class _Callable, class... _Args>
656inline _LIBCPP_INLINE_VISIBILITY
657void
658call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
659{
660    if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
661    {
662        typedef tuple<_Callable&&, _Args&&...> _Gp;
663        _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
664        __call_once_param<_Gp> __p(__f);
665        __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
666    }
667}
668
669#else  // _LIBCPP_CXX03_LANG
670
671template<class _Callable>
672inline _LIBCPP_INLINE_VISIBILITY
673void
674call_once(once_flag& __flag, _Callable& __func)
675{
676    if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
677    {
678        __call_once_param<_Callable> __p(__func);
679        __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
680    }
681}
682
683template<class _Callable>
684inline _LIBCPP_INLINE_VISIBILITY
685void
686call_once(once_flag& __flag, const _Callable& __func)
687{
688    if (__flag.__state_ != ~0ul)
689    {
690        __call_once_param<const _Callable> __p(__func);
691        __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
692    }
693}
694
695#endif  // _LIBCPP_CXX03_LANG
696
697_LIBCPP_END_NAMESPACE_STD
698
699#endif  // _LIBCPP_MUTEX
700