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_BASE
11#define _LIBCPP___MUTEX_BASE
12
13#include <__chrono/duration.h>
14#include <__chrono/steady_clock.h>
15#include <__chrono/system_clock.h>
16#include <__chrono/time_point.h>
17#include <__config>
18#include <__threading_support>
19#include <ratio>
20#include <system_error>
21#include <time.h>
22
23#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24#  pragma GCC system_header
25#  pragma clang include_instead(<mutex>)
26#  pragma clang include_instead(<shared_mutex>)
27#endif
28
29_LIBCPP_PUSH_MACROS
30#include <__undef_macros>
31
32
33_LIBCPP_BEGIN_NAMESPACE_STD
34
35#ifndef _LIBCPP_HAS_NO_THREADS
36
37class _LIBCPP_TYPE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex
38{
39    __libcpp_mutex_t __m_ = _LIBCPP_MUTEX_INITIALIZER;
40
41public:
42    _LIBCPP_INLINE_VISIBILITY
43    _LIBCPP_CONSTEXPR mutex() = default;
44
45    mutex(const mutex&) = delete;
46    mutex& operator=(const mutex&) = delete;
47
48#if defined(_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION)
49    ~mutex() = default;
50#else
51    ~mutex() _NOEXCEPT;
52#endif
53
54    void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability());
55    bool try_lock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true));
56    void unlock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability());
57
58    typedef __libcpp_mutex_t* native_handle_type;
59    _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
60};
61
62static_assert(is_nothrow_default_constructible<mutex>::value,
63              "the default constructor for std::mutex must be nothrow");
64
65struct _LIBCPP_TYPE_VIS defer_lock_t { explicit defer_lock_t() = default; };
66struct _LIBCPP_TYPE_VIS try_to_lock_t { explicit try_to_lock_t() = default; };
67struct _LIBCPP_TYPE_VIS adopt_lock_t { explicit adopt_lock_t() = default; };
68
69#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
70
71extern _LIBCPP_EXPORTED_FROM_ABI const defer_lock_t  defer_lock;
72extern _LIBCPP_EXPORTED_FROM_ABI const try_to_lock_t try_to_lock;
73extern _LIBCPP_EXPORTED_FROM_ABI const adopt_lock_t  adopt_lock;
74
75#else
76
77/* inline */ constexpr defer_lock_t  defer_lock  = defer_lock_t();
78/* inline */ constexpr try_to_lock_t try_to_lock = try_to_lock_t();
79/* inline */ constexpr adopt_lock_t  adopt_lock  = adopt_lock_t();
80
81#endif
82
83template <class _Mutex>
84class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable)
85lock_guard
86{
87public:
88    typedef _Mutex mutex_type;
89
90private:
91    mutex_type& __m_;
92public:
93
94    _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY
95    explicit lock_guard(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
96        : __m_(__m) {__m_.lock();}
97
98    _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY
99    lock_guard(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
100        : __m_(__m) {}
101    _LIBCPP_INLINE_VISIBILITY
102    ~lock_guard() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
103
104private:
105    lock_guard(lock_guard const&) = delete;
106    lock_guard& operator=(lock_guard const&) = delete;
107};
108
109template <class _Mutex>
110class _LIBCPP_TEMPLATE_VIS unique_lock
111{
112public:
113    typedef _Mutex mutex_type;
114
115private:
116    mutex_type* __m_;
117    bool __owns_;
118
119public:
120    _LIBCPP_INLINE_VISIBILITY
121    unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
122    _LIBCPP_INLINE_VISIBILITY
123    explicit unique_lock(mutex_type& __m)
124        : __m_(_VSTD::addressof(__m)), __owns_(true) {__m_->lock();}
125    _LIBCPP_INLINE_VISIBILITY
126    unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
127        : __m_(_VSTD::addressof(__m)), __owns_(false) {}
128    _LIBCPP_INLINE_VISIBILITY
129    unique_lock(mutex_type& __m, try_to_lock_t)
130        : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock()) {}
131    _LIBCPP_INLINE_VISIBILITY
132    unique_lock(mutex_type& __m, adopt_lock_t)
133        : __m_(_VSTD::addressof(__m)), __owns_(true) {}
134    template <class _Clock, class _Duration>
135    _LIBCPP_INLINE_VISIBILITY
136        unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
137            : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_until(__t)) {}
138    template <class _Rep, class _Period>
139    _LIBCPP_INLINE_VISIBILITY
140        unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
141            : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_for(__d)) {}
142    _LIBCPP_INLINE_VISIBILITY
143    ~unique_lock()
144    {
145        if (__owns_)
146            __m_->unlock();
147    }
148
149    unique_lock(unique_lock const&) = delete;
150    unique_lock& operator=(unique_lock const&) = delete;
151
152    _LIBCPP_INLINE_VISIBILITY
153    unique_lock(unique_lock&& __u) _NOEXCEPT
154        : __m_(__u.__m_), __owns_(__u.__owns_)
155        {__u.__m_ = nullptr; __u.__owns_ = false;}
156    _LIBCPP_INLINE_VISIBILITY
157    unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
158        {
159            if (__owns_)
160                __m_->unlock();
161            __m_ = __u.__m_;
162            __owns_ = __u.__owns_;
163            __u.__m_ = nullptr;
164            __u.__owns_ = false;
165            return *this;
166        }
167
168    void lock();
169    bool try_lock();
170
171    template <class _Rep, class _Period>
172        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
173    template <class _Clock, class _Duration>
174        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
175
176    void unlock();
177
178    _LIBCPP_INLINE_VISIBILITY
179    void swap(unique_lock& __u) _NOEXCEPT
180    {
181        _VSTD::swap(__m_, __u.__m_);
182        _VSTD::swap(__owns_, __u.__owns_);
183    }
184    _LIBCPP_INLINE_VISIBILITY
185    mutex_type* release() _NOEXCEPT
186    {
187        mutex_type* __m = __m_;
188        __m_ = nullptr;
189        __owns_ = false;
190        return __m;
191    }
192
193    _LIBCPP_INLINE_VISIBILITY
194    bool owns_lock() const _NOEXCEPT {return __owns_;}
195    _LIBCPP_INLINE_VISIBILITY
196    explicit operator bool() const _NOEXCEPT {return __owns_;}
197    _LIBCPP_INLINE_VISIBILITY
198    mutex_type* mutex() const _NOEXCEPT {return __m_;}
199};
200
201template <class _Mutex>
202void
203unique_lock<_Mutex>::lock()
204{
205    if (__m_ == nullptr)
206        __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
207    if (__owns_)
208        __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
209    __m_->lock();
210    __owns_ = true;
211}
212
213template <class _Mutex>
214bool
215unique_lock<_Mutex>::try_lock()
216{
217    if (__m_ == nullptr)
218        __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
219    if (__owns_)
220        __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
221    __owns_ = __m_->try_lock();
222    return __owns_;
223}
224
225template <class _Mutex>
226template <class _Rep, class _Period>
227bool
228unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
229{
230    if (__m_ == nullptr)
231        __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
232    if (__owns_)
233        __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
234    __owns_ = __m_->try_lock_for(__d);
235    return __owns_;
236}
237
238template <class _Mutex>
239template <class _Clock, class _Duration>
240bool
241unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
242{
243    if (__m_ == nullptr)
244        __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
245    if (__owns_)
246        __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
247    __owns_ = __m_->try_lock_until(__t);
248    return __owns_;
249}
250
251template <class _Mutex>
252void
253unique_lock<_Mutex>::unlock()
254{
255    if (!__owns_)
256        __throw_system_error(EPERM, "unique_lock::unlock: not locked");
257    __m_->unlock();
258    __owns_ = false;
259}
260
261template <class _Mutex>
262inline _LIBCPP_INLINE_VISIBILITY
263void
264swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
265    {__x.swap(__y);}
266
267//enum class cv_status
268_LIBCPP_DECLARE_STRONG_ENUM(cv_status)
269{
270    no_timeout,
271    timeout
272};
273_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
274
275class _LIBCPP_TYPE_VIS condition_variable
276{
277    __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER;
278public:
279    _LIBCPP_INLINE_VISIBILITY
280    _LIBCPP_CONSTEXPR condition_variable() _NOEXCEPT = default;
281
282#ifdef _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION
283    ~condition_variable() = default;
284#else
285    ~condition_variable();
286#endif
287
288    condition_variable(const condition_variable&) = delete;
289    condition_variable& operator=(const condition_variable&) = delete;
290
291    void notify_one() _NOEXCEPT;
292    void notify_all() _NOEXCEPT;
293
294    void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
295    template <class _Predicate>
296        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
297        void wait(unique_lock<mutex>& __lk, _Predicate __pred);
298
299    template <class _Clock, class _Duration>
300        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
301        cv_status
302        wait_until(unique_lock<mutex>& __lk,
303                   const chrono::time_point<_Clock, _Duration>& __t);
304
305    template <class _Clock, class _Duration, class _Predicate>
306        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
307        bool
308        wait_until(unique_lock<mutex>& __lk,
309                   const chrono::time_point<_Clock, _Duration>& __t,
310                   _Predicate __pred);
311
312    template <class _Rep, class _Period>
313        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
314        cv_status
315        wait_for(unique_lock<mutex>& __lk,
316                 const chrono::duration<_Rep, _Period>& __d);
317
318    template <class _Rep, class _Period, class _Predicate>
319        bool
320        _LIBCPP_INLINE_VISIBILITY
321        wait_for(unique_lock<mutex>& __lk,
322                 const chrono::duration<_Rep, _Period>& __d,
323                 _Predicate __pred);
324
325    typedef __libcpp_condvar_t* native_handle_type;
326    _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}
327
328private:
329    void __do_timed_wait(unique_lock<mutex>& __lk,
330       chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
331#if defined(_LIBCPP_HAS_COND_CLOCKWAIT)
332    void __do_timed_wait(unique_lock<mutex>& __lk,
333       chrono::time_point<chrono::steady_clock, chrono::nanoseconds>) _NOEXCEPT;
334#endif
335    template <class _Clock>
336    void __do_timed_wait(unique_lock<mutex>& __lk,
337       chrono::time_point<_Clock, chrono::nanoseconds>) _NOEXCEPT;
338};
339#endif // !_LIBCPP_HAS_NO_THREADS
340
341template <class _Rep, class _Period>
342inline _LIBCPP_INLINE_VISIBILITY
343typename enable_if
344<
345    is_floating_point<_Rep>::value,
346    chrono::nanoseconds
347>::type
348__safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d)
349{
350    using namespace chrono;
351    using __ratio = ratio_divide<_Period, nano>;
352    using __ns_rep = nanoseconds::rep;
353    _Rep __result_float = __d.count() * __ratio::num / __ratio::den;
354
355    _Rep __result_max = numeric_limits<__ns_rep>::max();
356    if (__result_float >= __result_max) {
357        return nanoseconds::max();
358    }
359
360    _Rep __result_min = numeric_limits<__ns_rep>::min();
361    if (__result_float <= __result_min) {
362        return nanoseconds::min();
363    }
364
365    return nanoseconds(static_cast<__ns_rep>(__result_float));
366}
367
368template <class _Rep, class _Period>
369inline _LIBCPP_INLINE_VISIBILITY
370typename enable_if
371<
372    !is_floating_point<_Rep>::value,
373    chrono::nanoseconds
374>::type
375__safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d)
376{
377    using namespace chrono;
378    if (__d.count() == 0) {
379        return nanoseconds(0);
380    }
381
382    using __ratio = ratio_divide<_Period, nano>;
383    using __ns_rep = nanoseconds::rep;
384    __ns_rep __result_max = numeric_limits<__ns_rep>::max();
385    if (__d.count() > 0 && __d.count() > __result_max / __ratio::num) {
386        return nanoseconds::max();
387    }
388
389    __ns_rep __result_min = numeric_limits<__ns_rep>::min();
390    if (__d.count() < 0 && __d.count() < __result_min / __ratio::num) {
391        return nanoseconds::min();
392    }
393
394    __ns_rep __result = __d.count() * __ratio::num / __ratio::den;
395    if (__result == 0) {
396        return nanoseconds(1);
397    }
398
399    return nanoseconds(__result);
400}
401
402#ifndef _LIBCPP_HAS_NO_THREADS
403template <class _Predicate>
404void
405condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
406{
407    while (!__pred())
408        wait(__lk);
409}
410
411template <class _Clock, class _Duration>
412cv_status
413condition_variable::wait_until(unique_lock<mutex>& __lk,
414                               const chrono::time_point<_Clock, _Duration>& __t)
415{
416    using namespace chrono;
417    using __clock_tp_ns = time_point<_Clock, nanoseconds>;
418
419    typename _Clock::time_point __now = _Clock::now();
420    if (__t <= __now)
421        return cv_status::timeout;
422
423    __clock_tp_ns __t_ns = __clock_tp_ns(_VSTD::__safe_nanosecond_cast(__t.time_since_epoch()));
424
425    __do_timed_wait(__lk, __t_ns);
426    return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
427}
428
429template <class _Clock, class _Duration, class _Predicate>
430bool
431condition_variable::wait_until(unique_lock<mutex>& __lk,
432                   const chrono::time_point<_Clock, _Duration>& __t,
433                   _Predicate __pred)
434{
435    while (!__pred())
436    {
437        if (wait_until(__lk, __t) == cv_status::timeout)
438            return __pred();
439    }
440    return true;
441}
442
443template <class _Rep, class _Period>
444cv_status
445condition_variable::wait_for(unique_lock<mutex>& __lk,
446                             const chrono::duration<_Rep, _Period>& __d)
447{
448    using namespace chrono;
449    if (__d <= __d.zero())
450        return cv_status::timeout;
451    using __ns_rep = nanoseconds::rep;
452    steady_clock::time_point __c_now = steady_clock::now();
453
454#if defined(_LIBCPP_HAS_COND_CLOCKWAIT)
455    using __clock_tp_ns = time_point<steady_clock, nanoseconds>;
456    __ns_rep __now_count_ns = _VSTD::__safe_nanosecond_cast(__c_now.time_since_epoch()).count();
457#else
458    using __clock_tp_ns = time_point<system_clock, nanoseconds>;
459    __ns_rep __now_count_ns = _VSTD::__safe_nanosecond_cast(system_clock::now().time_since_epoch()).count();
460#endif
461
462    __ns_rep __d_ns_count = _VSTD::__safe_nanosecond_cast(__d).count();
463
464    if (__now_count_ns > numeric_limits<__ns_rep>::max() - __d_ns_count) {
465        __do_timed_wait(__lk, __clock_tp_ns::max());
466    } else {
467        __do_timed_wait(__lk, __clock_tp_ns(nanoseconds(__now_count_ns + __d_ns_count)));
468    }
469
470    return steady_clock::now() - __c_now < __d ? cv_status::no_timeout :
471                                                 cv_status::timeout;
472}
473
474template <class _Rep, class _Period, class _Predicate>
475inline
476bool
477condition_variable::wait_for(unique_lock<mutex>& __lk,
478                             const chrono::duration<_Rep, _Period>& __d,
479                             _Predicate __pred)
480{
481    return wait_until(__lk, chrono::steady_clock::now() + __d,
482                      _VSTD::move(__pred));
483}
484
485#if defined(_LIBCPP_HAS_COND_CLOCKWAIT)
486inline
487void
488condition_variable::__do_timed_wait(unique_lock<mutex>& __lk,
489     chrono::time_point<chrono::steady_clock, chrono::nanoseconds> __tp) _NOEXCEPT
490{
491    using namespace chrono;
492    if (!__lk.owns_lock())
493        __throw_system_error(EPERM,
494                            "condition_variable::timed wait: mutex not locked");
495    nanoseconds __d = __tp.time_since_epoch();
496    timespec __ts;
497    seconds __s = duration_cast<seconds>(__d);
498    using __ts_sec = decltype(__ts.tv_sec);
499    const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max();
500    if (__s.count() < __ts_sec_max)
501    {
502        __ts.tv_sec = static_cast<__ts_sec>(__s.count());
503        __ts.tv_nsec = (__d - __s).count();
504    }
505    else
506    {
507        __ts.tv_sec = __ts_sec_max;
508        __ts.tv_nsec = giga::num - 1;
509    }
510    int __ec = pthread_cond_clockwait(&__cv_, __lk.mutex()->native_handle(), CLOCK_MONOTONIC, &__ts);
511    if (__ec != 0 && __ec != ETIMEDOUT)
512        __throw_system_error(__ec, "condition_variable timed_wait failed");
513}
514#endif // _LIBCPP_HAS_COND_CLOCKWAIT
515
516template <class _Clock>
517inline
518void
519condition_variable::__do_timed_wait(unique_lock<mutex>& __lk,
520     chrono::time_point<_Clock, chrono::nanoseconds> __tp) _NOEXCEPT
521{
522    wait_for(__lk, __tp - _Clock::now());
523}
524
525#endif // !_LIBCPP_HAS_NO_THREADS
526
527_LIBCPP_END_NAMESPACE_STD
528
529_LIBCPP_POP_MACROS
530
531#endif // _LIBCPP___MUTEX_BASE
532