Last active
September 1, 2016 23:51
-
-
Save EricWF/a071376b1216aabdd1695eec2175c374 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/cxa_thread_atexit.cpp b/src/cxa_thread_atexit.cpp | |
index 3ca6c16..7aea753 100644 | |
--- a/src/cxa_thread_atexit.cpp | |
+++ b/src/cxa_thread_atexit.cpp | |
@@ -1,157 +1,137 @@ | |
//===----------------------- cxa_thread_atexit.cpp ------------------------===// | |
// | |
// 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. | |
// | |
//===----------------------------------------------------------------------===// | |
#include "abort_message.h" | |
#include "cxxabi.h" | |
#include <cstdlib> | |
#include <pthread.h> | |
+#include <cassert> | |
namespace __cxxabiv1 { | |
using Dtor = void(*)(void*); | |
extern "C" | |
#ifndef HAVE___CXA_THREAD_ATEXIT_IMPL | |
// A weak symbol is used to detect this function's presence in the C library | |
// at runtime, even if libc++ is built against an older libc | |
__attribute__((__weak__)) | |
#endif | |
int __cxa_thread_atexit_impl(Dtor, void*, void*); | |
#ifndef HAVE___CXA_THREAD_ATEXIT_IMPL | |
namespace { | |
// This implementation is used if the C library does not provide | |
// __cxa_thread_atexit_impl() for us. It has a number of limitations that are | |
// difficult to impossible to address without ..._impl(): | |
// | |
// - dso_symbol is ignored. This means that a shared library may be unloaded | |
// (via dlclose()) before its thread_local destructors have run. | |
// | |
// - thread_local destructors for the main thread are run by the destructor of | |
// a static object. This is later than expected; they should run before the | |
// destructors of any objects with static storage duration. | |
// | |
// - thread_local destructors on other threads run on the first iteration | |
// through the pthread_key destructors. std::notify_all_at_thread_exit() | |
// and similar functions must be careful to wait until the second iteration | |
// to provide their indended ordering guarantees. | |
// | |
// Another limitation, though one shared with ..._impl(), is that any | |
// thread_locals that are first initialized after non-thread_local global | |
// destructors begin to run will not be destroyed. [basic.start.term] states | |
// that all thread_local destructors are sequenced before the destruction of | |
// objects with static storage duration, resulting in a contradiction if a | |
// thread_local is constructed after that point. Thus we consider such | |
// programs ill-formed, and don't bother to run those destructors. (If the | |
// program terminates abnormally after such a thread_local is constructed, | |
// the destructor is not expected to run and thus there is no contradiction. | |
// So construction still has to work.) | |
struct DtorList { | |
Dtor dtor; | |
void* obj; | |
DtorList* next; | |
}; | |
pthread_key_t dtors; | |
+ __thread DtorList* list_head = nullptr; | |
+ __thread bool thread_alive = false; | |
void run_dtors(void* ptr) { | |
- // pthread_key destructors run after the key is set to nullptr, but we need | |
- // to restore it here to get the right ordering if __cxa_thread_atexit() is | |
- // called during the loop. | |
- if (pthread_setspecific(dtors, ptr) != 0) { | |
- abort_message("pthread_setspecific() failed during thread_local destruction"); | |
- } | |
- | |
- auto list = static_cast<DtorList**>(ptr); | |
- | |
- while (auto head = *list) { | |
- *list = head->next; | |
+ assert(&list_head == static_cast<DtorList**>(ptr)); | |
+ while (auto head = list_head) { | |
+ list_head = head->next; | |
head->dtor(head->obj); | |
std::free(head); | |
} | |
- | |
- std::free(list); | |
- | |
- // Set the pthread_key to nullptr (or else the destructor would run again) | |
- if (pthread_setspecific(dtors, nullptr) != 0) { | |
- abort_message("pthread_setspecific() failed during thread_local destruction"); | |
- } | |
+ thread_alive = false; | |
} | |
struct DtorsManager { | |
DtorsManager() { | |
// There is intentionally no matching pthread_key_delete call, as | |
// __cxa_thread_atexit() may be called arbitrarily late (for example, from | |
// global destructors or atexit() handlers). | |
if (pthread_key_create(&dtors, run_dtors) != 0) { | |
abort_message("pthread_key_create() failed in __cxa_thread_atexit()"); | |
} | |
} | |
~DtorsManager() { | |
// pthread_key destructors do not run on threads that call exit() | |
// (including when the main thread returns from main()), so we explicitly | |
// call the destructor here. This runs at exit time (potentially earlier | |
// if libc++abi is dlclose()'d). Any thread_locals initialized after this | |
// point will not be destroyed. | |
auto ptr = pthread_getspecific(dtors); | |
if (ptr) { | |
run_dtors(ptr); | |
} | |
} | |
}; | |
} // namespace | |
#endif // HAVE__CXA_THREAD_ATEXIT_IMPL | |
extern "C" { | |
_LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(Dtor dtor, void* obj, void* dso_symbol) throw() { | |
#ifdef HAVE___CXA_THREAD_ATEXIT_IMPL | |
return __cxa_thread_atexit_impl(dtor, obj, dso_symbol); | |
#else | |
if (__cxa_thread_atexit_impl) { | |
return __cxa_thread_atexit_impl(dtor, obj, dso_symbol); | |
} else { | |
// Initialize the dtors pthread_key (uses __cxa_guard_*() for one-time | |
// initialization and __cxa_atexit() for destruction) | |
static DtorsManager manager; | |
- auto list = static_cast<DtorList**>(pthread_getspecific(dtors)); | |
- if (!list) { | |
- list = static_cast<DtorList**>(std::malloc(sizeof(DtorList*))); | |
- if (!list) { | |
+ if (!thread_alive && pthread_setspecific(dtors, &list_head) != 0) | |
return -1; | |
- } | |
- *list = nullptr; | |
- if (pthread_setspecific(dtors, list) != 0) { | |
- std::free(list); | |
- return -1; | |
- } | |
- } | |
+ thread_alive = true; | |
auto head = static_cast<DtorList*>(std::malloc(sizeof(DtorList))); | |
if (!head) { | |
return -1; | |
} | |
head->dtor = dtor; | |
head->obj = obj; | |
- head->next = *list; | |
- *list = head; | |
+ head->next = list_head; | |
+ list_head = head; | |
return 0; | |
} | |
#endif // HAVE___CXA_THREAD_ATEXIT_IMPL | |
} | |
} // extern "C" | |
} // namespace __cxxabiv1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment