blob: b8426c66fde643c7ef8bab7ef117ca77d936e10e [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161//===-------------------- condition_variable.cpp --------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:403// 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 Hinnant3e519522010-05-11 19:42:166//
7//===----------------------------------------------------------------------===//
8
Jonathan Roelofsb3fcc672014-09-05 19:45:059#include "__config"
10
11#ifndef _LIBCPP_HAS_NO_THREADS
12
Howard Hinnant3e519522010-05-11 19:42:1613#include "condition_variable"
14#include "thread"
15#include "system_error"
Eric Fiseliera016efb2017-05-31 22:07:4916#include "__undef_macros"
Howard Hinnant3e519522010-05-11 19:42:1617
Petr Hosekb19977d2019-05-30 06:57:2718#if defined(__unix__) && defined(__ELF__) && defined(_LIBCPP_HAS_COMMENT_LIB_PRAGMA)
Petr Hosek996e62e2019-05-30 01:34:4119#pragma comment(lib, "pthread")
20#endif
21
Howard Hinnant3e519522010-05-11 19:42:1622_LIBCPP_BEGIN_NAMESPACE_STD
23
Eric Fiselier8cedf042019-07-07 17:24:0324// ~condition_variable is defined elsewhere.
Howard Hinnant3e519522010-05-11 19:42:1625
26void
Howard Hinnant45c663d2012-07-21 16:32:5327condition_variable::notify_one() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:1628{
Asiri Rathnayakec7e42392016-05-06 14:06:2929 __libcpp_condvar_signal(&__cv_);
Howard Hinnant3e519522010-05-11 19:42:1630}
31
32void
Howard Hinnant45c663d2012-07-21 16:32:5333condition_variable::notify_all() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:1634{
Asiri Rathnayakec7e42392016-05-06 14:06:2935 __libcpp_condvar_broadcast(&__cv_);
Howard Hinnant3e519522010-05-11 19:42:1636}
37
38void
Marshall Clow1641a7c2014-03-26 02:45:0439condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:1640{
41 if (!lk.owns_lock())
42 __throw_system_error(EPERM,
43 "condition_variable::wait: mutex not locked");
Asiri Rathnayakec7e42392016-05-06 14:06:2944 int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
Howard Hinnant3e519522010-05-11 19:42:1645 if (ec)
46 __throw_system_error(ec, "condition_variable wait failed");
47}
48
49void
50condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
Marshall Clow1641a7c2014-03-26 02:45:0451 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:1652{
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 Hinnantaad745a2012-08-30 19:14:3358 if (d > nanoseconds(0x59682F000000E941))
59 d = nanoseconds(0x59682F000000E941);
Mikhail Maltsevcfdc7f02019-06-21 08:33:4760 __libcpp_timespec_t ts;
Howard Hinnant3e519522010-05-11 19:42:1661 seconds s = duration_cast<seconds>(d);
Howard Hinnantaad745a2012-08-30 19:14:3362 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 }
Asiri Rathnayakec7e42392016-05-06 14:06:2974 int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
Howard Hinnant3e519522010-05-11 19:42:1675 if (ec != 0 && ec != ETIMEDOUT)
76 __throw_system_error(ec, "condition_variable timed_wait failed");
77}
78
Howard Hinnantb77c0c02010-09-03 21:46:3779void
80notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
81{
Eric Fiselierff94d252016-09-03 08:07:4082 auto& tl_ptr = __thread_local_data();
83 // If this thread was not created using std::thread then it will not have
84 // previously allocated.
85 if (tl_ptr.get() == nullptr) {
86 tl_ptr.set_pointer(new __thread_struct);
87 }
Howard Hinnant10e4a482010-10-14 19:18:0488 __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
Howard Hinnantb77c0c02010-09-03 21:46:3789}
90
Howard Hinnant3e519522010-05-11 19:42:1691_LIBCPP_END_NAMESPACE_STD
Jonathan Roelofsb3fcc672014-09-05 19:45:0592
93#endif // !_LIBCPP_HAS_NO_THREADS