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