1 //===-------------------- condition_variable.cpp --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #ifndef _LIBCPP_HAS_NO_THREADS
14 #include "condition_variable"
16 #include "system_error"
18 _LIBCPP_BEGIN_NAMESPACE_STD
20 condition_variable::~condition_variable()
22 __libcpp_condvar_destroy(&__cv_);
26 condition_variable::notify_one() _NOEXCEPT
28 __libcpp_condvar_signal(&__cv_);
32 condition_variable::notify_all() _NOEXCEPT
34 __libcpp_condvar_broadcast(&__cv_);
38 condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT
41 __throw_system_error(EPERM,
42 "condition_variable::wait: mutex not locked");
43 int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
45 __throw_system_error(ec, "condition_variable wait failed");
49 condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
50 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT
52 using namespace chrono;
54 __throw_system_error(EPERM,
55 "condition_variable::timed wait: mutex not locked");
56 nanoseconds d = tp.time_since_epoch();
57 if (d > nanoseconds(0x59682F000000E941))
58 d = nanoseconds(0x59682F000000E941);
60 seconds s = duration_cast<seconds>(d);
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)
65 ts.tv_sec = static_cast<ts_sec>(s.count());
66 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
70 ts.tv_sec = ts_sec_max;
71 ts.tv_nsec = giga::num - 1;
73 int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
74 if (ec != 0 && ec != ETIMEDOUT)
75 __throw_system_error(ec, "condition_variable timed_wait failed");
79 notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
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);
87 __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
90 _LIBCPP_END_NAMESPACE_STD
92 #endif // !_LIBCPP_HAS_NO_THREADS