blob: 3f607271b9ebf0d7b1f2db128a2bd25f3146f38c [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===-------------------- condition_variable.cpp --------------------------===//
2//
Howard Hinnantc566dc32010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00004//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000010#include "__config"
11
12#ifndef _LIBCPP_HAS_NO_THREADS
13
Howard Hinnantc51e1022010-05-11 19:42:16 +000014#include "condition_variable"
15#include "thread"
16#include "system_error"
Howard Hinnantc51e1022010-05-11 19:42:16 +000017
18_LIBCPP_BEGIN_NAMESPACE_STD
19
20condition_variable::~condition_variable()
21{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000022 __libcpp_condvar_destroy(&__cv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000023}
24
25void
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +000026condition_variable::notify_one() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000027{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000028 __libcpp_condvar_signal(&__cv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000029}
30
31void
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +000032condition_variable::notify_all() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000033{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000034 __libcpp_condvar_broadcast(&__cv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000035}
36
37void
Marshall Clow126356b2014-03-26 02:45:04 +000038condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000039{
40 if (!lk.owns_lock())
41 __throw_system_error(EPERM,
42 "condition_variable::wait: mutex not locked");
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000043 int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
Howard Hinnantc51e1022010-05-11 19:42:16 +000044 if (ec)
45 __throw_system_error(ec, "condition_variable wait failed");
46}
47
48void
49condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
Marshall Clow126356b2014-03-26 02:45:04 +000050 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000051{
52 using namespace chrono;
53 if (!lk.owns_lock())
54 __throw_system_error(EPERM,
55 "condition_variable::timed wait: mutex not locked");
56 nanoseconds d = tp.time_since_epoch();
Howard Hinnant9ce8dc52012-08-30 19:14:33 +000057 if (d > nanoseconds(0x59682F000000E941))
58 d = nanoseconds(0x59682F000000E941);
Howard Hinnantc51e1022010-05-11 19:42:16 +000059 timespec ts;
60 seconds s = duration_cast<seconds>(d);
Howard Hinnant9ce8dc52012-08-30 19:14:33 +000061 typedef decltype(ts.tv_sec) ts_sec;
62 _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
63 if (s.count() < ts_sec_max)
64 {
65 ts.tv_sec = static_cast<ts_sec>(s.count());
66 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
67 }
68 else
69 {
70 ts.tv_sec = ts_sec_max;
71 ts.tv_nsec = giga::num - 1;
72 }
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000073 int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
Howard Hinnantc51e1022010-05-11 19:42:16 +000074 if (ec != 0 && ec != ETIMEDOUT)
75 __throw_system_error(ec, "condition_variable timed_wait failed");
76}
77
Howard Hinnante6a10852010-09-03 21:46:37 +000078void
79notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
80{
Eric Fiselier99245c52016-09-03 08:07:40 +000081 auto& tl_ptr = __thread_local_data();
82 // If this thread was not created using std::thread then it will not have
83 // previously allocated.
84 if (tl_ptr.get() == nullptr) {
85 tl_ptr.set_pointer(new __thread_struct);
86 }
Howard Hinnant15d55052010-10-14 19:18:04 +000087 __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
Howard Hinnante6a10852010-09-03 21:46:37 +000088}
89
Howard Hinnantc51e1022010-05-11 19:42:16 +000090_LIBCPP_END_NAMESPACE_STD
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000091
92#endif // !_LIBCPP_HAS_NO_THREADS