Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 1 | //===-------------------- condition_variable.cpp --------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 9 | #include "__config" |
| 10 | |
| 11 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 12 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 13 | #include "condition_variable" |
| 14 | #include "thread" |
| 15 | #include "system_error" |
Eric Fiselier | a016efb | 2017-05-31 22:07:49 | [diff] [blame] | 16 | #include "__undef_macros" |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 17 | |
| 18 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 19 | |
| 20 | condition_variable::~condition_variable() |
| 21 | { |
Asiri Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 22 | __libcpp_condvar_destroy(&__cv_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | void |
Howard Hinnant | 45c663d | 2012-07-21 16:32:53 | [diff] [blame] | 26 | condition_variable::notify_one() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 27 | { |
Asiri Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 28 | __libcpp_condvar_signal(&__cv_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void |
Howard Hinnant | 45c663d | 2012-07-21 16:32:53 | [diff] [blame] | 32 | condition_variable::notify_all() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 33 | { |
Asiri Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 34 | __libcpp_condvar_broadcast(&__cv_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void |
Marshall Clow | 1641a7c | 2014-03-26 02:45:04 | [diff] [blame] | 38 | condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 39 | { |
| 40 | if (!lk.owns_lock()) |
| 41 | __throw_system_error(EPERM, |
| 42 | "condition_variable::wait: mutex not locked"); |
Asiri Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 43 | int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 44 | if (ec) |
| 45 | __throw_system_error(ec, "condition_variable wait failed"); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | condition_variable::__do_timed_wait(unique_lock<mutex>& lk, |
Marshall Clow | 1641a7c | 2014-03-26 02:45:04 | [diff] [blame] | 50 | chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 51 | { |
| 52 | using namespace chrono; |
| 53 | if (!lk.owns_lock()) |
| 54 | __throw_system_error(EPERM, |
| 55 | "condition_variable::timed wait: mutex not locked"); |
| 56 | nanoseconds d = tp.time_since_epoch(); |
Howard Hinnant | aad745a | 2012-08-30 19:14:33 | [diff] [blame] | 57 | if (d > nanoseconds(0x59682F000000E941)) |
| 58 | d = nanoseconds(0x59682F000000E941); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 59 | timespec ts; |
| 60 | seconds s = duration_cast<seconds>(d); |
Howard Hinnant | aad745a | 2012-08-30 19:14:33 | [diff] [blame] | 61 | typedef decltype(ts.tv_sec) ts_sec; |
| 62 | _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max(); |
| 63 | if (s.count() < ts_sec_max) |
| 64 | { |
| 65 | ts.tv_sec = static_cast<ts_sec>(s.count()); |
| 66 | ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count()); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | ts.tv_sec = ts_sec_max; |
| 71 | ts.tv_nsec = giga::num - 1; |
| 72 | } |
Asiri Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 73 | int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 74 | if (ec != 0 && ec != ETIMEDOUT) |
| 75 | __throw_system_error(ec, "condition_variable timed_wait failed"); |
| 76 | } |
| 77 | |
Howard Hinnant | b77c0c0 | 2010-09-03 21:46:37 | [diff] [blame] | 78 | void |
| 79 | notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk) |
| 80 | { |
Eric Fiselier | ff94d25 | 2016-09-03 08:07:40 | [diff] [blame] | 81 | auto& tl_ptr = __thread_local_data(); |
| 82 | // If this thread was not created using std::thread then it will not have |
| 83 | // previously allocated. |
| 84 | if (tl_ptr.get() == nullptr) { |
| 85 | tl_ptr.set_pointer(new __thread_struct); |
| 86 | } |
Howard Hinnant | 10e4a48 | 2010-10-14 19:18:04 | [diff] [blame] | 87 | __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release()); |
Howard Hinnant | b77c0c0 | 2010-09-03 21:46:37 | [diff] [blame] | 88 | } |
| 89 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 90 | _LIBCPP_END_NAMESPACE_STD |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 91 | |
| 92 | #endif // !_LIBCPP_HAS_NO_THREADS |