blob: b17c4cf6458e1c4cc0ec9febd3310e716d33e434 [file] [log] [blame]
Louis Dionne9bd93882021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00002//
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
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -05009#include <__config>
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000010
11#ifndef _LIBCPP_HAS_NO_THREADS
12
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050013#include <condition_variable>
14#include <thread>
15#include <system_error>
Howard Hinnantc51e1022010-05-11 19:42:16 +000016
Michał Górny8d676fb2019-12-02 11:49:20 +010017#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050018# pragma comment(lib, "pthread")
Petr Hosek99575aa2019-05-30 01:34:41 +000019#endif
20
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050021_LIBCPP_PUSH_MACROS
22#include <__undef_macros>
23
Howard Hinnantc51e1022010-05-11 19:42:16 +000024_LIBCPP_BEGIN_NAMESPACE_STD
25
Eric Fiselier256466e2019-07-07 17:24:03 +000026// ~condition_variable is defined elsewhere.
Howard Hinnantc51e1022010-05-11 19:42:16 +000027
28void
Louis Dionne65358e12021-03-01 12:09:45 -050029condition_variable::notify_one() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000030{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000031 __libcpp_condvar_signal(&__cv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000032}
33
34void
Louis Dionne65358e12021-03-01 12:09:45 -050035condition_variable::notify_all() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000036{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000037 __libcpp_condvar_broadcast(&__cv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000038}
39
40void
Louis Dionne65358e12021-03-01 12:09:45 -050041condition_variable::wait(unique_lock<mutex>& lk) noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000042{
43 if (!lk.owns_lock())
44 __throw_system_error(EPERM,
45 "condition_variable::wait: mutex not locked");
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000046 int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 if (ec)
48 __throw_system_error(ec, "condition_variable wait failed");
49}
50
51void
52condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
Louis Dionne65358e12021-03-01 12:09:45 -050053 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000054{
55 using namespace chrono;
56 if (!lk.owns_lock())
57 __throw_system_error(EPERM,
58 "condition_variable::timed wait: mutex not locked");
59 nanoseconds d = tp.time_since_epoch();
Howard Hinnant9ce8dc52012-08-30 19:14:33 +000060 if (d > nanoseconds(0x59682F000000E941))
61 d = nanoseconds(0x59682F000000E941);
Mikhail Maltsevee7a4a02019-06-21 08:33:47 +000062 __libcpp_timespec_t ts;
Howard Hinnantc51e1022010-05-11 19:42:16 +000063 seconds s = duration_cast<seconds>(d);
Howard Hinnant9ce8dc52012-08-30 19:14:33 +000064 typedef decltype(ts.tv_sec) ts_sec;
65 _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
66 if (s.count() < ts_sec_max)
67 {
68 ts.tv_sec = static_cast<ts_sec>(s.count());
69 ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
70 }
71 else
72 {
73 ts.tv_sec = ts_sec_max;
74 ts.tv_nsec = giga::num - 1;
75 }
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000076 int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
Howard Hinnantc51e1022010-05-11 19:42:16 +000077 if (ec != 0 && ec != ETIMEDOUT)
78 __throw_system_error(ec, "condition_variable timed_wait failed");
79}
80
Howard Hinnante6a10852010-09-03 21:46:37 +000081void
82notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
83{
Eric Fiselier99245c52016-09-03 08:07:40 +000084 auto& tl_ptr = __thread_local_data();
85 // If this thread was not created using std::thread then it will not have
86 // previously allocated.
87 if (tl_ptr.get() == nullptr) {
88 tl_ptr.set_pointer(new __thread_struct);
89 }
Howard Hinnant15d55052010-10-14 19:18:04 +000090 __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
Howard Hinnante6a10852010-09-03 21:46:37 +000091}
92
Howard Hinnantc51e1022010-05-11 19:42:16 +000093_LIBCPP_END_NAMESPACE_STD
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000094
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050095_LIBCPP_POP_MACROS
96
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000097#endif // !_LIBCPP_HAS_NO_THREADS