Add support for TLS callbacks on Windows. (#15423)
commit5fee1d3eea11ddae2f2b3a92bfb03cea7b47c091
authorJohan Lorensson <lateralusx.github@gmail.com>
Tue, 2 Jul 2019 12:06:57 +0000 (2 14:06 +0200)
committerGitHub <noreply@github.com>
Tue, 2 Jul 2019 12:06:57 +0000 (2 14:06 +0200)
tree3306e7a60a8572a2881f7fce94b9ca87f5da7001
parentb62878973d7d44cfa749c5dbabfd2f656efd9aba
Add support for TLS callbacks on Windows. (#15423)

* Add support for TLS callbacks on Windows.

Mono runtime depends on DllMain to be called by OS in order to correctly
detach threads from the runtime. If this doesn't happen,
threads from native thread pools (not owned by runtime) and attached
using native->managed callback, won't detach resulting in attached
threads no longer running. Since threads are still attached
to runtime, next GC will try to suspend the thread and that will fail
and that in turn will put the state machine in an incorrect state,
bringing down the runtime at next GC (when the thread state is revisited).

This problem is currently handled by DllMain, but since DllMain only exist
in DLL's, statically link Mono runtime will get us into the above scenario.

This commit add support to hook up a TLS callback using the same mechanism
used by MSVC linker and c-runtime (also available under MINGW). The
callback will be included in a section/segment of an object file and included
in final image (DLL or EXE) by linker and called by OS loader, solving the
problem with static linked Mono runtime not being able to detach terminating
threads.

The commit keeps current DllMain method when building a DLL but
includes a mechanism making sure we only use one of the callback techniques
at runtime, if both have been included in final image. This will simplify the build
of libmini since we can always build the object including the callback, regardless
how the library will be consumed.
mono/mini/Makefile.am.in
mono/mini/mini-windows-dllmain.c
mono/mini/mini-windows-tls-callback.c [new file with mode: 0644]
mono/mini/mini-windows.c
mono/mini/mini-windows.h
msvc/libmini-win32.targets
msvc/libmini-win32.targets.filters