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

Tavian Barnes via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 2 11:05:12 PDT 2016


tavianator added a comment.

In https://reviews.llvm.org/D21803#532309, @EricWF wrote:

> `__thread`
>
> What do you think of this idea?


Makes sense to me, I'll integrate it into the next revision.


================
Comment at: src/cxa_thread_atexit.cpp:42
@@ +41,3 @@
+  // - 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
----------------
EricWF wrote:
> Can you clarify what you mean by "other threads"?
> 
> How is libc++ supposed to detect and handle this problem? 
I meant "non-main threads" ("other" is in relation to the bullet point above), but I can clarify this, sure.

libc++ could be patched to do something like this:

```
pthread_key_t key1, key2;

void destructor1(void* ptr) {
  pthread_setspecific(key2, ptr);
}

void destructor2(void* ptr) {
  // Runs in the second iteration through pthread_key destructors,
  // therefore after thread_local destructors
}

pthread_key_create(&key1, destructor1);
pthread_key_create(&key2, destructor2);

pthread_setspecific(key1, ptr);
```

(Or it could use a counter/flag and a single pthread_key.)

libstdc++ has the same bug when __cxa_thread_atexit_impl() isn't available, so I'm not sure that change would really be necessary.  If it is, I can write up the libc++ patch.


https://reviews.llvm.org/D21803





More information about the cfe-commits mailing list