blob: 061d1388d3ef9277bc35c2fcc11b66d1c4e36c4d [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===-------------------- condition_variable.cpp --------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "condition_variable"
11#include "thread"
12#include "system_error"
13#include "cassert"
14
15_LIBCPP_BEGIN_NAMESPACE_STD
16
17condition_variable::~condition_variable()
18{
Howard Hinnantec3773c2011-12-01 20:21:04 +000019 pthread_cond_destroy(&__cv_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000020}
21
22void
Howard Hinnantc8f74132012-07-21 16:32:53 +000023condition_variable::notify_one() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024{
25 pthread_cond_signal(&__cv_);
26}
27
28void
Howard Hinnantc8f74132012-07-21 16:32:53 +000029condition_variable::notify_all() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030{
31 pthread_cond_broadcast(&__cv_);
32}
33
34void
Marshall Clowb0767852014-03-26 02:45:04 +000035condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000036{
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");
43}
44
45void
46condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
Marshall Clowb0767852014-03-26 02:45:04 +000047 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048{
49 using namespace chrono;
50 if (!lk.owns_lock())
51 __throw_system_error(EPERM,
52 "condition_variable::timed wait: mutex not locked");
53 nanoseconds d = tp.time_since_epoch();
Howard Hinnantcf115d22012-08-30 19:14:33 +000054 if (d > nanoseconds(0x59682F000000E941))
55 d = nanoseconds(0x59682F000000E941);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056 timespec ts;
57 seconds s = duration_cast<seconds>(d);
Howard Hinnantcf115d22012-08-30 19:14:33 +000058 typedef decltype(ts.tv_sec) ts_sec;
59 _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
60 if (s.count() < ts_sec_max)
61 {
62 ts.tv_sec = static_cast<ts_sec>(s.count());
63 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
64 }
65 else
66 {
67 ts.tv_sec = ts_sec_max;
68 ts.tv_nsec = giga::num - 1;
69 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070 int ec = pthread_cond_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
71 if (ec != 0 && ec != ETIMEDOUT)
72 __throw_system_error(ec, "condition_variable timed_wait failed");
73}
74
Howard Hinnante6e4d012010-09-03 21:46:37 +000075void
76notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
77{
Howard Hinnant5306d682010-10-14 19:18:04 +000078 __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
Howard Hinnante6e4d012010-09-03 21:46:37 +000079}
80
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000081_LIBCPP_END_NAMESPACE_STD