llvm-project/libcxx/src/condition_variable.cpp
James Y Knight c3656afdf1
Revert "[libc++] Introduce ABI sensitive areas to avoid requiring _LIBCPP_HIDE_FROM_ABI everywhere (#131156)" (#141756)
This reverts commit c861fe8a71e64f3d2108c58147e7375cd9314521.

Unfortunately, this use of hidden visibility attributes causes
user-defined specializations of standard-library types to also be marked
hidden by default, which is incorrect. See discussion thread on #131156.

...and also reverts the follow-up commits:

Revert "[libc++] Add explicit ABI annotations to functions from the block runtime declared in <__functional/function.h> (#140592)"
This reverts commit 3e4c9dc299c35155934688184319d391b298fff7.

Revert "[libc++] Make ABI annotations explicit for windows-specific code (#140507)"
This reverts commit f73287e623a6c2e4a3485832bc3e10860cd26eb5.

Revert "[libc++][NFC] Replace a few "namespace std" with the correct macro (#140510)"
This reverts commit 1d411f27c769a32cb22ce50b9dc4421e34fd40dd.
2025-05-28 12:04:51 -04:00

78 lines
2.7 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <condition_variable>
#include <limits>
#include <ratio>
#include <thread>
#include <__chrono/duration.h>
#include <__chrono/system_clock.h>
#include <__chrono/time_point.h>
#include <__system_error/throw_system_error.h>
#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
# pragma comment(lib, "pthread")
#endif
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
_LIBCPP_BEGIN_NAMESPACE_STD
// ~condition_variable is defined elsewhere.
void condition_variable::notify_one() noexcept { __libcpp_condvar_signal(&__cv_); }
void condition_variable::notify_all() noexcept { __libcpp_condvar_broadcast(&__cv_); }
void condition_variable::wait(unique_lock<mutex>& lk) noexcept {
if (!lk.owns_lock())
std::__throw_system_error(EPERM, "condition_variable::wait: mutex not locked");
int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
if (ec)
std::__throw_system_error(ec, "condition_variable wait failed");
}
void condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) noexcept {
using namespace chrono;
if (!lk.owns_lock())
std::__throw_system_error(EPERM, "condition_variable::timed wait: mutex not locked");
nanoseconds d = tp.time_since_epoch();
if (d > nanoseconds(0x59682F000000E941))
d = nanoseconds(0x59682F000000E941);
__libcpp_timespec_t ts;
seconds s = duration_cast<seconds>(d);
typedef decltype(ts.tv_sec) ts_sec;
constexpr ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
if (s.count() < ts_sec_max) {
ts.tv_sec = static_cast<ts_sec>(s.count());
ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
} else {
ts.tv_sec = ts_sec_max;
ts.tv_nsec = giga::num - 1;
}
int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
if (ec != 0 && ec != ETIMEDOUT)
std::__throw_system_error(ec, "condition_variable timed_wait failed");
}
void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk) {
auto& tl_ptr = __thread_local_data();
// If this thread was not created using std::thread then it will not have
// previously allocated.
if (tl_ptr.get() == nullptr) {
tl_ptr.set_pointer(new __thread_struct);
}
__thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
}
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS