xref: /netbsd-src/external/gpl3/gcc.old/dist/libphobos/testsuite/libphobos.allocations/tls_gc_integration.d (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 import core.memory, core.thread, core.bitop;
2 
3 /*
4  * This test repeatedly performs operations on GC-allocated objects which
5  * are only reachable from TLS storage. Tests are performed in multiple threads
6  * and GC collections are triggered repeatedly, so if the GC does not properly
7  * scan TLS memory, this provokes a crash.
8  */
9 class TestTLS
10 {
11     uint a;
addNumber()12     void addNumber()
13     {
14         auto val = volatileLoad(&a);
15         val++;
16         volatileStore(&a, val);
17     }
18 }
19 
20 TestTLS tlsPtr;
21 
this()22 static this()
23 {
24     tlsPtr = new TestTLS();
25 }
26 
main()27 void main()
28 {
29     void runThread()
30     {
31         for (size_t i = 0; i < 100; i++)
32         {
33             Thread.sleep(10.msecs);
34             tlsPtr.addNumber();
35             GC.collect();
36         }
37     }
38 
39     Thread[] threads;
40     for (size_t i = 0; i < 20; i++)
41     {
42         auto t = new Thread(&runThread);
43         threads ~= t;
44         t.start();
45     }
46     runThread();
47 
48     foreach (thread; threads)
49         thread.join();
50 }
51