aboutsummaryrefslogtreecommitdiff
path: root/libphobos/testsuite/libphobos.allocations/tls_gc_integration.d
blob: 7c084abcaf109617d32d0d288203d52c4f076091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import core.memory, core.thread, core.volatile;

/*
 * This test repeatedly performs operations on GC-allocated objects which
 * are only reachable from TLS storage. Tests are performed in multiple threads
 * and GC collections are triggered repeatedly, so if the GC does not properly
 * scan TLS memory, this provokes a crash.
 */
class TestTLS
{
    uint a;
    void addNumber()
    {
        auto val = volatileLoad(&a);
        val++;
        volatileStore(&a, val);
    }
}

TestTLS tlsPtr;

static this()
{
    tlsPtr = new TestTLS();
}

void main()
{
    void runThread()
    {
        for (size_t i = 0; i < 100; i++)
        {
            Thread.sleep(10.msecs);
            tlsPtr.addNumber();
            GC.collect();
        }
    }

    Thread[] threads;
    for (size_t i = 0; i < 20; i++)
    {
        auto t = new Thread(&runThread);
        threads ~= t;
        t.start();
    }
    runThread();

    foreach (thread; threads)
        thread.join();
}