__mutex_base (232924) | __mutex_base (241900) |
---|---|
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// --- 24 unchanged lines hidden (view full) --- 33_LIBCPP_BEGIN_NAMESPACE_STD 34 35class _LIBCPP_VISIBLE mutex 36{ 37 pthread_mutex_t __m_; 38 39public: 40 _LIBCPP_INLINE_VISIBILITY | 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// --- 24 unchanged lines hidden (view full) --- 33_LIBCPP_BEGIN_NAMESPACE_STD 34 35class _LIBCPP_VISIBLE mutex 36{ 37 pthread_mutex_t __m_; 38 39public: 40 _LIBCPP_INLINE_VISIBILITY |
41 mutex() {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;} | 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 |
42 ~mutex(); 43 44private: 45 mutex(const mutex&);// = delete; 46 mutex& operator=(const mutex&);// = delete; 47 48public: 49 void lock(); | 46 ~mutex(); 47 48private: 49 mutex(const mutex&);// = delete; 50 mutex& operator=(const mutex&);// = delete; 51 52public: 53 void lock(); |
50 bool try_lock(); 51 void unlock(); | 54 bool try_lock() _NOEXCEPT; 55 void unlock() _NOEXCEPT; |
52 53 typedef pthread_mutex_t* native_handle_type; 54 _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;} 55}; 56 57struct _LIBCPP_VISIBLE defer_lock_t {}; 58struct _LIBCPP_VISIBLE try_to_lock_t {}; 59struct _LIBCPP_VISIBLE adopt_lock_t {}; 60 | 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 |
61//constexpr 62extern const 63defer_lock_t defer_lock; | 65#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX) |
64 | 66 |
65//constexpr 66extern const 67try_to_lock_t try_to_lock; | 67extern const defer_lock_t defer_lock; 68extern const try_to_lock_t try_to_lock; 69extern const adopt_lock_t adopt_lock; |
68 | 70 |
69//constexpr 70extern const 71adopt_lock_t adopt_lock; | 71#else |
72 | 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 |
|
73template <class _Mutex> 74class _LIBCPP_VISIBLE lock_guard 75{ 76public: 77 typedef _Mutex mutex_type; 78 79private: 80 mutex_type& __m_; --- 20 unchanged lines hidden (view full) --- 101 typedef _Mutex mutex_type; 102 103private: 104 mutex_type* __m_; 105 bool __owns_; 106 107public: 108 _LIBCPP_INLINE_VISIBILITY | 79template <class _Mutex> 80class _LIBCPP_VISIBLE lock_guard 81{ 82public: 83 typedef _Mutex mutex_type; 84 85private: 86 mutex_type& __m_; --- 20 unchanged lines hidden (view full) --- 107 typedef _Mutex mutex_type; 108 109private: 110 mutex_type* __m_; 111 bool __owns_; 112 113public: 114 _LIBCPP_INLINE_VISIBILITY |
109 unique_lock() : __m_(nullptr), __owns_(false) {} | 115 unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {} |
110 _LIBCPP_INLINE_VISIBILITY 111 explicit unique_lock(mutex_type& __m) 112 : __m_(&__m), __owns_(true) {__m_->lock();} 113 _LIBCPP_INLINE_VISIBILITY | 116 _LIBCPP_INLINE_VISIBILITY 117 explicit unique_lock(mutex_type& __m) 118 : __m_(&__m), __owns_(true) {__m_->lock();} 119 _LIBCPP_INLINE_VISIBILITY |
114 unique_lock(mutex_type& __m, defer_lock_t) | 120 unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT |
115 : __m_(&__m), __owns_(false) {} 116 _LIBCPP_INLINE_VISIBILITY 117 unique_lock(mutex_type& __m, try_to_lock_t) 118 : __m_(&__m), __owns_(__m.try_lock()) {} 119 _LIBCPP_INLINE_VISIBILITY 120 unique_lock(mutex_type& __m, adopt_lock_t) 121 : __m_(&__m), __owns_(true) {} 122 template <class _Clock, class _Duration> --- 13 unchanged lines hidden (view full) --- 136 137private: 138 unique_lock(unique_lock const&); // = delete; 139 unique_lock& operator=(unique_lock const&); // = delete; 140 141public: 142#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 143 _LIBCPP_INLINE_VISIBILITY | 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> --- 13 unchanged lines hidden (view full) --- 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 |
144 unique_lock(unique_lock&& __u) | 150 unique_lock(unique_lock&& __u) _NOEXCEPT |
145 : __m_(__u.__m_), __owns_(__u.__owns_) 146 {__u.__m_ = nullptr; __u.__owns_ = false;} 147 _LIBCPP_INLINE_VISIBILITY | 151 : __m_(__u.__m_), __owns_(__u.__owns_) 152 {__u.__m_ = nullptr; __u.__owns_ = false;} 153 _LIBCPP_INLINE_VISIBILITY |
148 unique_lock& operator=(unique_lock&& __u) | 154 unique_lock& operator=(unique_lock&& __u) _NOEXCEPT |
149 { 150 if (__owns_) 151 __m_->unlock(); 152 __m_ = __u.__m_; 153 __owns_ = __u.__owns_; 154 __u.__m_ = nullptr; 155 __u.__owns_ = false; 156 return *this; --- 28 unchanged lines hidden (view full) --- 185 template <class _Rep, class _Period> 186 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d); 187 template <class _Clock, class _Duration> 188 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); 189 190 void unlock(); 191 192 _LIBCPP_INLINE_VISIBILITY | 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; --- 28 unchanged lines hidden (view full) --- 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 |
193 void swap(unique_lock& __u) | 199 void swap(unique_lock& __u) _NOEXCEPT |
194 { 195 _VSTD::swap(__m_, __u.__m_); 196 _VSTD::swap(__owns_, __u.__owns_); 197 } 198 _LIBCPP_INLINE_VISIBILITY | 200 { 201 _VSTD::swap(__m_, __u.__m_); 202 _VSTD::swap(__owns_, __u.__owns_); 203 } 204 _LIBCPP_INLINE_VISIBILITY |
199 mutex_type* release() | 205 mutex_type* release() _NOEXCEPT |
200 { 201 mutex_type* __m = __m_; 202 __m_ = nullptr; 203 __owns_ = false; 204 return __m; 205 } 206 207 _LIBCPP_INLINE_VISIBILITY | 206 { 207 mutex_type* __m = __m_; 208 __m_ = nullptr; 209 __owns_ = false; 210 return __m; 211 } 212 213 _LIBCPP_INLINE_VISIBILITY |
208 bool owns_lock() const {return __owns_;} | 214 bool owns_lock() const _NOEXCEPT {return __owns_;} |
209 _LIBCPP_INLINE_VISIBILITY 210 _LIBCPP_EXPLICIT | 215 _LIBCPP_INLINE_VISIBILITY 216 _LIBCPP_EXPLICIT |
211 operator bool () const {return __owns_;} | 217 operator bool () const _NOEXCEPT {return __owns_;} |
212 _LIBCPP_INLINE_VISIBILITY | 218 _LIBCPP_INLINE_VISIBILITY |
213 mutex_type* mutex() const {return __m_;} | 219 mutex_type* mutex() const _NOEXCEPT {return __m_;} |
214}; 215 216template <class _Mutex> 217void 218unique_lock<_Mutex>::lock() 219{ 220 if (__m_ == nullptr) 221 __throw_system_error(EPERM, "unique_lock::lock: references null mutex"); --- 49 unchanged lines hidden (view full) --- 271 __throw_system_error(EPERM, "unique_lock::unlock: not locked"); 272 __m_->unlock(); 273 __owns_ = false; 274} 275 276template <class _Mutex> 277inline _LIBCPP_INLINE_VISIBILITY 278void | 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"); --- 49 unchanged lines hidden (view full) --- 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 |
279swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) {__x.swap(__y);} | 285swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT 286 {__x.swap(__y);} |
280 281struct _LIBCPP_VISIBLE cv_status 282{ 283 enum _ { 284 no_timeout, 285 timeout 286 }; 287 --- 4 unchanged lines hidden (view full) --- 292 293}; 294 295class _LIBCPP_VISIBLE condition_variable 296{ 297 pthread_cond_t __cv_; 298public: 299 _LIBCPP_INLINE_VISIBILITY | 287 288struct _LIBCPP_VISIBLE cv_status 289{ 290 enum _ { 291 no_timeout, 292 timeout 293 }; 294 --- 4 unchanged lines hidden (view full) --- 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 |
|
300 condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;} | 310 condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;} |
311#endif |
|
301 ~condition_variable(); 302 303private: 304 condition_variable(const condition_variable&); // = delete; 305 condition_variable& operator=(const condition_variable&); // = delete; 306 307public: | 312 ~condition_variable(); 313 314private: 315 condition_variable(const condition_variable&); // = delete; 316 condition_variable& operator=(const condition_variable&); // = delete; 317 318public: |
308 void notify_one(); 309 void notify_all(); | 319 void notify_one() _NOEXCEPT; 320 void notify_all() _NOEXCEPT; |
310 311 void wait(unique_lock<mutex>& __lk); 312 template <class _Predicate> 313 void wait(unique_lock<mutex>& __lk, _Predicate __pred); 314 | 321 322 void wait(unique_lock<mutex>& __lk); 323 template <class _Predicate> 324 void wait(unique_lock<mutex>& __lk, _Predicate __pred); 325 |
315 template <class _Duration> 316 cv_status 317 wait_until(unique_lock<mutex>& __lk, 318 const chrono::time_point<chrono::system_clock, _Duration>& __t); 319 | |
320 template <class _Clock, class _Duration> 321 cv_status 322 wait_until(unique_lock<mutex>& __lk, 323 const chrono::time_point<_Clock, _Duration>& __t); 324 325 template <class _Clock, class _Duration, class _Predicate> 326 bool 327 wait_until(unique_lock<mutex>& __lk, --- 38 unchanged lines hidden (view full) --- 366template <class _Predicate> 367void 368condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred) 369{ 370 while (!__pred()) 371 wait(__lk); 372} 373 | 326 template <class _Clock, class _Duration> 327 cv_status 328 wait_until(unique_lock<mutex>& __lk, 329 const chrono::time_point<_Clock, _Duration>& __t); 330 331 template <class _Clock, class _Duration, class _Predicate> 332 bool 333 wait_until(unique_lock<mutex>& __lk, --- 38 unchanged lines hidden (view full) --- 372template <class _Predicate> 373void 374condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred) 375{ 376 while (!__pred()) 377 wait(__lk); 378} 379 |
374template <class _Duration> 375cv_status 376condition_variable::wait_until(unique_lock<mutex>& __lk, 377 const chrono::time_point<chrono::system_clock, _Duration>& __t) 378{ 379 using namespace chrono; 380 typedef time_point<system_clock, nanoseconds> __nano_sys_tmpt; 381 __do_timed_wait(__lk, 382 __nano_sys_tmpt(__ceil<nanoseconds>(__t.time_since_epoch()))); 383 return system_clock::now() < __t ? cv_status::no_timeout : 384 cv_status::timeout; 385} 386 | |
387template <class _Clock, class _Duration> 388cv_status 389condition_variable::wait_until(unique_lock<mutex>& __lk, 390 const chrono::time_point<_Clock, _Duration>& __t) 391{ 392 using namespace chrono; | 380template <class _Clock, class _Duration> 381cv_status 382condition_variable::wait_until(unique_lock<mutex>& __lk, 383 const chrono::time_point<_Clock, _Duration>& __t) 384{ 385 using namespace chrono; |
393 system_clock::time_point __s_now = system_clock::now(); 394 typename _Clock::time_point __c_now = _Clock::now(); 395 __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__t - __c_now)); | 386 wait_for(__lk, __t - _Clock::now()); |
396 return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout; 397} 398 399template <class _Clock, class _Duration, class _Predicate> 400bool 401condition_variable::wait_until(unique_lock<mutex>& __lk, 402 const chrono::time_point<_Clock, _Duration>& __t, 403 _Predicate __pred) --- 7 unchanged lines hidden (view full) --- 411} 412 413template <class _Rep, class _Period> 414cv_status 415condition_variable::wait_for(unique_lock<mutex>& __lk, 416 const chrono::duration<_Rep, _Period>& __d) 417{ 418 using namespace chrono; | 387 return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout; 388} 389 390template <class _Clock, class _Duration, class _Predicate> 391bool 392condition_variable::wait_until(unique_lock<mutex>& __lk, 393 const chrono::time_point<_Clock, _Duration>& __t, 394 _Predicate __pred) --- 7 unchanged lines hidden (view full) --- 402} 403 404template <class _Rep, class _Period> 405cv_status 406condition_variable::wait_for(unique_lock<mutex>& __lk, 407 const chrono::duration<_Rep, _Period>& __d) 408{ 409 using namespace chrono; |
410 if (__d <= __d.zero()) 411 return cv_status::timeout; 412 typedef time_point<system_clock, duration<long double, nano> > __sys_tpf; 413 typedef time_point<system_clock, nanoseconds> __sys_tpi; 414 __sys_tpf _Max = __sys_tpi::max(); |
|
419 system_clock::time_point __s_now = system_clock::now(); 420 steady_clock::time_point __c_now = steady_clock::now(); | 415 system_clock::time_point __s_now = system_clock::now(); 416 steady_clock::time_point __c_now = steady_clock::now(); |
421 __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d)); | 417 if (_Max - __d > __s_now) 418 __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d)); 419 else 420 __do_timed_wait(__lk, __sys_tpi::max()); |
422 return steady_clock::now() - __c_now < __d ? cv_status::no_timeout : 423 cv_status::timeout; 424} 425 426template <class _Rep, class _Period, class _Predicate> 427inline _LIBCPP_INLINE_VISIBILITY 428bool 429condition_variable::wait_for(unique_lock<mutex>& __lk, 430 const chrono::duration<_Rep, _Period>& __d, 431 _Predicate __pred) 432{ 433 return wait_until(__lk, chrono::steady_clock::now() + __d, 434 _VSTD::move(__pred)); 435} 436 437_LIBCPP_END_NAMESPACE_STD 438 439#endif // _LIBCPP___MUTEX_BASE | 421 return steady_clock::now() - __c_now < __d ? cv_status::no_timeout : 422 cv_status::timeout; 423} 424 425template <class _Rep, class _Period, class _Predicate> 426inline _LIBCPP_INLINE_VISIBILITY 427bool 428condition_variable::wait_for(unique_lock<mutex>& __lk, 429 const chrono::duration<_Rep, _Period>& __d, 430 _Predicate __pred) 431{ 432 return wait_until(__lk, chrono::steady_clock::now() + __d, 433 _VSTD::move(__pred)); 434} 435 436_LIBCPP_END_NAMESPACE_STD 437 438#endif // _LIBCPP___MUTEX_BASE |