__mutex_base revision 232950
1227825Stheraven// -*- C++ -*-
2227825Stheraven//===----------------------------------------------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP___MUTEX_BASE
12227825Stheraven#define _LIBCPP___MUTEX_BASE
13227825Stheraven
14227825Stheraven#include <__config>
15227825Stheraven#include <chrono>
16227825Stheraven#include <system_error>
17227825Stheraven#include <pthread.h>
18227825Stheraven
19227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20227825Stheraven#pragma GCC system_header
21227825Stheraven#endif
22227825Stheraven
23227825Stheraven#ifdef _LIBCPP_SHARED_LOCK
24227825Stheraven
25227825Stheravennamespace ting {
26227825Stheraventemplate <class _Mutex> class shared_lock;
27227825Stheraventemplate <class _Mutex> class upgrade_lock;
28227825Stheraven}
29227825Stheraven
30227825Stheraven#endif  // _LIBCPP_SHARED_LOCK
31227825Stheraven
32227825Stheraven
33227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
34227825Stheraven
35227825Stheravenclass _LIBCPP_VISIBLE mutex
36227825Stheraven{
37227825Stheraven    pthread_mutex_t __m_;
38227825Stheraven
39227825Stheravenpublic:
40227825Stheraven    _LIBCPP_INLINE_VISIBILITY
41227825Stheraven     mutex() {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;}
42227825Stheraven     ~mutex();
43227825Stheraven
44227825Stheravenprivate:
45227825Stheraven    mutex(const mutex&);// = delete;
46227825Stheraven    mutex& operator=(const mutex&);// = delete;
47227825Stheraven
48227825Stheravenpublic:
49227825Stheraven    void lock();
50227825Stheraven    bool try_lock();
51227825Stheraven    void unlock();
52227825Stheraven
53227825Stheraven    typedef pthread_mutex_t* native_handle_type;
54227825Stheraven    _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
55227825Stheraven};
56227825Stheraven
57227825Stheravenstruct _LIBCPP_VISIBLE defer_lock_t {};
58227825Stheravenstruct _LIBCPP_VISIBLE try_to_lock_t {};
59227825Stheravenstruct _LIBCPP_VISIBLE adopt_lock_t {};
60227825Stheraven
61227825Stheraven//constexpr
62227825Stheravenextern const
63227825Stheravendefer_lock_t  defer_lock;
64227825Stheraven
65227825Stheraven//constexpr
66227825Stheravenextern const
67227825Stheraventry_to_lock_t try_to_lock;
68227825Stheraven
69227825Stheraven//constexpr
70227825Stheravenextern const
71227825Stheravenadopt_lock_t  adopt_lock;
72227825Stheraven
73227825Stheraventemplate <class _Mutex>
74227825Stheravenclass _LIBCPP_VISIBLE lock_guard
75227825Stheraven{
76227825Stheravenpublic:
77227825Stheraven    typedef _Mutex mutex_type;
78227825Stheraven
79227825Stheravenprivate:
80227825Stheraven    mutex_type& __m_;
81227825Stheravenpublic:
82227825Stheraven
83227825Stheraven    _LIBCPP_INLINE_VISIBILITY
84227825Stheraven    explicit lock_guard(mutex_type& __m)
85227825Stheraven        : __m_(__m) {__m_.lock();}
86227825Stheraven    _LIBCPP_INLINE_VISIBILITY
87227825Stheraven    lock_guard(mutex_type& __m, adopt_lock_t)
88227825Stheraven        : __m_(__m) {}
89227825Stheraven    _LIBCPP_INLINE_VISIBILITY
90227825Stheraven    ~lock_guard() {__m_.unlock();}
91227825Stheraven
92227825Stheravenprivate:
93227825Stheraven    lock_guard(lock_guard const&);// = delete;
94227825Stheraven    lock_guard& operator=(lock_guard const&);// = delete;
95227825Stheraven};
96227825Stheraven
97227825Stheraventemplate <class _Mutex>
98227825Stheravenclass _LIBCPP_VISIBLE unique_lock
99227825Stheraven{
100227825Stheravenpublic:
101227825Stheraven    typedef _Mutex mutex_type;
102227825Stheraven
103227825Stheravenprivate:
104227825Stheraven    mutex_type* __m_;
105227825Stheraven    bool __owns_;
106227825Stheraven
107227825Stheravenpublic:
108227825Stheraven    _LIBCPP_INLINE_VISIBILITY
109227825Stheraven    unique_lock() : __m_(nullptr), __owns_(false) {}
110227825Stheraven    _LIBCPP_INLINE_VISIBILITY
111227825Stheraven    explicit unique_lock(mutex_type& __m)
112227825Stheraven        : __m_(&__m), __owns_(true) {__m_->lock();}
113227825Stheraven    _LIBCPP_INLINE_VISIBILITY
114227825Stheraven    unique_lock(mutex_type& __m, defer_lock_t)
115227825Stheraven        : __m_(&__m), __owns_(false) {}
116227825Stheraven    _LIBCPP_INLINE_VISIBILITY
117227825Stheraven    unique_lock(mutex_type& __m, try_to_lock_t)
118227825Stheraven        : __m_(&__m), __owns_(__m.try_lock()) {}
119227825Stheraven    _LIBCPP_INLINE_VISIBILITY
120227825Stheraven    unique_lock(mutex_type& __m, adopt_lock_t)
121227825Stheraven        : __m_(&__m), __owns_(true) {}
122227825Stheraven    template <class _Clock, class _Duration>
123227825Stheraven    _LIBCPP_INLINE_VISIBILITY
124227825Stheraven        unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
125227825Stheraven            : __m_(&__m), __owns_(__m.try_lock_until(__t)) {}
126227825Stheraven    template <class _Rep, class _Period>
127227825Stheraven    _LIBCPP_INLINE_VISIBILITY
128227825Stheraven        unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
129227825Stheraven            : __m_(&__m), __owns_(__m.try_lock_for(__d)) {}
130227825Stheraven    _LIBCPP_INLINE_VISIBILITY
131227825Stheraven    ~unique_lock()
132227825Stheraven    {
133227825Stheraven        if (__owns_)
134227825Stheraven            __m_->unlock();
135227825Stheraven    }
136227825Stheraven
137227825Stheravenprivate:
138227825Stheraven    unique_lock(unique_lock const&); // = delete;
139227825Stheraven    unique_lock& operator=(unique_lock const&); // = delete;
140227825Stheraven
141227825Stheravenpublic:
142227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
143227825Stheraven    _LIBCPP_INLINE_VISIBILITY
144227825Stheraven    unique_lock(unique_lock&& __u)
145227825Stheraven        : __m_(__u.__m_), __owns_(__u.__owns_)
146227825Stheraven        {__u.__m_ = nullptr; __u.__owns_ = false;}
147227825Stheraven    _LIBCPP_INLINE_VISIBILITY
148227825Stheraven    unique_lock& operator=(unique_lock&& __u)
149227825Stheraven        {
150227825Stheraven            if (__owns_)
151227825Stheraven                __m_->unlock();
152227825Stheraven            __m_ = __u.__m_;
153227825Stheraven            __owns_ = __u.__owns_;
154227825Stheraven            __u.__m_ = nullptr;
155227825Stheraven            __u.__owns_ = false;
156227825Stheraven            return *this;
157227825Stheraven        }
158227825Stheraven
159227825Stheraven#ifdef _LIBCPP_SHARED_LOCK
160227825Stheraven
161227825Stheraven    unique_lock(ting::shared_lock<mutex_type>&&, try_to_lock_t);
162227825Stheraven    template <class _Clock, class _Duration>
163227825Stheraven        unique_lock(ting::shared_lock<mutex_type>&&,
164227825Stheraven                    const chrono::time_point<_Clock, _Duration>&);
165227825Stheraven    template <class _Rep, class _Period>
166227825Stheraven        unique_lock(ting::shared_lock<mutex_type>&&,
167227825Stheraven                    const chrono::duration<_Rep, _Period>&);
168227825Stheraven
169227825Stheraven    explicit unique_lock(ting::upgrade_lock<mutex_type>&&);
170227825Stheraven    unique_lock(ting::upgrade_lock<mutex_type>&&, try_to_lock_t);
171227825Stheraven    template <class _Clock, class _Duration>
172227825Stheraven        unique_lock(ting::upgrade_lock<mutex_type>&&,
173227825Stheraven                    const chrono::time_point<_Clock, _Duration>&);
174227825Stheraven    template <class _Rep, class _Period>
175227825Stheraven        unique_lock(ting::upgrade_lock<mutex_type>&&,
176227825Stheraven                    const chrono::duration<_Rep, _Period>&);
177227825Stheraven
178227825Stheraven#endif  // _LIBCPP_SHARED_LOCK
179227825Stheraven
180227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
181227825Stheraven
182227825Stheraven    void lock();
183227825Stheraven    bool try_lock();
184227825Stheraven
185227825Stheraven    template <class _Rep, class _Period>
186227825Stheraven        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
187227825Stheraven    template <class _Clock, class _Duration>
188227825Stheraven        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
189227825Stheraven
190227825Stheraven    void unlock();
191227825Stheraven
192227825Stheraven    _LIBCPP_INLINE_VISIBILITY
193227825Stheraven    void swap(unique_lock& __u)
194227825Stheraven    {
195227825Stheraven        _VSTD::swap(__m_, __u.__m_);
196227825Stheraven        _VSTD::swap(__owns_, __u.__owns_);
197227825Stheraven    }
198227825Stheraven    _LIBCPP_INLINE_VISIBILITY
199227825Stheraven    mutex_type* release()
200227825Stheraven    {
201227825Stheraven        mutex_type* __m = __m_;
202227825Stheraven        __m_ = nullptr;
203227825Stheraven        __owns_ = false;
204227825Stheraven        return __m;
205227825Stheraven    }
206227825Stheraven
207227825Stheraven    _LIBCPP_INLINE_VISIBILITY
208227825Stheraven    bool owns_lock() const {return __owns_;}
209227825Stheraven    _LIBCPP_INLINE_VISIBILITY
210232950Stheraven    _LIBCPP_EXPLICIT
211227825Stheraven        operator bool () const {return __owns_;}
212227825Stheraven    _LIBCPP_INLINE_VISIBILITY
213227825Stheraven    mutex_type* mutex() const {return __m_;}
214227825Stheraven};
215227825Stheraven
216227825Stheraventemplate <class _Mutex>
217227825Stheravenvoid
218227825Stheravenunique_lock<_Mutex>::lock()
219227825Stheraven{
220227825Stheraven    if (__m_ == nullptr)
221227825Stheraven        __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
222227825Stheraven    if (__owns_)
223227825Stheraven        __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
224227825Stheraven    __m_->lock();
225227825Stheraven    __owns_ = true;
226227825Stheraven}
227227825Stheraven
228227825Stheraventemplate <class _Mutex>
229227825Stheravenbool
230227825Stheravenunique_lock<_Mutex>::try_lock()
231227825Stheraven{
232227825Stheraven    if (__m_ == nullptr)
233227825Stheraven        __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
234227825Stheraven    if (__owns_)
235227825Stheraven        __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
236227825Stheraven    __owns_ = __m_->try_lock();
237227825Stheraven    return __owns_;
238227825Stheraven}
239227825Stheraven
240227825Stheraventemplate <class _Mutex>
241227825Stheraventemplate <class _Rep, class _Period>
242227825Stheravenbool
243227825Stheravenunique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
244227825Stheraven{
245227825Stheraven    if (__m_ == nullptr)
246227825Stheraven        __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
247227825Stheraven    if (__owns_)
248227825Stheraven        __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
249227825Stheraven    __owns_ = __m_->try_lock_for(__d);
250227825Stheraven    return __owns_;
251227825Stheraven}
252227825Stheraven
253227825Stheraventemplate <class _Mutex>
254227825Stheraventemplate <class _Clock, class _Duration>
255227825Stheravenbool
256227825Stheravenunique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
257227825Stheraven{
258227825Stheraven    if (__m_ == nullptr)
259227825Stheraven        __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
260227825Stheraven    if (__owns_)
261227825Stheraven        __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
262227825Stheraven    __owns_ = __m_->try_lock_until(__t);
263227825Stheraven    return __owns_;
264227825Stheraven}
265227825Stheraven
266227825Stheraventemplate <class _Mutex>
267227825Stheravenvoid
268227825Stheravenunique_lock<_Mutex>::unlock()
269227825Stheraven{
270227825Stheraven    if (!__owns_)
271227825Stheraven        __throw_system_error(EPERM, "unique_lock::unlock: not locked");
272227825Stheraven    __m_->unlock();
273227825Stheraven    __owns_ = false;
274227825Stheraven}
275227825Stheraven
276227825Stheraventemplate <class _Mutex>
277227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
278227825Stheravenvoid
279227825Stheravenswap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) {__x.swap(__y);}
280227825Stheraven
281227825Stheravenstruct _LIBCPP_VISIBLE cv_status
282227825Stheraven{
283227825Stheraven    enum _ {
284227825Stheraven        no_timeout,
285227825Stheraven        timeout
286227825Stheraven    };
287227825Stheraven
288227825Stheraven    _ __v_;
289227825Stheraven
290227825Stheraven    _LIBCPP_INLINE_VISIBILITY cv_status(_ __v) : __v_(__v) {}
291227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;}
292227825Stheraven
293227825Stheraven};
294227825Stheraven
295227825Stheravenclass _LIBCPP_VISIBLE condition_variable
296227825Stheraven{
297227825Stheraven    pthread_cond_t __cv_;
298227825Stheravenpublic:
299227825Stheraven    _LIBCPP_INLINE_VISIBILITY
300227825Stheraven    condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;}
301227825Stheraven    ~condition_variable();
302227825Stheraven
303227825Stheravenprivate:
304227825Stheraven    condition_variable(const condition_variable&); // = delete;
305227825Stheraven    condition_variable& operator=(const condition_variable&); // = delete;
306227825Stheraven
307227825Stheravenpublic:
308227825Stheraven    void notify_one();
309227825Stheraven    void notify_all();
310227825Stheraven
311227825Stheraven    void wait(unique_lock<mutex>& __lk);
312227825Stheraven    template <class _Predicate>
313227825Stheraven        void wait(unique_lock<mutex>& __lk, _Predicate __pred);
314227825Stheraven
315227825Stheraven    template <class _Duration>
316227825Stheraven        cv_status
317227825Stheraven        wait_until(unique_lock<mutex>& __lk,
318227825Stheraven                   const chrono::time_point<chrono::system_clock, _Duration>& __t);
319227825Stheraven
320227825Stheraven    template <class _Clock, class _Duration>
321227825Stheraven        cv_status
322227825Stheraven        wait_until(unique_lock<mutex>& __lk,
323227825Stheraven                   const chrono::time_point<_Clock, _Duration>& __t);
324227825Stheraven
325227825Stheraven    template <class _Clock, class _Duration, class _Predicate>
326227825Stheraven        bool
327227825Stheraven        wait_until(unique_lock<mutex>& __lk,
328227825Stheraven                   const chrono::time_point<_Clock, _Duration>& __t,
329227825Stheraven                   _Predicate __pred);
330227825Stheraven
331227825Stheraven    template <class _Rep, class _Period>
332227825Stheraven        cv_status
333227825Stheraven        wait_for(unique_lock<mutex>& __lk,
334227825Stheraven                 const chrono::duration<_Rep, _Period>& __d);
335227825Stheraven
336227825Stheraven    template <class _Rep, class _Period, class _Predicate>
337227825Stheraven        bool
338227825Stheraven        wait_for(unique_lock<mutex>& __lk,
339227825Stheraven                 const chrono::duration<_Rep, _Period>& __d,
340227825Stheraven                 _Predicate __pred);
341227825Stheraven
342227825Stheraven    typedef pthread_cond_t* native_handle_type;
343227825Stheraven    _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}
344227825Stheraven
345227825Stheravenprivate:
346227825Stheraven    void __do_timed_wait(unique_lock<mutex>& __lk,
347227825Stheraven                 chrono::time_point<chrono::system_clock, chrono::nanoseconds>);
348227825Stheraven};
349227825Stheraven
350227825Stheraventemplate <class _To, class _Rep, class _Period>
351227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
352227825Stheraventypename enable_if
353227825Stheraven<
354227825Stheraven    chrono::__is_duration<_To>::value,
355227825Stheraven    _To
356227825Stheraven>::type
357227825Stheraven__ceil(chrono::duration<_Rep, _Period> __d)
358227825Stheraven{
359227825Stheraven    using namespace chrono;
360227825Stheraven    _To __r = duration_cast<_To>(__d);
361227825Stheraven    if (__r < __d)
362227825Stheraven        ++__r;
363227825Stheraven    return __r;
364227825Stheraven}
365227825Stheraven
366227825Stheraventemplate <class _Predicate>
367227825Stheravenvoid
368227825Stheravencondition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
369227825Stheraven{
370227825Stheraven    while (!__pred())
371227825Stheraven        wait(__lk);
372227825Stheraven}
373227825Stheraven
374227825Stheraventemplate <class _Duration>
375227825Stheravencv_status
376227825Stheravencondition_variable::wait_until(unique_lock<mutex>& __lk,
377227825Stheraven                 const chrono::time_point<chrono::system_clock, _Duration>& __t)
378227825Stheraven{
379227825Stheraven    using namespace chrono;
380227825Stheraven    typedef time_point<system_clock, nanoseconds> __nano_sys_tmpt;
381227825Stheraven    __do_timed_wait(__lk,
382227825Stheraven                  __nano_sys_tmpt(__ceil<nanoseconds>(__t.time_since_epoch())));
383227825Stheraven    return system_clock::now() < __t ? cv_status::no_timeout :
384227825Stheraven                                       cv_status::timeout;
385227825Stheraven}
386227825Stheraven
387227825Stheraventemplate <class _Clock, class _Duration>
388227825Stheravencv_status
389227825Stheravencondition_variable::wait_until(unique_lock<mutex>& __lk,
390227825Stheraven                               const chrono::time_point<_Clock, _Duration>& __t)
391227825Stheraven{
392227825Stheraven    using namespace chrono;
393227825Stheraven    system_clock::time_point     __s_now = system_clock::now();
394227825Stheraven    typename _Clock::time_point  __c_now = _Clock::now();
395227825Stheraven    __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__t - __c_now));
396227825Stheraven    return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
397227825Stheraven}
398227825Stheraven
399227825Stheraventemplate <class _Clock, class _Duration, class _Predicate>
400227825Stheravenbool
401227825Stheravencondition_variable::wait_until(unique_lock<mutex>& __lk,
402227825Stheraven                   const chrono::time_point<_Clock, _Duration>& __t,
403227825Stheraven                   _Predicate __pred)
404227825Stheraven{
405227825Stheraven    while (!__pred())
406227825Stheraven    {
407227825Stheraven        if (wait_until(__lk, __t) == cv_status::timeout)
408227825Stheraven            return __pred();
409227825Stheraven    }
410227825Stheraven    return true;
411227825Stheraven}
412227825Stheraven
413227825Stheraventemplate <class _Rep, class _Period>
414227825Stheravencv_status
415227825Stheravencondition_variable::wait_for(unique_lock<mutex>& __lk,
416227825Stheraven                             const chrono::duration<_Rep, _Period>& __d)
417227825Stheraven{
418227825Stheraven    using namespace chrono;
419227825Stheraven    system_clock::time_point __s_now = system_clock::now();
420227825Stheraven    steady_clock::time_point __c_now = steady_clock::now();
421227825Stheraven    __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d));
422227825Stheraven    return steady_clock::now() - __c_now < __d ? cv_status::no_timeout :
423227825Stheraven                                                 cv_status::timeout;
424227825Stheraven}
425227825Stheraven
426227825Stheraventemplate <class _Rep, class _Period, class _Predicate>
427227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
428227825Stheravenbool
429227825Stheravencondition_variable::wait_for(unique_lock<mutex>& __lk,
430227825Stheraven                             const chrono::duration<_Rep, _Period>& __d,
431227825Stheraven                             _Predicate __pred)
432227825Stheraven{
433227825Stheraven    return wait_until(__lk, chrono::steady_clock::now() + __d,
434227825Stheraven                      _VSTD::move(__pred));
435227825Stheraven}
436227825Stheraven
437227825Stheraven_LIBCPP_END_NAMESPACE_STD
438227825Stheraven
439227825Stheraven#endif  // _LIBCPP___MUTEX_BASE
440