1*58b9f456SAndroid Build Coastguard Worker //===-------------------- condition_variable.cpp --------------------------===//
2*58b9f456SAndroid Build Coastguard Worker //
3*58b9f456SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*58b9f456SAndroid Build Coastguard Worker //
5*58b9f456SAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*58b9f456SAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*58b9f456SAndroid Build Coastguard Worker //
8*58b9f456SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*58b9f456SAndroid Build Coastguard Worker
10*58b9f456SAndroid Build Coastguard Worker #include "__config"
11*58b9f456SAndroid Build Coastguard Worker
12*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_HAS_NO_THREADS
13*58b9f456SAndroid Build Coastguard Worker
14*58b9f456SAndroid Build Coastguard Worker #include "condition_variable"
15*58b9f456SAndroid Build Coastguard Worker #include "thread"
16*58b9f456SAndroid Build Coastguard Worker #include "system_error"
17*58b9f456SAndroid Build Coastguard Worker #include "__undef_macros"
18*58b9f456SAndroid Build Coastguard Worker
19*58b9f456SAndroid Build Coastguard Worker _LIBCPP_BEGIN_NAMESPACE_STD
20*58b9f456SAndroid Build Coastguard Worker
~condition_variable()21*58b9f456SAndroid Build Coastguard Worker condition_variable::~condition_variable()
22*58b9f456SAndroid Build Coastguard Worker {
23*58b9f456SAndroid Build Coastguard Worker __libcpp_condvar_destroy(&__cv_);
24*58b9f456SAndroid Build Coastguard Worker }
25*58b9f456SAndroid Build Coastguard Worker
26*58b9f456SAndroid Build Coastguard Worker void
notify_one()27*58b9f456SAndroid Build Coastguard Worker condition_variable::notify_one() _NOEXCEPT
28*58b9f456SAndroid Build Coastguard Worker {
29*58b9f456SAndroid Build Coastguard Worker __libcpp_condvar_signal(&__cv_);
30*58b9f456SAndroid Build Coastguard Worker }
31*58b9f456SAndroid Build Coastguard Worker
32*58b9f456SAndroid Build Coastguard Worker void
notify_all()33*58b9f456SAndroid Build Coastguard Worker condition_variable::notify_all() _NOEXCEPT
34*58b9f456SAndroid Build Coastguard Worker {
35*58b9f456SAndroid Build Coastguard Worker __libcpp_condvar_broadcast(&__cv_);
36*58b9f456SAndroid Build Coastguard Worker }
37*58b9f456SAndroid Build Coastguard Worker
38*58b9f456SAndroid Build Coastguard Worker void
wait(unique_lock<mutex> & lk)39*58b9f456SAndroid Build Coastguard Worker condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT
40*58b9f456SAndroid Build Coastguard Worker {
41*58b9f456SAndroid Build Coastguard Worker if (!lk.owns_lock())
42*58b9f456SAndroid Build Coastguard Worker __throw_system_error(EPERM,
43*58b9f456SAndroid Build Coastguard Worker "condition_variable::wait: mutex not locked");
44*58b9f456SAndroid Build Coastguard Worker int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
45*58b9f456SAndroid Build Coastguard Worker if (ec)
46*58b9f456SAndroid Build Coastguard Worker __throw_system_error(ec, "condition_variable wait failed");
47*58b9f456SAndroid Build Coastguard Worker }
48*58b9f456SAndroid Build Coastguard Worker
49*58b9f456SAndroid Build Coastguard Worker void
__do_timed_wait(unique_lock<mutex> & lk,chrono::time_point<chrono::system_clock,chrono::nanoseconds> tp)50*58b9f456SAndroid Build Coastguard Worker condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
51*58b9f456SAndroid Build Coastguard Worker chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT
52*58b9f456SAndroid Build Coastguard Worker {
53*58b9f456SAndroid Build Coastguard Worker using namespace chrono;
54*58b9f456SAndroid Build Coastguard Worker if (!lk.owns_lock())
55*58b9f456SAndroid Build Coastguard Worker __throw_system_error(EPERM,
56*58b9f456SAndroid Build Coastguard Worker "condition_variable::timed wait: mutex not locked");
57*58b9f456SAndroid Build Coastguard Worker nanoseconds d = tp.time_since_epoch();
58*58b9f456SAndroid Build Coastguard Worker if (d > nanoseconds(0x59682F000000E941))
59*58b9f456SAndroid Build Coastguard Worker d = nanoseconds(0x59682F000000E941);
60*58b9f456SAndroid Build Coastguard Worker timespec ts;
61*58b9f456SAndroid Build Coastguard Worker seconds s = duration_cast<seconds>(d);
62*58b9f456SAndroid Build Coastguard Worker typedef decltype(ts.tv_sec) ts_sec;
63*58b9f456SAndroid Build Coastguard Worker _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
64*58b9f456SAndroid Build Coastguard Worker if (s.count() < ts_sec_max)
65*58b9f456SAndroid Build Coastguard Worker {
66*58b9f456SAndroid Build Coastguard Worker ts.tv_sec = static_cast<ts_sec>(s.count());
67*58b9f456SAndroid Build Coastguard Worker ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
68*58b9f456SAndroid Build Coastguard Worker }
69*58b9f456SAndroid Build Coastguard Worker else
70*58b9f456SAndroid Build Coastguard Worker {
71*58b9f456SAndroid Build Coastguard Worker ts.tv_sec = ts_sec_max;
72*58b9f456SAndroid Build Coastguard Worker ts.tv_nsec = giga::num - 1;
73*58b9f456SAndroid Build Coastguard Worker }
74*58b9f456SAndroid Build Coastguard Worker int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
75*58b9f456SAndroid Build Coastguard Worker if (ec != 0 && ec != ETIMEDOUT)
76*58b9f456SAndroid Build Coastguard Worker __throw_system_error(ec, "condition_variable timed_wait failed");
77*58b9f456SAndroid Build Coastguard Worker }
78*58b9f456SAndroid Build Coastguard Worker
79*58b9f456SAndroid Build Coastguard Worker void
notify_all_at_thread_exit(condition_variable & cond,unique_lock<mutex> lk)80*58b9f456SAndroid Build Coastguard Worker notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
81*58b9f456SAndroid Build Coastguard Worker {
82*58b9f456SAndroid Build Coastguard Worker auto& tl_ptr = __thread_local_data();
83*58b9f456SAndroid Build Coastguard Worker // If this thread was not created using std::thread then it will not have
84*58b9f456SAndroid Build Coastguard Worker // previously allocated.
85*58b9f456SAndroid Build Coastguard Worker if (tl_ptr.get() == nullptr) {
86*58b9f456SAndroid Build Coastguard Worker tl_ptr.set_pointer(new __thread_struct);
87*58b9f456SAndroid Build Coastguard Worker }
88*58b9f456SAndroid Build Coastguard Worker __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
89*58b9f456SAndroid Build Coastguard Worker }
90*58b9f456SAndroid Build Coastguard Worker
91*58b9f456SAndroid Build Coastguard Worker _LIBCPP_END_NAMESPACE_STD
92*58b9f456SAndroid Build Coastguard Worker
93*58b9f456SAndroid Build Coastguard Worker #endif // !_LIBCPP_HAS_NO_THREADS
94