Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===-------------------- condition_variable.cpp --------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 10 | #include "__config" |
| 11 | |
| 12 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 13 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | #include "condition_variable" |
| 15 | #include "thread" |
| 16 | #include "system_error" |
| 17 | #include "cassert" |
| 18 | |
| 19 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 20 | |
| 21 | condition_variable::~condition_variable() |
| 22 | { |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 23 | pthread_cond_destroy(&__cv_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 27 | condition_variable::notify_one() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | { |
| 29 | pthread_cond_signal(&__cv_); |
| 30 | } |
| 31 | |
| 32 | void |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 33 | condition_variable::notify_all() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 34 | { |
| 35 | pthread_cond_broadcast(&__cv_); |
| 36 | } |
| 37 | |
| 38 | void |
Marshall Clow | b076785 | 2014-03-26 02:45:04 +0000 | [diff] [blame] | 39 | condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | { |
| 41 | if (!lk.owns_lock()) |
| 42 | __throw_system_error(EPERM, |
| 43 | "condition_variable::wait: mutex not locked"); |
| 44 | int ec = pthread_cond_wait(&__cv_, lk.mutex()->native_handle()); |
| 45 | if (ec) |
| 46 | __throw_system_error(ec, "condition_variable wait failed"); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | condition_variable::__do_timed_wait(unique_lock<mutex>& lk, |
Marshall Clow | b076785 | 2014-03-26 02:45:04 +0000 | [diff] [blame] | 51 | chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 52 | { |
| 53 | using namespace chrono; |
| 54 | if (!lk.owns_lock()) |
| 55 | __throw_system_error(EPERM, |
| 56 | "condition_variable::timed wait: mutex not locked"); |
| 57 | nanoseconds d = tp.time_since_epoch(); |
Howard Hinnant | cf115d2 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 58 | if (d > nanoseconds(0x59682F000000E941)) |
| 59 | d = nanoseconds(0x59682F000000E941); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | timespec ts; |
| 61 | seconds s = duration_cast<seconds>(d); |
Howard Hinnant | cf115d2 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 62 | typedef decltype(ts.tv_sec) ts_sec; |
| 63 | _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max(); |
| 64 | if (s.count() < ts_sec_max) |
| 65 | { |
| 66 | ts.tv_sec = static_cast<ts_sec>(s.count()); |
| 67 | ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count()); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | ts.tv_sec = ts_sec_max; |
| 72 | ts.tv_nsec = giga::num - 1; |
| 73 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 74 | int ec = pthread_cond_timedwait(&__cv_, lk.mutex()->native_handle(), &ts); |
| 75 | if (ec != 0 && ec != ETIMEDOUT) |
| 76 | __throw_system_error(ec, "condition_variable timed_wait failed"); |
| 77 | } |
| 78 | |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 79 | void |
| 80 | notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk) |
| 81 | { |
Howard Hinnant | 5306d68 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 82 | __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release()); |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 85 | _LIBCPP_END_NAMESPACE_STD |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 86 | |
| 87 | #endif // !_LIBCPP_HAS_NO_THREADS |