1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP___MUTEX_BASE 12#define _LIBCPP___MUTEX_BASE 13 14#include <__config> 15#include <chrono> 16#include <system_error> 17#include <pthread.h> 18 19#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 20#pragma GCC system_header 21#endif 22 23#ifdef _LIBCPP_SHARED_LOCK 24 25namespace ting { 26template <class _Mutex> class shared_lock; 27template <class _Mutex> class upgrade_lock; 28} 29 30#endif // _LIBCPP_SHARED_LOCK 31 32 33_LIBCPP_BEGIN_NAMESPACE_STD 34 35class _LIBCPP_VISIBLE mutex 36{ 37 pthread_mutex_t __m_; 38 39public: 40 _LIBCPP_INLINE_VISIBILITY 41#ifndef _LIBCPP_HAS_NO_CONSTEXPR 42 constexpr mutex() _NOEXCEPT : __m_ PTHREAD_MUTEX_INITIALIZER {} 43#else 44 mutex() _NOEXCEPT {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;} 45#endif 46 ~mutex(); 47 48private: 49 mutex(const mutex&);// = delete; 50 mutex& operator=(const mutex&);// = delete; 51 52public: 53 void lock(); 54 bool try_lock() _NOEXCEPT; 55 void unlock() _NOEXCEPT; 56 57 typedef pthread_mutex_t* native_handle_type; 58 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;} 59}; 60 61struct _LIBCPP_VISIBLE defer_lock_t {}; 62struct _LIBCPP_VISIBLE try_to_lock_t {}; 63struct _LIBCPP_VISIBLE adopt_lock_t {}; 64 65#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX) 66 67extern const defer_lock_t defer_lock; 68extern const try_to_lock_t try_to_lock; 69extern const adopt_lock_t adopt_lock; 70 71#else 72 73constexpr defer_lock_t defer_lock = defer_lock_t(); 74constexpr try_to_lock_t try_to_lock = try_to_lock_t(); 75constexpr adopt_lock_t adopt_lock = adopt_lock_t(); 76 77#endif 78 79template <class _Mutex> 80class _LIBCPP_VISIBLE lock_guard 81{ 82public: 83 typedef _Mutex mutex_type; 84 85private: 86 mutex_type& __m_; 87public: 88 89 _LIBCPP_INLINE_VISIBILITY 90 explicit lock_guard(mutex_type& __m) 91 : __m_(__m) {__m_.lock();} 92 _LIBCPP_INLINE_VISIBILITY 93 lock_guard(mutex_type& __m, adopt_lock_t) 94 : __m_(__m) {} 95 _LIBCPP_INLINE_VISIBILITY 96 ~lock_guard() {__m_.unlock();} 97 98private: 99 lock_guard(lock_guard const&);// = delete; 100 lock_guard& operator=(lock_guard const&);// = delete; 101}; 102 103template <class _Mutex> 104class _LIBCPP_VISIBLE unique_lock 105{ 106public: 107 typedef _Mutex mutex_type; 108 109private: 110 mutex_type* __m_; 111 bool __owns_; 112 113public: 114 _LIBCPP_INLINE_VISIBILITY 115 unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {} 116 _LIBCPP_INLINE_VISIBILITY 117 explicit unique_lock(mutex_type& __m) 118 : __m_(&__m), __owns_(true) {__m_->lock();} 119 _LIBCPP_INLINE_VISIBILITY 120 unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT 121 : __m_(&__m), __owns_(false) {} 122 _LIBCPP_INLINE_VISIBILITY 123 unique_lock(mutex_type& __m, try_to_lock_t) 124 : __m_(&__m), __owns_(__m.try_lock()) {} 125 _LIBCPP_INLINE_VISIBILITY 126 unique_lock(mutex_type& __m, adopt_lock_t) 127 : __m_(&__m), __owns_(true) {} 128 template <class _Clock, class _Duration> 129 _LIBCPP_INLINE_VISIBILITY 130 unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t) 131 : __m_(&__m), __owns_(__m.try_lock_until(__t)) {} 132 template <class _Rep, class _Period> 133 _LIBCPP_INLINE_VISIBILITY 134 unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d) 135 : __m_(&__m), __owns_(__m.try_lock_for(__d)) {} 136 _LIBCPP_INLINE_VISIBILITY 137 ~unique_lock() 138 { 139 if (__owns_) 140 __m_->unlock(); 141 } 142 143private: 144 unique_lock(unique_lock const&); // = delete; 145 unique_lock& operator=(unique_lock const&); // = delete; 146 147public: 148#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 149 _LIBCPP_INLINE_VISIBILITY 150 unique_lock(unique_lock&& __u) _NOEXCEPT 151 : __m_(__u.__m_), __owns_(__u.__owns_) 152 {__u.__m_ = nullptr; __u.__owns_ = false;} 153 _LIBCPP_INLINE_VISIBILITY 154 unique_lock& operator=(unique_lock&& __u) _NOEXCEPT 155 { 156 if (__owns_) 157 __m_->unlock(); 158 __m_ = __u.__m_; 159 __owns_ = __u.__owns_; 160 __u.__m_ = nullptr; 161 __u.__owns_ = false; 162 return *this; 163 } 164 165#ifdef _LIBCPP_SHARED_LOCK 166 167 unique_lock(ting::shared_lock<mutex_type>&&, try_to_lock_t); 168 template <class _Clock, class _Duration> 169 unique_lock(ting::shared_lock<mutex_type>&&, 170 const chrono::time_point<_Clock, _Duration>&); 171 template <class _Rep, class _Period> 172 unique_lock(ting::shared_lock<mutex_type>&&, 173 const chrono::duration<_Rep, _Period>&); 174 175 explicit unique_lock(ting::upgrade_lock<mutex_type>&&); 176 unique_lock(ting::upgrade_lock<mutex_type>&&, try_to_lock_t); 177 template <class _Clock, class _Duration> 178 unique_lock(ting::upgrade_lock<mutex_type>&&, 179 const chrono::time_point<_Clock, _Duration>&); 180 template <class _Rep, class _Period> 181 unique_lock(ting::upgrade_lock<mutex_type>&&, 182 const chrono::duration<_Rep, _Period>&); 183 184#endif // _LIBCPP_SHARED_LOCK 185 186#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 187 188 void lock(); 189 bool try_lock(); 190 191 template <class _Rep, class _Period> 192 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d); 193 template <class _Clock, class _Duration> 194 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); 195 196 void unlock(); 197 198 _LIBCPP_INLINE_VISIBILITY 199 void swap(unique_lock& __u) _NOEXCEPT 200 { 201 _VSTD::swap(__m_, __u.__m_); 202 _VSTD::swap(__owns_, __u.__owns_); 203 } 204 _LIBCPP_INLINE_VISIBILITY 205 mutex_type* release() _NOEXCEPT 206 { 207 mutex_type* __m = __m_; 208 __m_ = nullptr; 209 __owns_ = false; 210 return __m; 211 } 212 213 _LIBCPP_INLINE_VISIBILITY 214 bool owns_lock() const _NOEXCEPT {return __owns_;} 215 _LIBCPP_INLINE_VISIBILITY 216 _LIBCPP_EXPLICIT 217 operator bool () const _NOEXCEPT {return __owns_;} 218 _LIBCPP_INLINE_VISIBILITY 219 mutex_type* mutex() const _NOEXCEPT {return __m_;} 220}; 221 222template <class _Mutex> 223void 224unique_lock<_Mutex>::lock() 225{ 226 if (__m_ == nullptr) 227 __throw_system_error(EPERM, "unique_lock::lock: references null mutex"); 228 if (__owns_) 229 __throw_system_error(EDEADLK, "unique_lock::lock: already locked"); 230 __m_->lock(); 231 __owns_ = true; 232} 233 234template <class _Mutex> 235bool 236unique_lock<_Mutex>::try_lock() 237{ 238 if (__m_ == nullptr) 239 __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex"); 240 if (__owns_) 241 __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked"); 242 __owns_ = __m_->try_lock(); 243 return __owns_; 244} 245 246template <class _Mutex> 247template <class _Rep, class _Period> 248bool 249unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) 250{ 251 if (__m_ == nullptr) 252 __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex"); 253 if (__owns_) 254 __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked"); 255 __owns_ = __m_->try_lock_for(__d); 256 return __owns_; 257} 258 259template <class _Mutex> 260template <class _Clock, class _Duration> 261bool 262unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) 263{ 264 if (__m_ == nullptr) 265 __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex"); 266 if (__owns_) 267 __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked"); 268 __owns_ = __m_->try_lock_until(__t); 269 return __owns_; 270} 271 272template <class _Mutex> 273void 274unique_lock<_Mutex>::unlock() 275{ 276 if (!__owns_) 277 __throw_system_error(EPERM, "unique_lock::unlock: not locked"); 278 __m_->unlock(); 279 __owns_ = false; 280} 281 282template <class _Mutex> 283inline _LIBCPP_INLINE_VISIBILITY 284void 285swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT 286 {__x.swap(__y);} 287 288struct _LIBCPP_VISIBLE cv_status 289{ 290 enum _ { 291 no_timeout, 292 timeout 293 }; 294 295 _ __v_; 296 297 _LIBCPP_INLINE_VISIBILITY cv_status(_ __v) : __v_(__v) {} 298 _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;} 299 300}; 301 302class _LIBCPP_VISIBLE condition_variable 303{ 304 pthread_cond_t __cv_; 305public: 306 _LIBCPP_INLINE_VISIBILITY 307#ifndef _LIBCPP_HAS_NO_CONSTEXPR 308 constexpr condition_variable() : __cv_ PTHREAD_COND_INITIALIZER {} 309#else 310 condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;} 311#endif 312 ~condition_variable(); 313 314private: 315 condition_variable(const condition_variable&); // = delete; 316 condition_variable& operator=(const condition_variable&); // = delete; 317 318public: 319 void notify_one() _NOEXCEPT; 320 void notify_all() _NOEXCEPT; 321 322 void wait(unique_lock<mutex>& __lk); 323 template <class _Predicate> 324 void wait(unique_lock<mutex>& __lk, _Predicate __pred); 325 326 template <class _Duration> 327 cv_status 328 wait_until(unique_lock<mutex>& __lk, 329 const chrono::time_point<chrono::system_clock, _Duration>& __t); 330 331 template <class _Clock, class _Duration> 332 cv_status 333 wait_until(unique_lock<mutex>& __lk, 334 const chrono::time_point<_Clock, _Duration>& __t); 335 336 template <class _Clock, class _Duration, class _Predicate> 337 bool 338 wait_until(unique_lock<mutex>& __lk, 339 const chrono::time_point<_Clock, _Duration>& __t, 340 _Predicate __pred); 341 342 template <class _Rep, class _Period> 343 cv_status 344 wait_for(unique_lock<mutex>& __lk, 345 const chrono::duration<_Rep, _Period>& __d); 346 347 template <class _Rep, class _Period, class _Predicate> 348 bool 349 wait_for(unique_lock<mutex>& __lk, 350 const chrono::duration<_Rep, _Period>& __d, 351 _Predicate __pred); 352 353 typedef pthread_cond_t* native_handle_type; 354 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;} 355 356private: 357 void __do_timed_wait(unique_lock<mutex>& __lk, 358 chrono::time_point<chrono::system_clock, chrono::nanoseconds>); 359}; 360 361template <class _To, class _Rep, class _Period> 362inline _LIBCPP_INLINE_VISIBILITY 363typename enable_if 364< 365 chrono::__is_duration<_To>::value, 366 _To 367>::type 368__ceil(chrono::duration<_Rep, _Period> __d) 369{ 370 using namespace chrono; 371 _To __r = duration_cast<_To>(__d); 372 if (__r < __d) 373 ++__r; 374 return __r; 375} 376 377template <class _Predicate> 378void 379condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred) 380{ 381 while (!__pred()) 382 wait(__lk); 383} 384 385template <class _Duration> 386cv_status 387condition_variable::wait_until(unique_lock<mutex>& __lk, 388 const chrono::time_point<chrono::system_clock, _Duration>& __t) 389{ 390 using namespace chrono; 391 typedef time_point<system_clock, nanoseconds> __nano_sys_tmpt; 392 __do_timed_wait(__lk, 393 __nano_sys_tmpt(__ceil<nanoseconds>(__t.time_since_epoch()))); 394 return system_clock::now() < __t ? cv_status::no_timeout : 395 cv_status::timeout; 396} 397 398template <class _Clock, class _Duration> 399cv_status 400condition_variable::wait_until(unique_lock<mutex>& __lk, 401 const chrono::time_point<_Clock, _Duration>& __t) 402{ 403 using namespace chrono; 404 system_clock::time_point __s_now = system_clock::now(); 405 typename _Clock::time_point __c_now = _Clock::now(); 406 __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__t - __c_now)); 407 return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout; 408} 409 410template <class _Clock, class _Duration, class _Predicate> 411bool 412condition_variable::wait_until(unique_lock<mutex>& __lk, 413 const chrono::time_point<_Clock, _Duration>& __t, 414 _Predicate __pred) 415{ 416 while (!__pred()) 417 { 418 if (wait_until(__lk, __t) == cv_status::timeout) 419 return __pred(); 420 } 421 return true; 422} 423 424template <class _Rep, class _Period> 425cv_status 426condition_variable::wait_for(unique_lock<mutex>& __lk, 427 const chrono::duration<_Rep, _Period>& __d) 428{ 429 using namespace chrono; 430 system_clock::time_point __s_now = system_clock::now(); 431 steady_clock::time_point __c_now = steady_clock::now(); 432 __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d)); 433 return steady_clock::now() - __c_now < __d ? cv_status::no_timeout : 434 cv_status::timeout; 435} 436 437template <class _Rep, class _Period, class _Predicate> 438inline _LIBCPP_INLINE_VISIBILITY 439bool 440condition_variable::wait_for(unique_lock<mutex>& __lk, 441 const chrono::duration<_Rep, _Period>& __d, 442 _Predicate __pred) 443{ 444 return wait_until(__lk, chrono::steady_clock::now() + __d, 445 _VSTD::move(__pred)); 446} 447 448_LIBCPP_END_NAMESPACE_STD 449 450#endif // _LIBCPP___MUTEX_BASE 451