Bug 22681 – rt_moduleTlsCtor/Dtor() don't work with phobos dynamically linked

Status
NEW
Severity
normal
Priority
P3
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-01-16T21:50:33Z
Last change time
2024-12-07T13:41:41Z
Assigned to
No Owner
Creator
duser
Moved to GitHub: dmd#17435 →

Comments

Comment #0 by duser — 2022-01-16T21:50:33Z
``` __gshared int ctors, dtors; static this() { ctors++; } static ~this() { dtors++; } import core.thread; import core.stdc.stdio; void main() { ctors = 0; dtors = 0; createLowLevelThread({ scope(failure) assert(0); // satisfy nothrow thread_attachThis(); rt_moduleTlsCtor(); rt_moduleTlsDtor(); thread_detachThis(); }).joinLowLevelThread; printf("ctors %d dtors %d\n", ctors, dtors); assert(ctors == 1 && dtors == 1); } ``` with these commands, it works: % dmd -run tlstest.d % ldc2 -run tlstest.d with these commands, the constructor and destructor don't run: % dmd -defaultlib=phobos2 -run tlstest.d % dmd -L-lphobos2 -run tlstest.d % ldc2 --link-defaultlib-shared -run tlstest.d other things that do TLS initialization like `new Thread()` and `rt_init()` work normally
Comment #1 by duser — 2022-01-16T21:53:15Z
slightly longer example with a C main showing `rt_init()`: ``` __gshared int ctors, dtors; static this() { ctors++; } static ~this() { dtors++; } import core.runtime; import core.thread; import core.stdc.stdio; extern(C) int main() { rt_init(); printf("ctors %d dtors %d (should be 1 0) - main\n", ctors, dtors); ctors = 0; dtors = 0; createLowLevelThread({ scope(failure) assert(0); // satisfy nothrow thread_attachThis(); rt_moduleTlsCtor(); printf("ctors %d dtors %d (should be 1 0) - ll thread\n", ctors, dtors); rt_moduleTlsDtor(); thread_detachThis(); }).joinLowLevelThread; printf("ctors %d dtors %d (should be 1 1) - ll thread\n", ctors, dtors); ctors = 0; dtors = 0; auto t = new Thread({ printf("ctors %d dtors %d (should be 1 0) - D thread\n", ctors, dtors); }); t.start(); t.join(); printf("ctors %d dtors %d (should be 1 1) - D thread\n", ctors, dtors); ctors = 0; dtors = 0; rt_term(); printf("ctors %d dtors %d (should be 0 1) - main\n", ctors, dtors); return 0; } ``` the only difference with dynamic phobos is that the constructors for the low-level thread don't run (OS threads with pthread are also affected) `rt_init()` successfully inits them for the main thread, and so does `new Thread()` for the D thread
Comment #2 by robert.schadek — 2024-12-07T13:41:41Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17435 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB