1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
981ad6265SDimitry Andric #include <condition_variable>
1081ad6265SDimitry Andric #include <thread>
110b57cec5SDimitry Andric
12480093f4SDimitry Andric #if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
130b57cec5SDimitry Andric # pragma comment(lib, "pthread")
140b57cec5SDimitry Andric #endif
150b57cec5SDimitry Andric
1681ad6265SDimitry Andric _LIBCPP_PUSH_MACROS
1781ad6265SDimitry Andric #include <__undef_macros>
1881ad6265SDimitry Andric
190b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric // ~condition_variable is defined elsewhere.
220b57cec5SDimitry Andric
notify_one()23*e710425bSDimitry Andric void condition_variable::notify_one() noexcept { __libcpp_condvar_signal(&__cv_); }
240b57cec5SDimitry Andric
notify_all()25*e710425bSDimitry Andric void condition_variable::notify_all() noexcept { __libcpp_condvar_broadcast(&__cv_); }
260b57cec5SDimitry Andric
wait(unique_lock<mutex> & lk)27*e710425bSDimitry Andric void condition_variable::wait(unique_lock<mutex>& lk) noexcept {
280b57cec5SDimitry Andric if (!lk.owns_lock())
29*e710425bSDimitry Andric __throw_system_error(EPERM, "condition_variable::wait: mutex not locked");
300b57cec5SDimitry Andric int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
310b57cec5SDimitry Andric if (ec)
320b57cec5SDimitry Andric __throw_system_error(ec, "condition_variable wait failed");
330b57cec5SDimitry Andric }
340b57cec5SDimitry Andric
__do_timed_wait(unique_lock<mutex> & lk,chrono::time_point<chrono::system_clock,chrono::nanoseconds> tp)35*e710425bSDimitry Andric void condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
36*e710425bSDimitry Andric chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) noexcept {
370b57cec5SDimitry Andric using namespace chrono;
380b57cec5SDimitry Andric if (!lk.owns_lock())
39*e710425bSDimitry Andric __throw_system_error(EPERM, "condition_variable::timed wait: mutex not locked");
400b57cec5SDimitry Andric nanoseconds d = tp.time_since_epoch();
410b57cec5SDimitry Andric if (d > nanoseconds(0x59682F000000E941))
420b57cec5SDimitry Andric d = nanoseconds(0x59682F000000E941);
430b57cec5SDimitry Andric __libcpp_timespec_t ts;
440b57cec5SDimitry Andric seconds s = duration_cast<seconds>(d);
450b57cec5SDimitry Andric typedef decltype(ts.tv_sec) ts_sec;
46c9157d92SDimitry Andric constexpr ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
47*e710425bSDimitry Andric if (s.count() < ts_sec_max) {
480b57cec5SDimitry Andric ts.tv_sec = static_cast<ts_sec>(s.count());
490b57cec5SDimitry Andric ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
50*e710425bSDimitry Andric } else {
510b57cec5SDimitry Andric ts.tv_sec = ts_sec_max;
520b57cec5SDimitry Andric ts.tv_nsec = giga::num - 1;
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
550b57cec5SDimitry Andric if (ec != 0 && ec != ETIMEDOUT)
560b57cec5SDimitry Andric __throw_system_error(ec, "condition_variable timed_wait failed");
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric
notify_all_at_thread_exit(condition_variable & cond,unique_lock<mutex> lk)59*e710425bSDimitry Andric void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk) {
600b57cec5SDimitry Andric auto& tl_ptr = __thread_local_data();
610b57cec5SDimitry Andric // If this thread was not created using std::thread then it will not have
620b57cec5SDimitry Andric // previously allocated.
630b57cec5SDimitry Andric if (tl_ptr.get() == nullptr) {
640b57cec5SDimitry Andric tl_ptr.set_pointer(new __thread_struct);
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
700b57cec5SDimitry Andric
7181ad6265SDimitry Andric _LIBCPP_POP_MACROS
72