Fibers and TLS woes

Dan Olson via digitalmars-d-ldc digitalmars-d-ldc at puremagic.com
Sat Feb 28 10:10:13 PST 2015


I discovered why one of the Fiber unittests in core.thread would crash
on iOS. TLS address is looked up based on the thread. In LDC optimized
code, the address lookup may only happen once in a function if the
address can be cached in a register. This is good, because it makes
multiple TLS access faster in a single function.

Now, Fibers are permitted to be run on different threads, as long as
only one thread has it active at a time. The problem is that the Fiber
context may be on a different thread before and after a yeild(). If a
TLS address is cached, the previous thread's TLS is accessed after the
yield call instead of the current threads TLS.

To make the one Fiber unittest work on multicore, I am switch to using
pthread_getspecific intead of TLS for sm_this. But in general this is a
problem with other architectures besides arm. I checked assembly of
x86_64 and TLS addresses are cached too when optimization is on.

I also looked at DMD asm, and it does not seem to cache TLS addresses on
OS X at least.
--
Dan


More information about the digitalmars-d-ldc mailing list