Louis Dionne | eb8650a | 2021-11-17 21:25:01 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 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 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 9 | #include <condition_variable> |
| 10 | #include <thread> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 11 | |
Michał Górny | a9b5fff | 2019-12-02 10:49:20 | [diff] [blame] | 12 | #if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB) |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 13 | # pragma comment(lib, "pthread") |
Petr Hosek | 996e62e | 2019-05-30 01:34:41 | [diff] [blame] | 14 | #endif |
| 15 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 16 | _LIBCPP_PUSH_MACROS |
| 17 | #include <__undef_macros> |
| 18 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 19 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 20 | |
Eric Fiselier | 8cedf04 | 2019-07-07 17:24:03 | [diff] [blame] | 21 | // ~condition_variable is defined elsewhere. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 22 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame^] | 23 | void condition_variable::notify_one() noexcept { __libcpp_condvar_signal(&__cv_); } |
| 24 | |
| 25 | void condition_variable::notify_all() noexcept { __libcpp_condvar_broadcast(&__cv_); } |
| 26 | |
| 27 | void condition_variable::wait(unique_lock<mutex>& lk) noexcept { |
| 28 | if (!lk.owns_lock()) |
| 29 | __throw_system_error(EPERM, "condition_variable::wait: mutex not locked"); |
| 30 | int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle()); |
| 31 | if (ec) |
| 32 | __throw_system_error(ec, "condition_variable wait failed"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 33 | } |
| 34 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame^] | 35 | void condition_variable::__do_timed_wait(unique_lock<mutex>& lk, |
| 36 | chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) noexcept { |
| 37 | using namespace chrono; |
| 38 | if (!lk.owns_lock()) |
| 39 | __throw_system_error(EPERM, "condition_variable::timed wait: mutex not locked"); |
| 40 | nanoseconds d = tp.time_since_epoch(); |
| 41 | if (d > nanoseconds(0x59682F000000E941)) |
| 42 | d = nanoseconds(0x59682F000000E941); |
| 43 | __libcpp_timespec_t ts; |
| 44 | seconds s = duration_cast<seconds>(d); |
| 45 | typedef decltype(ts.tv_sec) ts_sec; |
| 46 | constexpr ts_sec ts_sec_max = numeric_limits<ts_sec>::max(); |
| 47 | if (s.count() < ts_sec_max) { |
| 48 | ts.tv_sec = static_cast<ts_sec>(s.count()); |
| 49 | ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count()); |
| 50 | } else { |
| 51 | ts.tv_sec = ts_sec_max; |
| 52 | ts.tv_nsec = giga::num - 1; |
| 53 | } |
| 54 | int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts); |
| 55 | if (ec != 0 && ec != ETIMEDOUT) |
| 56 | __throw_system_error(ec, "condition_variable timed_wait failed"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 57 | } |
| 58 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame^] | 59 | void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk) { |
| 60 | auto& tl_ptr = __thread_local_data(); |
| 61 | // If this thread was not created using std::thread then it will not have |
| 62 | // previously allocated. |
| 63 | if (tl_ptr.get() == nullptr) { |
| 64 | tl_ptr.set_pointer(new __thread_struct); |
| 65 | } |
| 66 | __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release()); |
Howard Hinnant | b77c0c0 | 2010-09-03 21:46:37 | [diff] [blame] | 67 | } |
| 68 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 69 | _LIBCPP_END_NAMESPACE_STD |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 70 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 71 | _LIBCPP_POP_MACROS |