etc/services - sync with NetBSD-8
[minix.git] / external / bsd / libc++ / dist / libcxx / src / condition_variable.cpp
blob5fd5fc891c16032dc7aa514a7b4e7f6fdb39822b
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 "__config"
12 #ifndef _LIBCPP_HAS_NO_THREADS
14 #include "condition_variable"
15 #include "thread"
16 #include "system_error"
17 #include "cassert"
19 _LIBCPP_BEGIN_NAMESPACE_STD
21 condition_variable::~condition_variable()
23 pthread_cond_destroy(&__cv_);
26 void
27 condition_variable::notify_one() _NOEXCEPT
29 pthread_cond_signal(&__cv_);
32 void
33 condition_variable::notify_all() _NOEXCEPT
35 pthread_cond_broadcast(&__cv_);
38 void
39 condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT
41 if (!lk.owns_lock())
42 __throw_system_error(EPERM,
43 "condition_variable::wait: mutex not locked");
44 int ec = pthread_cond_wait(&__cv_, lk.mutex()->native_handle());
45 if (ec)
46 __throw_system_error(ec, "condition_variable wait failed");
49 void
50 condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
51 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT
53 using namespace chrono;
54 if (!lk.owns_lock())
55 __throw_system_error(EPERM,
56 "condition_variable::timed wait: mutex not locked");
57 nanoseconds d = tp.time_since_epoch();
58 if (d > nanoseconds(0x59682F000000E941))
59 d = nanoseconds(0x59682F000000E941);
60 timespec ts;
61 seconds s = duration_cast<seconds>(d);
62 typedef decltype(ts.tv_sec) ts_sec;
63 _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
64 if (s.count() < ts_sec_max)
66 ts.tv_sec = static_cast<ts_sec>(s.count());
67 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
69 else
71 ts.tv_sec = ts_sec_max;
72 ts.tv_nsec = giga::num - 1;
74 int ec = pthread_cond_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
75 if (ec != 0 && ec != ETIMEDOUT)
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
87 #endif // !_LIBCPP_HAS_NO_THREADS