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

Reply via email to