mutex revision 278724
11590Srgrimes// -*- C++ -*-
21590Srgrimes//===--------------------------- mutex ------------------------------------===//
31590Srgrimes//
41590Srgrimes//                     The LLVM Compiler Infrastructure
51590Srgrimes//
61590Srgrimes// This file is dual licensed under the MIT and the University of Illinois Open
71590Srgrimes// Source Licenses. See LICENSE.TXT for details.
81590Srgrimes//
91590Srgrimes//===----------------------------------------------------------------------===//
101590Srgrimes
111590Srgrimes#ifndef _LIBCPP_MUTEX
121590Srgrimes#define _LIBCPP_MUTEX
131590Srgrimes
141590Srgrimes/*
151590Srgrimes    mutex synopsis
161590Srgrimes
171590Srgrimesnamespace std
181590Srgrimes{
191590Srgrimes
201590Srgrimesclass mutex
211590Srgrimes{
221590Srgrimespublic:
231590Srgrimes     constexpr mutex() noexcept;
241590Srgrimes     ~mutex();
251590Srgrimes
261590Srgrimes    mutex(const mutex&) = delete;
271590Srgrimes    mutex& operator=(const mutex&) = delete;
281590Srgrimes
291590Srgrimes    void lock();
301590Srgrimes    bool try_lock();
311590Srgrimes    void unlock();
3250477Speter
331590Srgrimes    typedef pthread_mutex_t* native_handle_type;
34286759Spfg    native_handle_type native_handle();
351590Srgrimes};
361590Srgrimes
371590Srgrimesclass recursive_mutex
381590Srgrimes{
3998165Stjrpublic:
401590Srgrimes     recursive_mutex();
4168963Sru     ~recursive_mutex();
42185714Skeramida
4328696Scharnier    recursive_mutex(const recursive_mutex&) = delete;
441590Srgrimes    recursive_mutex& operator=(const recursive_mutex&) = delete;
451590Srgrimes
4628696Scharnier    void lock();
471590Srgrimes    bool try_lock() noexcept;
481590Srgrimes    void unlock();
49142332Strhodes
50142332Strhodes    typedef pthread_mutex_t* native_handle_type;
51142332Strhodes    native_handle_type native_handle();
52142332Strhodes};
53142332Strhodes
54142332Strhodesclass timed_mutex
55142332Strhodes{
56142332Strhodespublic:
57142332Strhodes     timed_mutex();
58142332Strhodes     ~timed_mutex();
59142332Strhodes
601590Srgrimes    timed_mutex(const timed_mutex&) = delete;
611590Srgrimes    timed_mutex& operator=(const timed_mutex&) = delete;
62101670Stjr
631590Srgrimes    void lock();
641590Srgrimes    bool try_lock();
651590Srgrimes    template <class Rep, class Period>
661590Srgrimes        bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
671590Srgrimes    template <class Clock, class Duration>
681590Srgrimes        bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
69165454Sru    void unlock();
70185714Skeramida};
71185714Skeramida
72185714Skeramidaclass recursive_timed_mutex
73286759Spfg{
74185714Skeramidapublic:
75185714Skeramida     recursive_timed_mutex();
76185714Skeramida     ~recursive_timed_mutex();
77185714Skeramida
78185714Skeramida    recursive_timed_mutex(const recursive_timed_mutex&) = delete;
791590Srgrimes    recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
801590Srgrimes
811590Srgrimes    void lock();
82142332Strhodes    bool try_lock() noexcept;
83142544Strhodes    template <class Rep, class Period>
84142332Strhodes        bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
851590Srgrimes    template <class Clock, class Duration>
861590Srgrimes        bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
871590Srgrimes    void unlock();
8898165Stjr};
8998165Stjr
9098165Stjrstruct defer_lock_t {};
9198165Stjrstruct try_to_lock_t {};
9298165Stjrstruct adopt_lock_t {};
9398165Stjr
94142332Strhodesconstexpr defer_lock_t  defer_lock{};
95142332Strhodesconstexpr try_to_lock_t try_to_lock{};
96142332Strhodesconstexpr adopt_lock_t  adopt_lock{};
971590Srgrimes
981590Srgrimestemplate <class Mutex>
991590Srgrimesclass lock_guard
1001590Srgrimes{
1011590Srgrimespublic:
1021590Srgrimes    typedef Mutex mutex_type;
10328696Scharnier
1041590Srgrimes    explicit lock_guard(mutex_type& m);
105142332Strhodes    lock_guard(mutex_type& m, adopt_lock_t);
106142332Strhodes    ~lock_guard();
10798165Stjr
108107276Sru    lock_guard(lock_guard const&) = delete;
10998165Stjr    lock_guard& operator=(lock_guard const&) = delete;
11098165Stjr};
11198165Stjr
1121590Srgrimestemplate <class Mutex>
1131590Srgrimesclass unique_lock
1141590Srgrimes{
115142332Strhodespublic:
116142332Strhodes    typedef Mutex mutex_type;
117142332Strhodes    unique_lock() noexcept;
11898165Stjr    explicit unique_lock(mutex_type& m);
11998165Stjr    unique_lock(mutex_type& m, defer_lock_t) noexcept;
120107276Sru    unique_lock(mutex_type& m, try_to_lock_t);
12198165Stjr    unique_lock(mutex_type& m, adopt_lock_t);
12298165Stjr    template <class Clock, class Duration>
12398165Stjr        unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
12498165Stjr    template <class Rep, class Period>
12598165Stjr        unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
126101671Stjr    ~unique_lock();
127140420Sru
128140420Sru    unique_lock(unique_lock const&) = delete;
12998165Stjr    unique_lock& operator=(unique_lock const&) = delete;
13098165Stjr
13198165Stjr    unique_lock(unique_lock&& u) noexcept;
13298165Stjr    unique_lock& operator=(unique_lock&& u) noexcept;
13398165Stjr
13498165Stjr    void lock();
13598165Stjr    bool try_lock();
13698165Stjr
137185714Skeramida    template <class Rep, class Period>
138185714Skeramida        bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
139185714Skeramida    template <class Clock, class Duration>
140185714Skeramida        bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
1411590Srgrimes
1421590Srgrimes    void unlock();
14328696Scharnier
144165454Sru    void swap(unique_lock& u) noexcept;
145165454Sru    mutex_type* release() noexcept;
146165454Sru
147165454Sru    bool owns_lock() const noexcept;
148165454Sru    explicit operator bool () const noexcept;
149141846Sru    mutex_type* mutex() const noexcept;
150131507Sru};
151165455Sru
152140055Srutemplate <class Mutex>
153140055Sru  void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
1541590Srgrimes
15581622Srutemplate <class L1, class L2, class... L3>
15681622Sru  int try_lock(L1&, L2&, L3&...);
15781622Srutemplate <class L1, class L2, class... L3>
15881622Sru  void lock(L1&, L2&, L3&...);
1591590Srgrimes
160165454Srustruct once_flag
161165454Sru{
162165454Sru    constexpr once_flag() noexcept;
163101670Stjr
1641590Srgrimes    once_flag(const once_flag&) = delete;
1651590Srgrimes    once_flag& operator=(const once_flag&) = delete;
166185714Skeramida};
167185714Skeramida
168185714Skeramidatemplate<class Callable, class ...Args>
169185714Skeramida  void call_once(once_flag& flag, Callable&& func, Args&&... args);
170185714Skeramida
171185714Skeramida}  // std
172185714Skeramida
173185714Skeramida*/
174185714Skeramida
175185714Skeramida#include <__config>
176140420Sru#include <__mutex_base>
177140420Sru#include <functional>
1781590Srgrimes#ifndef _LIBCPP_HAS_NO_VARIADICS
1791590Srgrimes#include <tuple>
18028696Scharnier#endif
18198167Stjr#include <sched.h>
18298165Stjr
18317891Swosch#include <__undef_min_max>
18417891Swosch
18517891Swosch#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18617891Swosch#pragma GCC system_header
18717891Swosch#endif
188
189_LIBCPP_BEGIN_NAMESPACE_STD
190
191#ifndef _LIBCPP_HAS_NO_THREADS
192
193class _LIBCPP_TYPE_VIS recursive_mutex
194{
195    pthread_mutex_t __m_;
196
197public:
198     recursive_mutex();
199     ~recursive_mutex();
200
201private:
202    recursive_mutex(const recursive_mutex&); // = delete;
203    recursive_mutex& operator=(const recursive_mutex&); // = delete;
204
205public:
206    void lock();
207    bool try_lock() _NOEXCEPT;
208    void unlock()  _NOEXCEPT;
209
210    typedef pthread_mutex_t* native_handle_type;
211    _LIBCPP_INLINE_VISIBILITY
212    native_handle_type native_handle() {return &__m_;}
213};
214
215class _LIBCPP_TYPE_VIS timed_mutex
216{
217    mutex              __m_;
218    condition_variable __cv_;
219    bool               __locked_;
220public:
221     timed_mutex();
222     ~timed_mutex();
223
224private:
225    timed_mutex(const timed_mutex&); // = delete;
226    timed_mutex& operator=(const timed_mutex&); // = delete;
227
228public:
229    void lock();
230    bool try_lock() _NOEXCEPT;
231    template <class _Rep, class _Period>
232        _LIBCPP_INLINE_VISIBILITY
233        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
234            {return try_lock_until(chrono::steady_clock::now() + __d);}
235    template <class _Clock, class _Duration>
236        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
237    void unlock() _NOEXCEPT;
238};
239
240template <class _Clock, class _Duration>
241bool
242timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
243{
244    using namespace chrono;
245    unique_lock<mutex> __lk(__m_);
246    bool no_timeout = _Clock::now() < __t;
247    while (no_timeout && __locked_)
248        no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
249    if (!__locked_)
250    {
251        __locked_ = true;
252        return true;
253    }
254    return false;
255}
256
257class _LIBCPP_TYPE_VIS recursive_timed_mutex
258{
259    mutex              __m_;
260    condition_variable __cv_;
261    size_t             __count_;
262    pthread_t          __id_;
263public:
264     recursive_timed_mutex();
265     ~recursive_timed_mutex();
266
267private:
268    recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
269    recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
270
271public:
272    void lock();
273    bool try_lock() _NOEXCEPT;
274    template <class _Rep, class _Period>
275        _LIBCPP_INLINE_VISIBILITY
276        bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
277            {return try_lock_until(chrono::steady_clock::now() + __d);}
278    template <class _Clock, class _Duration>
279        bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
280    void unlock() _NOEXCEPT;
281};
282
283template <class _Clock, class _Duration>
284bool
285recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
286{
287    using namespace chrono;
288    pthread_t __id = pthread_self();
289    unique_lock<mutex> lk(__m_);
290    if (pthread_equal(__id, __id_))
291    {
292        if (__count_ == numeric_limits<size_t>::max())
293            return false;
294        ++__count_;
295        return true;
296    }
297    bool no_timeout = _Clock::now() < __t;
298    while (no_timeout && __count_ != 0)
299        no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
300    if (__count_ == 0)
301    {
302        __count_ = 1;
303        __id_ = __id;
304        return true;
305    }
306    return false;
307}
308
309template <class _L0, class _L1>
310int
311try_lock(_L0& __l0, _L1& __l1)
312{
313    unique_lock<_L0> __u0(__l0, try_to_lock);
314    if (__u0.owns_lock())
315    {
316        if (__l1.try_lock())
317        {
318            __u0.release();
319            return -1;
320        }
321        else
322            return 1;
323    }
324    return 0;
325}
326
327#ifndef _LIBCPP_HAS_NO_VARIADICS
328
329template <class _L0, class _L1, class _L2, class... _L3>
330int
331try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
332{
333    int __r = 0;
334    unique_lock<_L0> __u0(__l0, try_to_lock);
335    if (__u0.owns_lock())
336    {
337        __r = try_lock(__l1, __l2, __l3...);
338        if (__r == -1)
339            __u0.release();
340        else
341            ++__r;
342    }
343    return __r;
344}
345
346#endif  // _LIBCPP_HAS_NO_VARIADICS
347
348template <class _L0, class _L1>
349void
350lock(_L0& __l0, _L1& __l1)
351{
352    while (true)
353    {
354        {
355            unique_lock<_L0> __u0(__l0);
356            if (__l1.try_lock())
357            {
358                __u0.release();
359                break;
360            }
361        }
362        sched_yield();
363        {
364            unique_lock<_L1> __u1(__l1);
365            if (__l0.try_lock())
366            {
367                __u1.release();
368                break;
369            }
370        }
371        sched_yield();
372    }
373}
374
375#ifndef _LIBCPP_HAS_NO_VARIADICS
376
377template <class _L0, class _L1, class _L2, class ..._L3>
378void
379__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
380{
381    while (true)
382    {
383        switch (__i)
384        {
385        case 0:
386            {
387                unique_lock<_L0> __u0(__l0);
388                __i = try_lock(__l1, __l2, __l3...);
389                if (__i == -1)
390                {
391                    __u0.release();
392                    return;
393                }
394            }
395            ++__i;
396            sched_yield();
397            break;
398        case 1:
399            {
400                unique_lock<_L1> __u1(__l1);
401                __i = try_lock(__l2, __l3..., __l0);
402                if (__i == -1)
403                {
404                    __u1.release();
405                    return;
406                }
407            }
408            if (__i == sizeof...(_L3) + 1)
409                __i = 0;
410            else
411                __i += 2;
412            sched_yield();
413            break;
414        default:
415            __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
416            return;
417        }
418    }
419}
420
421template <class _L0, class _L1, class _L2, class ..._L3>
422inline _LIBCPP_INLINE_VISIBILITY
423void
424lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
425{
426    __lock_first(0, __l0, __l1, __l2, __l3...);
427}
428
429#endif  // _LIBCPP_HAS_NO_VARIADICS
430
431#endif // !_LIBCPP_HAS_NO_THREADS
432
433struct _LIBCPP_TYPE_VIS_ONLY once_flag;
434
435#ifndef _LIBCPP_HAS_NO_VARIADICS
436
437template<class _Callable, class... _Args>
438_LIBCPP_INLINE_VISIBILITY
439void call_once(once_flag&, _Callable&&, _Args&&...);
440
441#else  // _LIBCPP_HAS_NO_VARIADICS
442
443template<class _Callable>
444_LIBCPP_INLINE_VISIBILITY
445void call_once(once_flag&, _Callable);
446
447#endif  // _LIBCPP_HAS_NO_VARIADICS
448
449struct _LIBCPP_TYPE_VIS_ONLY once_flag
450{
451    _LIBCPP_INLINE_VISIBILITY
452    _LIBCPP_CONSTEXPR
453        once_flag() _NOEXCEPT : __state_(0) {}
454
455private:
456    once_flag(const once_flag&); // = delete;
457    once_flag& operator=(const once_flag&); // = delete;
458
459    unsigned long __state_;
460
461#ifndef _LIBCPP_HAS_NO_VARIADICS
462    template<class _Callable, class... _Args>
463    friend
464    void call_once(once_flag&, _Callable&&, _Args&&...);
465#else  // _LIBCPP_HAS_NO_VARIADICS
466    template<class _Callable>
467    friend
468    void call_once(once_flag&, _Callable);
469#endif  // _LIBCPP_HAS_NO_VARIADICS
470};
471
472#ifndef _LIBCPP_HAS_NO_VARIADICS
473
474template <class _Fp>
475class __call_once_param
476{
477    _Fp __f_;
478public:
479#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
480    _LIBCPP_INLINE_VISIBILITY
481    explicit __call_once_param(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
482#else
483    _LIBCPP_INLINE_VISIBILITY
484    explicit __call_once_param(const _Fp& __f) : __f_(__f) {}
485#endif
486
487    _LIBCPP_INLINE_VISIBILITY
488    void operator()()
489    {
490        typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
491        __execute(_Index());
492    }
493
494private:
495    template <size_t ..._Indices>
496    _LIBCPP_INLINE_VISIBILITY
497    void __execute(__tuple_indices<_Indices...>)
498    {
499        __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
500    }
501};
502
503#else
504
505template <class _Fp>
506class __call_once_param
507{
508    _Fp __f_;
509public:
510#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
511    _LIBCPP_INLINE_VISIBILITY
512    explicit __call_once_param(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
513#else
514    _LIBCPP_INLINE_VISIBILITY
515    explicit __call_once_param(const _Fp& __f) : __f_(__f) {}
516#endif
517
518    _LIBCPP_INLINE_VISIBILITY
519    void operator()()
520    {
521        __f_();
522    }
523};
524
525#endif
526
527template <class _Fp>
528void
529__call_once_proxy(void* __vp)
530{
531    __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
532    (*__p)();
533}
534
535_LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
536
537#ifndef _LIBCPP_HAS_NO_VARIADICS
538
539template<class _Callable, class... _Args>
540inline _LIBCPP_INLINE_VISIBILITY
541void
542call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
543{
544    if (__flag.__state_ != ~0ul)
545    {
546        typedef tuple<typename decay<_Callable>::type, typename decay<_Args>::type...> _Gp;
547        __call_once_param<_Gp> __p(_Gp(__decay_copy(_VSTD::forward<_Callable>(__func)),
548                                __decay_copy(_VSTD::forward<_Args>(__args))...));
549        __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
550    }
551}
552
553#else  // _LIBCPP_HAS_NO_VARIADICS
554
555template<class _Callable>
556inline _LIBCPP_INLINE_VISIBILITY
557void
558call_once(once_flag& __flag, _Callable __func)
559{
560    if (__flag.__state_ != ~0ul)
561    {
562        __call_once_param<_Callable> __p(__func);
563        __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
564    }
565}
566
567#endif  // _LIBCPP_HAS_NO_VARIADICS
568
569_LIBCPP_END_NAMESPACE_STD
570
571#endif  // _LIBCPP_MUTEX
572