Am Thu, 17 Mar 2016 20:27:39 +0000 schrieb bitwise <bitwise....@gmail.com>:
> I've been doing some work on shared libraries for OSX, and have > come across a potential problem, which I'm not sure what to do > with. > > Currently, when a thread is spawned, that thread calls all the > TLS ctors, then runs the thread entry point function, and finally > calls the TLS dtors before the thread terminates. > > Example, for windows: > > https://github.com/D-Programming-Language/druntime/blob/15a227477a344583c4748d95492703901417f4f8/src/core/thread.d#L236 > > So, the question is, how do dynamic libraries interact here? > > Example: A dynamic library contains D modules with TLS ctors. If > I start a few threads, and then load a dynamic library, shouldn't > the TLS ctors in the dynamic library be called for each running > thread? If my assumption is correct, the next question is, how do > you do this? > > I don't think you can hijack each thread and have it run the TLS > ctors, and you can run them all from the thread loading the > shared library because of synchronization issues. So what's the > solution? Should TLS ctors in dynamic libraries simply be > specified not to run, or could they somehow be run lazily at the > first TLS access in a dynamic library? > > Any thoughts on this would be appreciated. > > Thanks, > Bit > 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. As TLS ctors are meant to initialize TLS storage the correct time to run these is after the per-thread TLS storage allocation. (The C runtime can actually allocate TLS memory lazily, on the first access to a TLS variable in a module). The C runtimes of course know when TLS storage is allocated, but they do not provide hooks we could use.