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-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_MUTEX 11#define _LIBCPP_MUTEX 12 13/* 14 mutex synopsis 15 16namespace std 17{ 18 19class mutex 20{ 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_mutex 37{ 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_mutex 54{ 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_mutex 72{ 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_guard 99{ 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++17 113{ 114public: 115 using mutex_type = Mutex; // If MutexTypes... consists of the single type Mutex 116 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 only 124}; 125 126template <class Mutex> 127class unique_lock 128{ 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_flag 175{ 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} // std 186 187*/ 188 189#include <__config> 190#include <__mutex_base> 191#include <__threading_support> 192#include <__utility/forward.h> 193#include <cstdint> 194#include <functional> 195#include <memory> 196#ifndef _LIBCPP_CXX03_LANG 197# include <tuple> 198#endif 199#include <version> 200 201#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 202#pragma GCC system_header 203#endif 204 205_LIBCPP_PUSH_MACROS 206#include <__undef_macros> 207 208 209_LIBCPP_BEGIN_NAMESPACE_STD 210 211#ifndef _LIBCPP_HAS_NO_THREADS 212 213class _LIBCPP_TYPE_VIS recursive_mutex 214{ 215 __libcpp_recursive_mutex_t __m_; 216 217public: 218 recursive_mutex(); 219 ~recursive_mutex(); 220 221private: 222 recursive_mutex(const recursive_mutex&); // = delete; 223 recursive_mutex& operator=(const recursive_mutex&); // = delete; 224 225public: 226 void lock(); 227 bool try_lock() _NOEXCEPT; 228 void unlock() _NOEXCEPT; 229 230 typedef __libcpp_recursive_mutex_t* native_handle_type; 231 232 _LIBCPP_INLINE_VISIBILITY 233 native_handle_type native_handle() {return &__m_;} 234}; 235 236class _LIBCPP_TYPE_VIS timed_mutex 237{ 238 mutex __m_; 239 condition_variable __cv_; 240 bool __locked_; 241public: 242 timed_mutex(); 243 ~timed_mutex(); 244 245private: 246 timed_mutex(const timed_mutex&); // = delete; 247 timed_mutex& operator=(const timed_mutex&); // = delete; 248 249public: 250 void lock(); 251 bool try_lock() _NOEXCEPT; 252 template <class _Rep, class _Period> 253 _LIBCPP_INLINE_VISIBILITY 254 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) 255 {return try_lock_until(chrono::steady_clock::now() + __d);} 256 template <class _Clock, class _Duration> 257 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS 258 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); 259 void unlock() _NOEXCEPT; 260}; 261 262template <class _Clock, class _Duration> 263bool 264timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) 265{ 266 using namespace chrono; 267 unique_lock<mutex> __lk(__m_); 268 bool no_timeout = _Clock::now() < __t; 269 while (no_timeout && __locked_) 270 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout; 271 if (!__locked_) 272 { 273 __locked_ = true; 274 return true; 275 } 276 return false; 277} 278 279class _LIBCPP_TYPE_VIS recursive_timed_mutex 280{ 281 mutex __m_; 282 condition_variable __cv_; 283 size_t __count_; 284 __thread_id __id_; 285public: 286 recursive_timed_mutex(); 287 ~recursive_timed_mutex(); 288 289private: 290 recursive_timed_mutex(const recursive_timed_mutex&); // = delete; 291 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete; 292 293public: 294 void lock(); 295 bool try_lock() _NOEXCEPT; 296 template <class _Rep, class _Period> 297 _LIBCPP_INLINE_VISIBILITY 298 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) 299 {return try_lock_until(chrono::steady_clock::now() + __d);} 300 template <class _Clock, class _Duration> 301 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS 302 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); 303 void unlock() _NOEXCEPT; 304}; 305 306template <class _Clock, class _Duration> 307bool 308recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) 309{ 310 using namespace chrono; 311 __thread_id __id = this_thread::get_id(); 312 unique_lock<mutex> lk(__m_); 313 if (__id == __id_) 314 { 315 if (__count_ == numeric_limits<size_t>::max()) 316 return false; 317 ++__count_; 318 return true; 319 } 320 bool no_timeout = _Clock::now() < __t; 321 while (no_timeout && __count_ != 0) 322 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout; 323 if (__count_ == 0) 324 { 325 __count_ = 1; 326 __id_ = __id; 327 return true; 328 } 329 return false; 330} 331 332template <class _L0, class _L1> 333int 334try_lock(_L0& __l0, _L1& __l1) 335{ 336 unique_lock<_L0> __u0(__l0, try_to_lock); 337 if (__u0.owns_lock()) 338 { 339 if (__l1.try_lock()) 340 { 341 __u0.release(); 342 return -1; 343 } 344 else 345 return 1; 346 } 347 return 0; 348} 349 350#ifndef _LIBCPP_CXX03_LANG 351 352template <class _L0, class _L1, class _L2, class... _L3> 353int 354try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) 355{ 356 int __r = 0; 357 unique_lock<_L0> __u0(__l0, try_to_lock); 358 if (__u0.owns_lock()) 359 { 360 __r = try_lock(__l1, __l2, __l3...); 361 if (__r == -1) 362 __u0.release(); 363 else 364 ++__r; 365 } 366 return __r; 367} 368 369#endif // _LIBCPP_CXX03_LANG 370 371template <class _L0, class _L1> 372void 373lock(_L0& __l0, _L1& __l1) 374{ 375 while (true) 376 { 377 { 378 unique_lock<_L0> __u0(__l0); 379 if (__l1.try_lock()) 380 { 381 __u0.release(); 382 break; 383 } 384 } 385 __libcpp_thread_yield(); 386 { 387 unique_lock<_L1> __u1(__l1); 388 if (__l0.try_lock()) 389 { 390 __u1.release(); 391 break; 392 } 393 } 394 __libcpp_thread_yield(); 395 } 396} 397 398#ifndef _LIBCPP_CXX03_LANG 399 400template <class _L0, class _L1, class _L2, class ..._L3> 401void 402__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) 403{ 404 while (true) 405 { 406 switch (__i) 407 { 408 case 0: 409 { 410 unique_lock<_L0> __u0(__l0); 411 __i = try_lock(__l1, __l2, __l3...); 412 if (__i == -1) 413 { 414 __u0.release(); 415 return; 416 } 417 } 418 ++__i; 419 __libcpp_thread_yield(); 420 break; 421 case 1: 422 { 423 unique_lock<_L1> __u1(__l1); 424 __i = try_lock(__l2, __l3..., __l0); 425 if (__i == -1) 426 { 427 __u1.release(); 428 return; 429 } 430 } 431 if (__i == sizeof...(_L3) + 1) 432 __i = 0; 433 else 434 __i += 2; 435 __libcpp_thread_yield(); 436 break; 437 default: 438 __lock_first(__i - 2, __l2, __l3..., __l0, __l1); 439 return; 440 } 441 } 442} 443 444template <class _L0, class _L1, class _L2, class ..._L3> 445inline _LIBCPP_INLINE_VISIBILITY 446void 447lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) 448{ 449 __lock_first(0, __l0, __l1, __l2, __l3...); 450} 451 452template <class _L0> 453inline _LIBCPP_INLINE_VISIBILITY 454void __unlock(_L0& __l0) { 455 __l0.unlock(); 456} 457 458template <class _L0, class _L1> 459inline _LIBCPP_INLINE_VISIBILITY 460void __unlock(_L0& __l0, _L1& __l1) { 461 __l0.unlock(); 462 __l1.unlock(); 463} 464 465template <class _L0, class _L1, class _L2, class ..._L3> 466inline _LIBCPP_INLINE_VISIBILITY 467void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) { 468 __l0.unlock(); 469 __l1.unlock(); 470 _VSTD::__unlock(__l2, __l3...); 471} 472 473#endif // _LIBCPP_CXX03_LANG 474 475#if _LIBCPP_STD_VER > 14 476template <class ..._Mutexes> 477class _LIBCPP_TEMPLATE_VIS scoped_lock; 478 479template <> 480class _LIBCPP_TEMPLATE_VIS scoped_lock<> { 481public: 482 explicit scoped_lock() {} 483 ~scoped_lock() = default; 484 485 _LIBCPP_INLINE_VISIBILITY 486 explicit scoped_lock(adopt_lock_t) {} 487 488 scoped_lock(scoped_lock const&) = delete; 489 scoped_lock& operator=(scoped_lock const&) = delete; 490}; 491 492template <class _Mutex> 493class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> { 494public: 495 typedef _Mutex mutex_type; 496private: 497 mutex_type& __m_; 498public: 499 explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m)) 500 : __m_(__m) {__m_.lock();} 501 502 ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();} 503 504 _LIBCPP_INLINE_VISIBILITY 505 explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m)) 506 : __m_(__m) {} 507 508 scoped_lock(scoped_lock const&) = delete; 509 scoped_lock& operator=(scoped_lock const&) = delete; 510}; 511 512template <class ..._MArgs> 513class _LIBCPP_TEMPLATE_VIS scoped_lock 514{ 515 static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required"); 516 typedef tuple<_MArgs&...> _MutexTuple; 517 518public: 519 _LIBCPP_INLINE_VISIBILITY 520 explicit scoped_lock(_MArgs&... __margs) 521 : __t_(__margs...) 522 { 523 _VSTD::lock(__margs...); 524 } 525 526 _LIBCPP_INLINE_VISIBILITY 527 scoped_lock(adopt_lock_t, _MArgs&... __margs) 528 : __t_(__margs...) 529 { 530 } 531 532 _LIBCPP_INLINE_VISIBILITY 533 ~scoped_lock() { 534 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices; 535 __unlock_unpack(_Indices{}, __t_); 536 } 537 538 scoped_lock(scoped_lock const&) = delete; 539 scoped_lock& operator=(scoped_lock const&) = delete; 540 541private: 542 template <size_t ..._Indx> 543 _LIBCPP_INLINE_VISIBILITY 544 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) { 545 _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...); 546 } 547 548 _MutexTuple __t_; 549}; 550 551#endif // _LIBCPP_STD_VER > 14 552#endif // !_LIBCPP_HAS_NO_THREADS 553 554struct _LIBCPP_TEMPLATE_VIS once_flag; 555 556#ifndef _LIBCPP_CXX03_LANG 557 558template<class _Callable, class... _Args> 559_LIBCPP_INLINE_VISIBILITY 560void call_once(once_flag&, _Callable&&, _Args&&...); 561 562#else // _LIBCPP_CXX03_LANG 563 564template<class _Callable> 565_LIBCPP_INLINE_VISIBILITY 566void call_once(once_flag&, _Callable&); 567 568template<class _Callable> 569_LIBCPP_INLINE_VISIBILITY 570void call_once(once_flag&, const _Callable&); 571 572#endif // _LIBCPP_CXX03_LANG 573 574struct _LIBCPP_TEMPLATE_VIS once_flag 575{ 576 _LIBCPP_INLINE_VISIBILITY 577 _LIBCPP_CONSTEXPR 578 once_flag() _NOEXCEPT : __state_(0) {} 579 580#if defined(_LIBCPP_ABI_MICROSOFT) 581 typedef uintptr_t _State_type; 582#else 583 typedef unsigned long _State_type; 584#endif 585 586 587private: 588 once_flag(const once_flag&); // = delete; 589 once_flag& operator=(const once_flag&); // = delete; 590 591 _State_type __state_; 592 593#ifndef _LIBCPP_CXX03_LANG 594 template<class _Callable, class... _Args> 595 friend 596 void call_once(once_flag&, _Callable&&, _Args&&...); 597#else // _LIBCPP_CXX03_LANG 598 template<class _Callable> 599 friend 600 void call_once(once_flag&, _Callable&); 601 602 template<class _Callable> 603 friend 604 void call_once(once_flag&, const _Callable&); 605#endif // _LIBCPP_CXX03_LANG 606}; 607 608#ifndef _LIBCPP_CXX03_LANG 609 610template <class _Fp> 611class __call_once_param 612{ 613 _Fp& __f_; 614public: 615 _LIBCPP_INLINE_VISIBILITY 616 explicit __call_once_param(_Fp& __f) : __f_(__f) {} 617 618 _LIBCPP_INLINE_VISIBILITY 619 void operator()() 620 { 621 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index; 622 __execute(_Index()); 623 } 624 625private: 626 template <size_t ..._Indices> 627 _LIBCPP_INLINE_VISIBILITY 628 void __execute(__tuple_indices<_Indices...>) 629 { 630 _VSTD::__invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...); 631 } 632}; 633 634#else 635 636template <class _Fp> 637class __call_once_param 638{ 639 _Fp& __f_; 640public: 641 _LIBCPP_INLINE_VISIBILITY 642 explicit __call_once_param(_Fp& __f) : __f_(__f) {} 643 644 _LIBCPP_INLINE_VISIBILITY 645 void operator()() 646 { 647 __f_(); 648 } 649}; 650 651#endif 652 653template <class _Fp> 654void _LIBCPP_INLINE_VISIBILITY 655__call_once_proxy(void* __vp) 656{ 657 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp); 658 (*__p)(); 659} 660 661_LIBCPP_FUNC_VIS void __call_once(volatile once_flag::_State_type&, void*, 662 void (*)(void*)); 663 664#ifndef _LIBCPP_CXX03_LANG 665 666template<class _Callable, class... _Args> 667inline _LIBCPP_INLINE_VISIBILITY 668void 669call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args) 670{ 671 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0)) 672 { 673 typedef tuple<_Callable&&, _Args&&...> _Gp; 674 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...); 675 __call_once_param<_Gp> __p(__f); 676 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>); 677 } 678} 679 680#else // _LIBCPP_CXX03_LANG 681 682template<class _Callable> 683inline _LIBCPP_INLINE_VISIBILITY 684void 685call_once(once_flag& __flag, _Callable& __func) 686{ 687 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0)) 688 { 689 __call_once_param<_Callable> __p(__func); 690 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>); 691 } 692} 693 694template<class _Callable> 695inline _LIBCPP_INLINE_VISIBILITY 696void 697call_once(once_flag& __flag, const _Callable& __func) 698{ 699 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0)) 700 { 701 __call_once_param<const _Callable> __p(__func); 702 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>); 703 } 704} 705 706#endif // _LIBCPP_CXX03_LANG 707 708_LIBCPP_END_NAMESPACE_STD 709 710_LIBCPP_POP_MACROS 711 712#endif // _LIBCPP_MUTEX 713