blob: e9b373965e165f3fac603b7fb5066adcea7a411e [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===-------------------- condition_variable.cpp --------------------------===//
2//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +00009#include "__config"
10
11#ifndef _LIBCPP_HAS_NO_THREADS
12
Howard Hinnantc51e1022010-05-11 19:42:16 +000013#include "condition_variable"
14#include "thread"
15#include "system_error"
Eric Fiselierf4433a32017-05-31 22:07:49 +000016#include "__undef_macros"
Howard Hinnantc51e1022010-05-11 19:42:16 +000017
Petr Hosek7a89a9c2019-05-30 06:57:27 +000018#if defined(__unix__) && defined(__ELF__) && defined(_LIBCPP_HAS_COMMENT_LIB_PRAGMA)
Petr Hosek99575aa2019-05-30 01:34:41 +000019#pragma comment(lib, "pthread")
20#endif
21
Howard Hinnantc51e1022010-05-11 19:42:16 +000022_LIBCPP_BEGIN_NAMESPACE_STD
23
24condition_variable::~condition_variable()
25{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000026 __libcpp_condvar_destroy(&__cv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000027}
28
29void
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +000030condition_variable::notify_one() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000031{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000032 __libcpp_condvar_signal(&__cv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000033}
34
35void
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +000036condition_variable::notify_all() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000037{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000038 __libcpp_condvar_broadcast(&__cv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000039}
40
41void
Marshall Clow126356b2014-03-26 02:45:04 +000042condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000043{
44 if (!lk.owns_lock())
45 __throw_system_error(EPERM,
46 "condition_variable::wait: mutex not locked");
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000047 int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
Howard Hinnantc51e1022010-05-11 19:42:16 +000048 if (ec)
49 __throw_system_error(ec, "condition_variable wait failed");
50}
51
52void
53condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
Marshall Clow126356b2014-03-26 02:45:04 +000054 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000055{
56 using namespace chrono;
57 if (!lk.owns_lock())
58 __throw_system_error(EPERM,
59 "condition_variable::timed wait: mutex not locked");
60 nanoseconds d = tp.time_since_epoch();
Howard Hinnant9ce8dc52012-08-30 19:14:33 +000061 if (d > nanoseconds(0x59682F000000E941))
62 d = nanoseconds(0x59682F000000E941);
Mikhail Maltsevee7a4a02019-06-21 08:33:47 +000063 __libcpp_timespec_t ts;
Howard Hinnantc51e1022010-05-11 19:42:16 +000064 seconds s = duration_cast<seconds>(d);
Howard Hinnant9ce8dc52012-08-30 19:14:33 +000065 typedef decltype(ts.tv_sec) ts_sec;
66 _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
67 if (s.count() < ts_sec_max)
68 {
69 ts.tv_sec = static_cast<ts_sec>(s.count());
70 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
71 }
72 else
73 {
74 ts.tv_sec = ts_sec_max;
75 ts.tv_nsec = giga::num - 1;
76 }
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000077 int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
Howard Hinnantc51e1022010-05-11 19:42:16 +000078 if (ec != 0 && ec != ETIMEDOUT)
79 __throw_system_error(ec, "condition_variable timed_wait failed");
80}
81
Howard Hinnante6a10852010-09-03 21:46:37 +000082void
83notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
84{
Eric Fiselier99245c52016-09-03 08:07:40 +000085 auto& tl_ptr = __thread_local_data();
86 // If this thread was not created using std::thread then it will not have
87 // previously allocated.
88 if (tl_ptr.get() == nullptr) {
89 tl_ptr.set_pointer(new __thread_struct);
90 }
Howard Hinnant15d55052010-10-14 19:18:04 +000091 __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
Howard Hinnante6a10852010-09-03 21:46:37 +000092}
93
Howard Hinnantc51e1022010-05-11 19:42:16 +000094_LIBCPP_END_NAMESPACE_STD
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000095
96#endif // !_LIBCPP_HAS_NO_THREADS