Thread local data and atomic instructions

I'm just dabbling with threads for the first time and have hit a few problems.

I need a shadow stack per thread and I'm not sure how to do that. Passing it
everywhere seems like a bad idea. I could put it in a global thread id ->
thread-local data mapping but that would incur lookup and locking every time
I fiddle with it (which is often!). Is it possible for me to create a global
using LLVM and mark it as thread local and use that instead?

Less importantly, I'd like to use atomic instructions to push my state machine
around (for suspending and resuming mutators) and, although I can see
llvm.atomic.cmp.swap in the LLVM Language Reference, I cannot see how to emit
these atomic instructions from my compiler in the APIs. How is this done?

Hello

I need a shadow stack per thread and I'm not sure how to do that. Passing it
everywhere seems like a bad idea. I could put it in a global thread id ->
thread-local data mapping but that would incur lookup and locking every time
I fiddle with it (which is often!). Is it possible for me to create a global
using LLVM and mark it as thread local and use that instead?

In theory - yes, but it will work only on x86/linux. No thread local
stuff on darwin at all, for example.

Ugh. What if I make thread local data in the C code of my run-time (compiled
with GCC) with C functions to get and set it?

How does llvm-gcc handle thread local data on Darwin then?

How does llvm-gcc handle thread local data on Darwin then?

It doesn't:

$ cat thread.c
__thread int foo;

$ gcc thread.c
thread.c:1: error: thread-local storage not supported for this target

$ llvm-gcc thread.c
thread.c:1: error: thread-local storage not supported for this target