Am Fri, 18 Mar 2016 14:35:41 +0000
schrieb Marc Schütz <schue...@gmx.net>:

> On Thursday, 17 March 2016 at 20:54:57 UTC, Johannes Pfau wrote:
> > It's been some time since I looked at that code, but IIRC TLS 
> > ctors for newly loaded libraries are not run in old threads. 
> > There's nothing we can do about this without help from the C 
> > runtime.  
> 
> Hmm... are all module _destructors_ called when those threads 
> exit? That would mean that some destructors may be called without 
> the corresponding constructors having run...

No. Each thread keeps a thread-local list of shared libraries it knows
about. This list is inherited from the parent thread on thread
creation. It also gets updated if a new library is loaded at runtime
(but only for the loading thread!). The runtime only runs constructors
and destructors for libraries in the list. Additionally the GC only
scans TLS memory of libraries in the list.


As an example:
* Create 2 Threads, TA & TB
* TB creates thread TB1
* TB loads library libA
* TB creates new thread TB2
* TA creates new thread TA1

Threads TB and TB2 will run TLS constructors and destructors for libA.
modules. These threads will also scan TLS memory from modules in libA.

Threads TA, TA1 and TB1 will not run TLS constructors or destructors for
libA. Additionally, the GC will not scan TLS memory for libA for these
threads.

So you can only safely use modules from libA in thread TB and TB2.

Relevant links:
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/sections_elf_shared.d#L198
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/sections_elf_shared.d#L281

https://github.com/D-Programming-Language/druntime/blob/master/src/core/thread.d#L385
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/minfo.d#L312
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/sections_elf_shared.d#L49
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/sections_elf_shared.d#L76

Reply via email to