mirror of
https://github.com/darlinghq/darling-libcxx.git
synced 2024-11-28 06:10:24 +00:00
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 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@280588 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
5e5eb6348d
commit
7f735c31c0
@ -99,6 +99,7 @@ void sleep_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
#include <tuple>
|
||||
#endif
|
||||
#include <__threading_support>
|
||||
#include <__debug>
|
||||
|
||||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
||||
#pragma GCC system_header
|
||||
@ -159,8 +160,7 @@ public:
|
||||
pointer operator*() const {return *get();}
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
pointer operator->() const {return get();}
|
||||
pointer release();
|
||||
void reset(pointer __p = nullptr);
|
||||
void set_pointer(pointer __p);
|
||||
};
|
||||
|
||||
template <class _Tp>
|
||||
@ -190,22 +190,13 @@ __thread_specific_ptr<_Tp>::~__thread_specific_ptr()
|
||||
// thread local data. For this reason we leak the key.
|
||||
}
|
||||
|
||||
template <class _Tp>
|
||||
typename __thread_specific_ptr<_Tp>::pointer
|
||||
__thread_specific_ptr<_Tp>::release()
|
||||
{
|
||||
pointer __p = get();
|
||||
__libcpp_tl_set(__key_, nullptr);
|
||||
return __p;
|
||||
}
|
||||
|
||||
template <class _Tp>
|
||||
void
|
||||
__thread_specific_ptr<_Tp>::reset(pointer __p)
|
||||
__thread_specific_ptr<_Tp>::set_pointer(pointer __p)
|
||||
{
|
||||
pointer __p_old = get();
|
||||
_LIBCPP_ASSERT(get() == nullptr,
|
||||
"Attempting to overwrite thread local data");
|
||||
__libcpp_tl_set(__key_, __p);
|
||||
delete __p_old;
|
||||
}
|
||||
|
||||
class _LIBCPP_TYPE_VIS thread;
|
||||
@ -351,7 +342,7 @@ void* __thread_proxy(void* __vp)
|
||||
{
|
||||
// _Fp = std::tuple< unique_ptr<__thread_struct>, Functor, Args...>
|
||||
std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
|
||||
__thread_local_data().reset(_VSTD::get<0>(*__p).release());
|
||||
__thread_local_data().set_pointer(_VSTD::get<0>(*__p).release());
|
||||
typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
|
||||
__thread_execute(*__p, _Index());
|
||||
return nullptr;
|
||||
@ -392,7 +383,7 @@ template <class _Fp>
|
||||
void* __thread_proxy_cxx03(void* __vp)
|
||||
{
|
||||
std::unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
|
||||
__thread_local_data().reset(__p->__tsp_.release());
|
||||
__thread_local_data().set_pointer(__p->__tsp_.release());
|
||||
(__p->__fn_)();
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -79,6 +79,12 @@ condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
|
||||
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());
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,75 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// UNSUPPORTED: libcpp-has-no-threads
|
||||
|
||||
// notify_all_at_thread_exit(...) requires move semantics to transfer the
|
||||
// unique_lock.
|
||||
// UNSUPPORTED: c++98, c++03
|
||||
|
||||
// <condition_variable>
|
||||
|
||||
// void
|
||||
// notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
||||
|
||||
// Test that this function works with threads that were not created by
|
||||
// std::thread. See http://llvm.org/PR30202.
|
||||
|
||||
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <cassert>
|
||||
#include <pthread.h>
|
||||
|
||||
std::condition_variable cv;
|
||||
std::mutex mut;
|
||||
bool exited = false;
|
||||
|
||||
typedef std::chrono::milliseconds ms;
|
||||
typedef std::chrono::high_resolution_clock Clock;
|
||||
|
||||
void* func(void*)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mut);
|
||||
std::notify_all_at_thread_exit(cv, std::move(lk));
|
||||
std::this_thread::sleep_for(ms(300));
|
||||
exited = true;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mut);
|
||||
pthread_t id;
|
||||
int res = pthread_create(&id, 0, &func, nullptr);
|
||||
assert(res == 0);
|
||||
Clock::time_point t0 = Clock::now();
|
||||
assert(exited == false);
|
||||
cv.wait(lk);
|
||||
Clock::time_point t1 = Clock::now();
|
||||
assert(exited);
|
||||
assert(t1-t0 > ms(250));
|
||||
pthread_join(id, 0);
|
||||
}
|
||||
exited = false;
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(mut);
|
||||
std::thread t(&func, nullptr);
|
||||
Clock::time_point t0 = Clock::now();
|
||||
assert(exited == false);
|
||||
cv.wait(lk);
|
||||
Clock::time_point t1 = Clock::now();
|
||||
assert(exited);
|
||||
assert(t1-t0 > ms(250));
|
||||
t.join();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user