libc++abi thread_local_destruction_order test failure on MacOS

Hi cfe-dev,

The thread_local_destruction_order test in libc++abi fails on MacOS (I'm
running on Sierra with the Xcode 8.1 toolchain, but I'm pretty certain the
failure isn't OS or toolchain specific). The root cause is the test
constructing a thread_local inside the destructor for another thread_local,
which works on glibc but not on MacOS.

Specifically, on MacOS, clang uses _tlv_atexit to register destructors for
thread_local variables, and then tlv_finalize runs these destructors upon
thread termination. Looking at the source of tlv_finalize [1], it doesn't
handle the case where a termination function registers another termination
function, hence the test failure.

Is this a problem with the tlv_finalize implementation, or is the test
invoking undefined behavior by constructing a thread_local inside the
destructor for another thread_local? I haven't been able to find anything
definitive about whether the test case is permissible. I did find a somewhat
analogous Stack Overflow question about constructing a static object inside
the destructor for another static object [2], where the accepted answer
speculates that doing so is undefined behavior, but I'm not sure if the same
reasoning applies to thread_local variables.

Thoughts?

[1] https://github.com/opensource-apple/dyld/blob/195030646877261f0c8c7ad8b001f52d6a26f514/src/threadLocalVariables.c#L423
[2] http://stackoverflow.com/q/14278006