Debian Codegraph @

libphobos/testsuite/libphobos.thread/external_threads.d

raw
1 import core.memory;
2 import core.sys.posix.pthread : pthread_create, pthread_join;
3 import core.sys.posix.sys.types : pthread_t;
4 import core.thread;
5
6 extern (C) void rt_moduleTlsCtor();
7 extern (C) void rt_moduleTlsDtor();
8
9 extern(C)
10 void* entry_point1(void*)
11 {
12 // try collecting - GC must ignore this call because this thread
13 // is not registered in runtime
14 GC.collect();
15 return null;
16 }
17
18 extern(C)
19 void* entry_point2(void*)
20 {
21 // This thread gets registered in druntime, does some work and gets
22 // unregistered to be cleaned up manually
23 thread_attachThis();
24 rt_moduleTlsCtor();
25
26 auto x = new int[10];
27
28 rt_moduleTlsDtor();
29 thread_detachThis();
30 return null;
31 }
32
33 void main()
34 {
35 // allocate some garbage
36 auto x = new int[1000];
37
38 {
39 pthread_t thread;
40 auto status = pthread_create(&thread, null, &entry_point1, null);
41 assert(status == 0);
42 pthread_join(thread, null);
43 }
44
45 {
46 pthread_t thread;
47 auto status = pthread_create(&thread, null, &entry_point2, null);
48 assert(status == 0);
49 pthread_join(thread, null);
50 }
51 }