codekingpro/portable-devtools
114k
1// -*- C++ -*-2//===--------------------------- mutex ------------------------------------===//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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCUDACXX_MUTEX11#define _LIBCUDACXX_MUTEX12 13/*14 mutex synopsis15 16namespace std17{18 19class mutex20{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_mutex37{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_mutex54{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_mutex72{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_guard99{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++17113{114public:115 using mutex_type = Mutex; // If MutexTypes... consists of the single type Mutex116 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 only124};125 126template <class Mutex>127class unique_lock128{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_flag175{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} // std186 187*/188 189#include <__config>190#include <__mutex_base>191#include <cstdint>192#include <functional>193#include <memory>194#include <tuple>195#include <version>196#include <__threading_support>197 198#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)199# pragma GCC system_header200#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)201# pragma clang system_header202#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)203# pragma system_header204#endif // no system header205 206_LIBCUDACXX_PUSH_MACROS207#include <__undef_macros>208 209 210_LIBCUDACXX_BEGIN_NAMESPACE_STD211 212#ifndef _LIBCUDACXX_HAS_NO_THREADS213 214class _LIBCUDACXX_TYPE_VIS recursive_mutex215{216 __libcpp_recursive_mutex_t __m_;217 218public:219 recursive_mutex();220 ~recursive_mutex();221 222private:223 recursive_mutex(const recursive_mutex&); // = delete;224 recursive_mutex& operator=(const recursive_mutex&); // = delete;225 226public:227 void lock();228 bool try_lock() noexcept;229 void unlock() noexcept;230 231 typedef __libcpp_recursive_mutex_t* native_handle_type;232 233 _LIBCUDACXX_INLINE_VISIBILITY234 native_handle_type native_handle() {return &__m_;}235};236 237class _LIBCUDACXX_TYPE_VIS timed_mutex238{239 mutex __m_;240 condition_variable __cv_;241 bool __locked_;242public:243 timed_mutex();244 ~timed_mutex();245 246private:247 timed_mutex(const timed_mutex&); // = delete;248 timed_mutex& operator=(const timed_mutex&); // = delete;249 250public:251 void lock();252 bool try_lock() noexcept;253 template <class _Rep, class _Period>254 _LIBCUDACXX_INLINE_VISIBILITY255 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)256 {return try_lock_until(chrono::steady_clock::now() + __d);}257 template <class _Clock, class _Duration>258 _LIBCUDACXX_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS259 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);260 void unlock() noexcept;261};262 263template <class _Clock, class _Duration>264bool265timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)266{267 using namespace chrono;268 unique_lock<mutex> __lk(__m_);269 bool no_timeout = _Clock::now() < __t;270 while (no_timeout && __locked_)271 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;272 if (!__locked_)273 {274 __locked_ = true;275 return true;276 }277 return false;278}279 280class _LIBCUDACXX_TYPE_VIS recursive_timed_mutex281{282 mutex __m_;283 condition_variable __cv_;284 size_t __count_;285 __thread_id __id_;286public:287 recursive_timed_mutex();288 ~recursive_timed_mutex();289 290private:291 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;292 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;293 294public:295 void lock();296 bool try_lock() noexcept;297 template <class _Rep, class _Period>298 _LIBCUDACXX_INLINE_VISIBILITY299 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 _LIBCUDACXX_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS303 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);304 void unlock() noexcept;305};306 307template <class _Clock, class _Duration>308bool309recursive_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>334int335try_lock(_L0& __l0, _L1& __l1)336{337 unique_lock<_L0> __u0(__l0, try_to_lock);338 if (__u0.owns_lock())339 {340 if (__l1.try_lock())341 {342 __u0.release();343 return -1;344 }345 else346 return 1;347 }348 return 0;349}350 351template <class _L0, class _L1, class _L2, class... _L3>352int353try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)354{355 int __r = 0;356 unique_lock<_L0> __u0(__l0, try_to_lock);357 if (__u0.owns_lock())358 {359 __r = try_lock(__l1, __l2, __l3...);360 if (__r == -1)361 __u0.release();362 else363 ++__r;364 }365 return __r;366}367 368template <class _L0, class _L1>369void370lock(_L0& __l0, _L1& __l1)371{372 while (true)373 {374 {375 unique_lock<_L0> __u0(__l0);376 if (__l1.try_lock())377 {378 __u0.release();379 break;380 }381 }382 __libcpp_thread_yield();383 {384 unique_lock<_L1> __u1(__l1);385 if (__l0.try_lock())386 {387 __u1.release();388 break;389 }390 }391 __libcpp_thread_yield();392 }393}394 395template <class _L0, class _L1, class _L2, class ..._L3>396void397__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)398{399 while (true)400 {401 switch (__i)402 {403 case 0:404 {405 unique_lock<_L0> __u0(__l0);406 __i = try_lock(__l1, __l2, __l3...);407 if (__i == -1)408 {409 __u0.release();410 return;411 }412 }413 ++__i;414 __libcpp_thread_yield();415 break;416 case 1:417 {418 unique_lock<_L1> __u1(__l1);419 __i = try_lock(__l2, __l3..., __l0);420 if (__i == -1)421 {422 __u1.release();423 return;424 }425 }426 if (__i == sizeof...(_L3) + 1)427 __i = 0;428 else429 __i += 2;430 __libcpp_thread_yield();431 break;432 default:433 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);434 return;435 }436 }437}438 439template <class _L0, class _L1, class _L2, class ..._L3>440inline _LIBCUDACXX_INLINE_VISIBILITY441void442lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)443{444 __lock_first(0, __l0, __l1, __l2, __l3...);445}446 447template <class _L0>448inline _LIBCUDACXX_INLINE_VISIBILITY449void __unlock(_L0& __l0) {450 __l0.unlock();451}452 453template <class _L0, class _L1>454inline _LIBCUDACXX_INLINE_VISIBILITY455void __unlock(_L0& __l0, _L1& __l1) {456 __l0.unlock();457 __l1.unlock();458}459 460template <class _L0, class _L1, class _L2, class ..._L3>461inline _LIBCUDACXX_INLINE_VISIBILITY462void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {463 __l0.unlock();464 __l1.unlock();465 _CUDA_VSTD::__unlock(__l2, __l3...);466}467 468#if _LIBCUDACXX_STD_VER > 14469template <class ..._Mutexes>470class _LIBCUDACXX_TEMPLATE_VIS scoped_lock;471 472template <>473class _LIBCUDACXX_TEMPLATE_VIS scoped_lock<> {474public:475 explicit scoped_lock() {}476 ~scoped_lock() = default;477 478 _LIBCUDACXX_INLINE_VISIBILITY479 explicit scoped_lock(adopt_lock_t) {}480 481 scoped_lock(scoped_lock const&) = delete;482 scoped_lock& operator=(scoped_lock const&) = delete;483};484 485template <class _Mutex>486class _LIBCUDACXX_TEMPLATE_VIS _LIBCUDACXX_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {487public:488 typedef _Mutex mutex_type;489private:490 mutex_type& __m_;491public:492 explicit scoped_lock(mutex_type & __m) _LIBCUDACXX_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))493 : __m_(__m) {__m_.lock();}494 495 ~scoped_lock() _LIBCUDACXX_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}496 497 _LIBCUDACXX_INLINE_VISIBILITY498 explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCUDACXX_THREAD_SAFETY_ANNOTATION(requires_capability(__m))499 : __m_(__m) {}500 501 scoped_lock(scoped_lock const&) = delete;502 scoped_lock& operator=(scoped_lock const&) = delete;503};504 505template <class ..._MArgs>506class _LIBCUDACXX_TEMPLATE_VIS scoped_lock507{508 static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");509 typedef tuple<_MArgs&...> _MutexTuple;510 511public:512 _LIBCUDACXX_INLINE_VISIBILITY513 explicit scoped_lock(_MArgs&... __margs)514 : __t_(__margs...)515 {516 _CUDA_VSTD::lock(__margs...);517 }518 519 _LIBCUDACXX_INLINE_VISIBILITY520 scoped_lock(adopt_lock_t, _MArgs&... __margs)521 : __t_(__margs...)522 {523 }524 525 _LIBCUDACXX_INLINE_VISIBILITY526 ~scoped_lock() {527 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;528 __unlock_unpack(_Indices{}, __t_);529 }530 531 scoped_lock(scoped_lock const&) = delete;532 scoped_lock& operator=(scoped_lock const&) = delete;533 534private:535 template <size_t ..._Indx>536 _LIBCUDACXX_INLINE_VISIBILITY537 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {538 _CUDA_VSTD::__unlock(_CUDA_VSTD::get<_Indx>(__mt)...);539 }540 541 _MutexTuple __t_;542};543 544#endif // _LIBCUDACXX_STD_VER > 14545#endif // !_LIBCUDACXX_HAS_NO_THREADS546 547struct _LIBCUDACXX_TEMPLATE_VIS once_flag;548 549template<class _Callable, class... _Args>550_LIBCUDACXX_INLINE_VISIBILITY551void call_once(once_flag&, _Callable&&, _Args&&...);552 553struct _LIBCUDACXX_TEMPLATE_VIS once_flag554{555 _LIBCUDACXX_INLINE_VISIBILITY556 constexpr once_flag() noexcept : __state_(0) {}557 558#if defined(_LIBCUDACXX_ABI_MICROSOFT)559 typedef uintptr_t _State_type;560#else561 typedef unsigned long _State_type;562#endif563 564 565private:566 once_flag(const once_flag&); // = delete;567 once_flag& operator=(const once_flag&); // = delete;568 569 _State_type __state_;570 571 template<class _Callable, class... _Args>572 friend573 void call_once(once_flag&, _Callable&&, _Args&&...);574};575 576template <class _Fp>577class __call_once_param578{579 _Fp& __f_;580public:581 _LIBCUDACXX_INLINE_VISIBILITY582 explicit __call_once_param(_Fp& __f) : __f_(__f) {}583 584 _LIBCUDACXX_INLINE_VISIBILITY585 void operator()()586 {587 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;588 __execute(_Index());589 }590 591private:592 template <size_t ..._Indices>593 _LIBCUDACXX_INLINE_VISIBILITY594 void __execute(__tuple_indices<_Indices...>)595 {596 __invoke(_CUDA_VSTD::get<0>(_CUDA_VSTD::move(__f_)), _CUDA_VSTD::get<_Indices>(_CUDA_VSTD::move(__f_))...);597 }598};599 600template <class _Fp>601void602__call_once_proxy(void* __vp)603{604 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);605 (*__p)();606}607 608_LIBCUDACXX_FUNC_VIS void __call_once(volatile once_flag::_State_type&, void*,609 void (*)(void*));610 611template<class _Callable, class... _Args>612inline _LIBCUDACXX_INLINE_VISIBILITY613void614call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)615{616 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))617 {618 typedef tuple<_Callable&&, _Args&&...> _Gp;619 _Gp __f(_CUDA_VSTD::forward<_Callable>(__func), _CUDA_VSTD::forward<_Args>(__args)...);620 __call_once_param<_Gp> __p(__f);621 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);622 }623}624 625_LIBCUDACXX_END_NAMESPACE_STD626 627_LIBCUDACXX_POP_MACROS628 629#endif // _LIBCUDACXX_MUTEX630 