On Saturday, 30 January 2021 at 12:28:16 UTC, Ali Çehreli wrote:
I wonder whether doing something in the runtime is possible.
For example, it may be more resilient and not crash when
suspending a thread fails because the thread may be dead
already.
However, studying the runtime code around thread_detachThis
three years ago, I had realized that like many things in
computing, the whole stop-the-world is wishful thinking because
there is no guarantee that your "please suspend this thread"
request to the OS has succeeded. You get a success return code
back but it means your request succeeded not that the thread
was or will be suspended. (I may be misremembering this point
but I know that the runtime requests things where OS does not
give full guarantee for.)
OT. A thread that suspends itself will always happen (not taking
fall through cases into account), if not, throw the OS away. If a
thread suspends another thread, then you don't really know when
that thread will be suspended. I would discourage that threads
suspends other threads because that will open up a new world of
race conditions. Some systems don't even allow it and its
benefits are very limited.
Back to topic. I think that the generic solution even if it
doesn't help you with your current implementation is to ban TLS
all together. I think there have already been requests to remove
TLS for druntime/phobos totally and I think this should
definitely be done sooner than later. Also if you write a shared
library in D, simply don't use TLS at all. This way it will not
matter if a thread is registered by druntime or not. TLS is in my
opinion a wart in computer science.