Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===-------------------- condition_variable.cpp --------------------------===// |
| 2 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [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 | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | #include "__config" |
| 10 | |
| 11 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 12 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 13 | #include "condition_variable" |
| 14 | #include "thread" |
| 15 | #include "system_error" |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 16 | #include "__undef_macros" |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 17 | |
Petr Hosek | 99575aa | 2019-05-30 01:34:41 +0000 | [diff] [blame^] | 18 | #if defined(__unix__) && defined(__ELF__) && defined(__clang__) |
| 19 | #pragma comment(lib, "pthread") |
| 20 | #endif |
| 21 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 23 | |
| 24 | condition_variable::~condition_variable() |
| 25 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 26 | __libcpp_condvar_destroy(&__cv_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void |
Howard Hinnant | 0fb1b0a | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 30 | condition_variable::notify_one() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 32 | __libcpp_condvar_signal(&__cv_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void |
Howard Hinnant | 0fb1b0a | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 36 | condition_variable::notify_all() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 37 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 38 | __libcpp_condvar_broadcast(&__cv_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void |
Marshall Clow | 126356b | 2014-03-26 02:45:04 +0000 | [diff] [blame] | 42 | condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | { |
| 44 | if (!lk.owns_lock()) |
| 45 | __throw_system_error(EPERM, |
| 46 | "condition_variable::wait: mutex not locked"); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 47 | int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | if (ec) |
| 49 | __throw_system_error(ec, "condition_variable wait failed"); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | condition_variable::__do_timed_wait(unique_lock<mutex>& lk, |
Marshall Clow | 126356b | 2014-03-26 02:45:04 +0000 | [diff] [blame] | 54 | chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 55 | { |
| 56 | using namespace chrono; |
| 57 | if (!lk.owns_lock()) |
| 58 | __throw_system_error(EPERM, |
| 59 | "condition_variable::timed wait: mutex not locked"); |
| 60 | nanoseconds d = tp.time_since_epoch(); |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 61 | if (d > nanoseconds(0x59682F000000E941)) |
| 62 | d = nanoseconds(0x59682F000000E941); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 63 | timespec ts; |
| 64 | seconds s = duration_cast<seconds>(d); |
Howard Hinnant | 9ce8dc5 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 65 | typedef decltype(ts.tv_sec) ts_sec; |
| 66 | _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max(); |
| 67 | if (s.count() < ts_sec_max) |
| 68 | { |
| 69 | ts.tv_sec = static_cast<ts_sec>(s.count()); |
| 70 | ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count()); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | ts.tv_sec = ts_sec_max; |
| 75 | ts.tv_nsec = giga::num - 1; |
| 76 | } |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 77 | int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 78 | if (ec != 0 && ec != ETIMEDOUT) |
| 79 | __throw_system_error(ec, "condition_variable timed_wait failed"); |
| 80 | } |
| 81 | |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 82 | void |
| 83 | notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk) |
| 84 | { |
Eric Fiselier | 99245c5 | 2016-09-03 08:07:40 +0000 | [diff] [blame] | 85 | auto& tl_ptr = __thread_local_data(); |
| 86 | // If this thread was not created using std::thread then it will not have |
| 87 | // previously allocated. |
| 88 | if (tl_ptr.get() == nullptr) { |
| 89 | tl_ptr.set_pointer(new __thread_struct); |
| 90 | } |
Howard Hinnant | 15d5505 | 2010-10-14 19:18:04 +0000 | [diff] [blame] | 91 | __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release()); |
Howard Hinnant | e6a1085 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 94 | _LIBCPP_END_NAMESPACE_STD |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 95 | |
| 96 | #endif // !_LIBCPP_HAS_NO_THREADS |