tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / libc++ / dist / libcxx / src / condition_variable.cpp
blob3ed861da96a1c23f914e8c174ab9d2a11e0b890e
1 //===-------------------- condition_variable.cpp --------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "condition_variable"
11 #include "thread"
12 #include "system_error"
13 #include "cassert"
15 _LIBCPP_BEGIN_NAMESPACE_STD
17 condition_variable::~condition_variable()
19 pthread_cond_destroy(&__cv_);
22 void
23 condition_variable::notify_one() _NOEXCEPT
25 pthread_cond_signal(&__cv_);
28 void
29 condition_variable::notify_all() _NOEXCEPT
31 pthread_cond_broadcast(&__cv_);
34 void
35 condition_variable::wait(unique_lock<mutex>& lk)
37 if (!lk.owns_lock())
38 __throw_system_error(EPERM,
39 "condition_variable::wait: mutex not locked");
40 int ec = pthread_cond_wait(&__cv_, lk.mutex()->native_handle());
41 if (ec)
42 __throw_system_error(ec, "condition_variable wait failed");
45 void
46 condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
47 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp)
49 #if !defined(__minix) /* LSC: FIXME a loop with yield plus sleep? */
50 using namespace chrono;
51 if (!lk.owns_lock())
52 __throw_system_error(EPERM,
53 "condition_variable::timed wait: mutex not locked");
54 nanoseconds d = tp.time_since_epoch();
55 if (d > nanoseconds(0x59682F000000E941))
56 d = nanoseconds(0x59682F000000E941);
57 timespec ts;
58 seconds s = duration_cast<seconds>(d);
59 typedef decltype(ts.tv_sec) ts_sec;
60 _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
61 if (s.count() < ts_sec_max)
63 ts.tv_sec = static_cast<ts_sec>(s.count());
64 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
66 else
68 ts.tv_sec = ts_sec_max;
69 ts.tv_nsec = giga::num - 1;
71 int ec = pthread_cond_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
72 if (ec != 0 && ec != ETIMEDOUT)
73 #else
74 int ec = EINVAL;
75 #endif /* !defined(__minix) */
76 __throw_system_error(ec, "condition_variable timed_wait failed");
79 void
80 notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
82 __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
85 _LIBCPP_END_NAMESPACE_STD