2010-05-12 03:42:16 +08:00
|
|
|
//===-------------------- condition_variable.cpp --------------------------===//
|
|
|
|
//
|
2010-05-12 05:36:01 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
2010-11-17 06:09:02 +08:00
|
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
// Source Licenses. See LICENSE.TXT for details.
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-09-06 03:45:05 +08:00
|
|
|
#include "__config"
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_HAS_NO_THREADS
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
#include "condition_variable"
|
|
|
|
#include "thread"
|
|
|
|
#include "system_error"
|
2017-06-01 06:07:49 +08:00
|
|
|
#include "__undef_macros"
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
condition_variable::~condition_variable()
|
|
|
|
{
|
2016-05-06 22:06:29 +08:00
|
|
|
__libcpp_condvar_destroy(&__cv_);
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-07-22 00:32:53 +08:00
|
|
|
condition_variable::notify_one() _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2016-05-06 22:06:29 +08:00
|
|
|
__libcpp_condvar_signal(&__cv_);
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-07-22 00:32:53 +08:00
|
|
|
condition_variable::notify_all() _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2016-05-06 22:06:29 +08:00
|
|
|
__libcpp_condvar_broadcast(&__cv_);
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-26 10:45:04 +08:00
|
|
|
condition_variable::wait(unique_lock<mutex>& lk) _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
if (!lk.owns_lock())
|
|
|
|
__throw_system_error(EPERM,
|
|
|
|
"condition_variable::wait: mutex not locked");
|
2016-05-06 22:06:29 +08:00
|
|
|
int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
|
2010-05-12 03:42:16 +08:00
|
|
|
if (ec)
|
|
|
|
__throw_system_error(ec, "condition_variable wait failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
|
2014-03-26 10:45:04 +08:00
|
|
|
chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) _NOEXCEPT
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
using namespace chrono;
|
|
|
|
if (!lk.owns_lock())
|
|
|
|
__throw_system_error(EPERM,
|
|
|
|
"condition_variable::timed wait: mutex not locked");
|
|
|
|
nanoseconds d = tp.time_since_epoch();
|
2012-08-31 03:14:33 +08:00
|
|
|
if (d > nanoseconds(0x59682F000000E941))
|
|
|
|
d = nanoseconds(0x59682F000000E941);
|
2010-05-12 03:42:16 +08:00
|
|
|
timespec ts;
|
|
|
|
seconds s = duration_cast<seconds>(d);
|
2012-08-31 03:14:33 +08:00
|
|
|
typedef decltype(ts.tv_sec) ts_sec;
|
|
|
|
_LIBCPP_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;
|
|
|
|
}
|
2016-05-06 22:06:29 +08:00
|
|
|
int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
|
2010-05-12 03:42:16 +08:00
|
|
|
if (ec != 0 && ec != ETIMEDOUT)
|
|
|
|
__throw_system_error(ec, "condition_variable timed_wait failed");
|
|
|
|
}
|
|
|
|
|
2010-09-04 05:46:37 +08:00
|
|
|
void
|
|
|
|
notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
|
|
|
|
{
|
Fix PR30202 - notify_all_at_thread_exit seg faults if run from a raw pthread context.
Summary:
This patch allows threads not created using `std::thread` to use `std::notify_all_at_thread_exit` by ensuring the TL state has been initialized within `std::notify_all_at_thread_exit`.
Additionally this patch "fixes" a potential oddity in `__thread_local_pointer::reset(pointer)`, which would previously delete the old thread local data. However there should *never* be old thread local data because pthread *should* null it out on thread exit. Unfortunately it's possible that pthread failed to do this according to the spec:
>
> Upon key creation, the value NULL shall be associated with the new key in all active threads. Upon thread creation, the value NULL shall be associated with all defined keys in the new thread.
>
> An optional destructor function may be associated with each key value. At thread exit, if a key value has a non-NULL destructor pointer, and the thread has a non-NULL value associated with that key, the value of the key is set to NULL, and then the function pointed to is called with the previously associated value as its sole argument. The order of destructor calls is unspecified if more than one destructor exists for a thread when it exits.
>
> If, after all the destructors have been called for all non-NULL values with associated destructors, there are still some non-NULL values with associated destructors, then the process is repeated. If, after at least {PTHREAD_DESTRUCTOR_ITERATIONS} iterations of destructor calls for outstanding non-NULL values, there are still some non-NULL values with associated destructors, implementations may stop calling destructors, or they may continue calling destructors until no non-NULL values with associated destructors exist, even though this might result in an infinite loop.
However if pthread fails to delete the value it is probably incorrect for us to do it. Destroying the value performs all of the "at thread exit" actions registered with it but we are way past "at thread exit".
Reviewers: mclow.lists, bcraig, EricWF
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D24159
llvm-svn: 280588
2016-09-03 16:07:40 +08:00
|
|
|
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);
|
|
|
|
}
|
2010-10-15 03:18:04 +08:00
|
|
|
__thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
|
2010-09-04 05:46:37 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
2014-09-06 03:45:05 +08:00
|
|
|
|
|
|
|
#endif // !_LIBCPP_HAS_NO_THREADS
|