[PATCH] D21803: [libcxxabi] Provide a fallback __cxa_thread_atexit() implementation

Ben Craig via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 29 06:50:09 PDT 2016

bcraig added subscribers: rmaprath, bcraig.
bcraig added a comment.

You should look at __thread_specific_ptr in libcxx's <thread>.  It does a lot of these things in order to satisfy the requirements of notify_all_at_thread_exit, set_value_at_thread_exit, and make_ready_at_thread_exit.

@rmaprath has been doing some work to make the threading runtime library swappable.  I don't recall if his work extended to libcxxabi or not, but I'll page him anyway.

This implementation of __cxa_thread_atexit doesn't interact nicely with shared libraries.  The following sequence of events causes unloaded code to get invoked.

- Create thread 42
- Load library foo from thread 42
- Call a function with a thread_local object with a dtor.
- Unload library foo.
- Join thread 42

glibc does some extra work during __cxa_thread_atexit_impl to bump the reference count of the shared library so that the user's "unload" doesn't actually unload the library.

Comment at: src/cxa_thread_atexit.cpp:40
@@ +39,3 @@
+    ~DtorListHolder() {
+      run_dtors(get());
I think this is correct, but it needs some comments because it is not obvious what (or why) this is implemented this way.

More specifically, document the cases where run_dtors is run because of ~DtorListHolder vs. the cases where run_dtors is run because of the callback registered at pthread_key_create.


More information about the cfe-commits mailing list