www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - Fibers and TLS woes

reply Dan Olson <zans.is.for.cans yahoo.com> writes:
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
Feb 28 2015
parent reply "Kai Nacke" <kai redstar.de> writes:
On Saturday, 28 February 2015 at 18:10:13 UTC, Dan Olson wrote:
 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
Hi Dan, this is issue 666: https://github.com/ldc-developers/ldc/issues/666 Regards, Kai
Feb 28 2015
parent Dan Olson <zans.is.for.cans yahoo.com> writes:
"Kai Nacke" <kai redstar.de> writes:
 this is issue 666: https://github.com/ldc-developers/ldc/issues/666
Thanks Kai. I could not imagine I was the first to stumble into this.
Feb 28 2015