Louis Dionne | eb8650a | 2021-11-17 21:25:01 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [diff] [blame] | 3 | // 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 Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame^] | 9 | #include <__config> |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 10 | |
| 11 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 12 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame^] | 13 | #include <condition_variable> |
| 14 | #include <thread> |
| 15 | #include <system_error> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 16 | |
Michał Górny | a9b5fff | 2019-12-02 10:49:20 | [diff] [blame] | 17 | #if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB) |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame^] | 18 | # pragma comment(lib, "pthread") |
Petr Hosek | 996e62e | 2019-05-30 01:34:41 | [diff] [blame] | 19 | #endif |
| 20 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame^] | 21 | _LIBCPP_PUSH_MACROS |
| 22 | #include <__undef_macros> |
| 23 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 24 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 25 | |
Eric Fiselier | 8cedf04 | 2019-07-07 17:24:03 | [diff] [blame] | 26 | // ~condition_variable is defined elsewhere. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 27 | |
| 28 | void |
Louis Dionne | 5601305 | 2021-03-01 17:09:45 | [diff] [blame] | 29 | condition_variable::notify_one() noexcept |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 30 | { |
Asiri Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 31 | __libcpp_condvar_signal(&__cv_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void |
Louis Dionne | 5601305 | 2021-03-01 17:09:45 | [diff] [blame] | 35 | condition_variable::notify_all() noexcept |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 36 | { |
Asiri Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 37 | __libcpp_condvar_broadcast(&__cv_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void |
Louis Dionne | 5601305 | 2021-03-01 17:09:45 | [diff] [blame] | 41 | condition_variable::wait(unique_lock<mutex>& lk) noexcept |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 42 | { |
| 43 | if (!lk.owns_lock()) |
| 44 | __throw_system_error(EPERM, |
| 45 | "condition_variable::wait: mutex not locked"); |
Asiri Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 46 | int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 47 | if (ec) |
| 48 | __throw_system_error(ec, "condition_variable wait failed"); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | condition_variable::__do_timed_wait(unique_lock<mutex>& lk, |
Louis Dionne | 5601305 | 2021-03-01 17:09:45 | [diff] [blame] | 53 | chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) noexcept |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 54 | { |
| 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 Hinnant | aad745a | 2012-08-30 19:14:33 | [diff] [blame] | 60 | if (d > nanoseconds(0x59682F000000E941)) |
| 61 | d = nanoseconds(0x59682F000000E941); |
Mikhail Maltsev | cfdc7f0 | 2019-06-21 08:33:47 | [diff] [blame] | 62 | __libcpp_timespec_t ts; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 63 | seconds s = duration_cast<seconds>(d); |
Howard Hinnant | aad745a | 2012-08-30 19:14:33 | [diff] [blame] | 64 | 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 Rathnayake | c7e4239 | 2016-05-06 14:06:29 | [diff] [blame] | 76 | int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 77 | if (ec != 0 && ec != ETIMEDOUT) |
| 78 | __throw_system_error(ec, "condition_variable timed_wait failed"); |
| 79 | } |
| 80 | |
Howard Hinnant | b77c0c0 | 2010-09-03 21:46:37 | [diff] [blame] | 81 | void |
| 82 | notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk) |
| 83 | { |
Eric Fiselier | ff94d25 | 2016-09-03 08:07:40 | [diff] [blame] | 84 | 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 Hinnant | 10e4a48 | 2010-10-14 19:18:04 | [diff] [blame] | 90 | __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release()); |
Howard Hinnant | b77c0c0 | 2010-09-03 21:46:37 | [diff] [blame] | 91 | } |
| 92 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 | [diff] [blame] | 93 | _LIBCPP_END_NAMESPACE_STD |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 94 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame^] | 95 | _LIBCPP_POP_MACROS |
| 96 | |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 97 | #endif // !_LIBCPP_HAS_NO_THREADS |