| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include <condition_variable> |
| 10 | #include <limits> |
| 11 | #include <ratio> |
| 12 | #include <thread> |
| 13 | #include <__chrono/duration.h> |
| 14 | #include <__chrono/system_clock.h> |
| 15 | #include <__chrono/time_point.h> |
| 16 | #include <__system_error/throw_system_error.h> |
| 17 | |
| 18 | #if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB) |
| 19 | # pragma comment(lib, "pthread") |
| 20 | #endif |
| 21 | |
| 22 | _LIBCPP_PUSH_MACROS |
| 23 | #include <__undef_macros> |
| 24 | |
| 25 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 26 | |
| 27 | // ~condition_variable is defined elsewhere. |
| 28 | |
| 29 | void condition_variable::notify_one() noexcept { __libcpp_condvar_signal(cv: &__cv_); } |
| 30 | |
| 31 | void condition_variable::notify_all() noexcept { __libcpp_condvar_broadcast(cv: &__cv_); } |
| 32 | |
| 33 | void condition_variable::wait(unique_lock<mutex>& lk) noexcept { |
| 34 | if (!lk.owns_lock()) |
| 35 | std::__throw_system_error(EPERM, what_arg: "condition_variable::wait: mutex not locked" ); |
| 36 | int ec = __libcpp_condvar_wait(cv: &__cv_, m: lk.mutex()->native_handle()); |
| 37 | if (ec) |
| 38 | std::__throw_system_error(ev: ec, what_arg: "condition_variable wait failed" ); |
| 39 | } |
| 40 | |
| 41 | void condition_variable::__do_timed_wait(unique_lock<mutex>& lk, |
| 42 | chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) noexcept { |
| 43 | using namespace chrono; |
| 44 | if (!lk.owns_lock()) |
| 45 | std::__throw_system_error(EPERM, what_arg: "condition_variable::timed wait: mutex not locked" ); |
| 46 | nanoseconds d = tp.time_since_epoch(); |
| 47 | if (d > nanoseconds(0x59682F000000E941)) |
| 48 | d = nanoseconds(0x59682F000000E941); |
| 49 | __libcpp_timespec_t ts; |
| 50 | seconds s = duration_cast<seconds>(fd: d); |
| 51 | typedef decltype(ts.tv_sec) ts_sec; |
| 52 | constexpr ts_sec ts_sec_max = numeric_limits<ts_sec>::max(); |
| 53 | if (s.count() < ts_sec_max) { |
| 54 | ts.tv_sec = static_cast<ts_sec>(s.count()); |
| 55 | ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count()); |
| 56 | } else { |
| 57 | ts.tv_sec = ts_sec_max; |
| 58 | ts.tv_nsec = giga::num - 1; |
| 59 | } |
| 60 | int ec = __libcpp_condvar_timedwait(cv: &__cv_, m: lk.mutex()->native_handle(), ts: &ts); |
| 61 | if (ec != 0 && ec != ETIMEDOUT) |
| 62 | std::__throw_system_error(ev: ec, what_arg: "condition_variable timed_wait failed" ); |
| 63 | } |
| 64 | |
| 65 | void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk) { |
| 66 | auto& tl_ptr = __thread_local_data(); |
| 67 | // If this thread was not created using std::thread then it will not have |
| 68 | // previously allocated. |
| 69 | if (tl_ptr.get() == nullptr) { |
| 70 | tl_ptr.set_pointer(new __thread_struct); |
| 71 | } |
| 72 | __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release()); |
| 73 | } |
| 74 | |
| 75 | _LIBCPP_END_NAMESPACE_STD |
| 76 | |
| 77 | _LIBCPP_POP_MACROS |
| 78 | |