blob: 5fd5fc891c16032dc7aa514a7b4e7f6fdb39822b [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
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +000010#include "__config"
11
12#ifndef _LIBCPP_HAS_NO_THREADS
13
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000014#include "condition_variable"
15#include "thread"
16#include "system_error"
17#include "cassert"
18
19_LIBCPP_BEGIN_NAMESPACE_STD
20
21condition_variable::~condition_variable()
22{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070023 pthread_cond_destroy(&__cv_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024}
25
26void
Howard Hinnantc8f74132012-07-21 16:32:53 +000027condition_variable::notify_one() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070029 pthread_cond_signal(&__cv_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030}
31
32void
Howard Hinnantc8f74132012-07-21 16:32:53 +000033condition_variable::notify_all() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070035 pthread_cond_broadcast(&__cv_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000036}
37
38void
Marshall Clowb0767852014-03-26 02:45:04 +000039condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040{
41 if (!lk.owns_lock())
42 __throw_system_error(EPERM,
43 "condition_variable::wait: mutex not locked");
Dan Albert1d4a1ed2016-05-25 22:36:09 -070044 int ec = pthread_cond_wait(&__cv_, lk.mutex()->native_handle());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045 if (ec)
46 __throw_system_error(ec, "condition_variable wait failed");
47}
48
49void
50condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
Marshall Clowb0767852014-03-26 02:45:04 +000051 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052{
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();
Howard Hinnantcf115d22012-08-30 19:14:33 +000058 if (d > nanoseconds(0x59682F000000E941))
59 d = nanoseconds(0x59682F000000E941);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 timespec ts;
61 seconds s = duration_cast<seconds>(d);
Howard Hinnantcf115d22012-08-30 19:14:33 +000062 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)
65 {
66 ts.tv_sec = static_cast<ts_sec>(s.count());
67 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
68 }
69 else
70 {
71 ts.tv_sec = ts_sec_max;
72 ts.tv_nsec = giga::num - 1;
73 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070074 int ec = pthread_cond_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075 if (ec != 0 && ec != ETIMEDOUT)
76 __throw_system_error(ec, "condition_variable timed_wait failed");
77}
78
Howard Hinnante6e4d012010-09-03 21:46:37 +000079void
80notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
81{
Howard Hinnant5306d682010-10-14 19:18:04 +000082 __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
Howard Hinnante6e4d012010-09-03 21:46:37 +000083}
84
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085_LIBCPP_END_NAMESPACE_STD
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +000086
87#endif // !_LIBCPP_HAS_NO_THREADS