Debian Codegraph @

gc/tests/threadkey_test.c

raw
1
2 #ifdef HAVE_CONFIG_H
3 # include "config.h"
4 #endif
5
6 #ifndef GC_THREADS
7 # define GC_THREADS
8 #endif
9
10 #define GC_NO_THREAD_REDIRECTS 1
11
12 #include "gc.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #if (!defined(GC_PTHREADS) || defined(GC_SOLARIS_THREADS) \
18 || defined(__native_client__)) && !defined(SKIP_THREADKEY_TEST)
19 /* FIXME: Skip this test on Solaris for now. The test may fail on */
20 /* other targets as well. Currently, tested only on Linux, Cygwin */
21 /* and Darwin. */
22 # define SKIP_THREADKEY_TEST
23 #endif
24
25 #ifdef SKIP_THREADKEY_TEST
26
27 int main(void)
28 {
29 printf("threadkey_test skipped\n");
30 return 0;
31 }
32
33 #else
34
35 #include <pthread.h>
36
37 pthread_key_t key;
38
39 #ifdef GC_SOLARIS_THREADS
40 /* pthread_once_t key_once = { PTHREAD_ONCE_INIT }; */
41 #else
42 pthread_once_t key_once = PTHREAD_ONCE_INIT;
43 #endif
44
45 void * entry (void *arg)
46 {
47 pthread_setspecific(key,
48 (void *)GC_HIDE_POINTER(GC_STRDUP("hello, world")));
49 return arg;
50 }
51
52 void * GC_CALLBACK on_thread_exit_inner (struct GC_stack_base * sb, void * arg)
53 {
54 int res = GC_register_my_thread (sb);
55 pthread_t t;
56 int creation_res; /* Used to suppress a warning about */
57 /* unchecked pthread_create() result. */
58 pthread_attr_t attr;
59
60 if (pthread_attr_init(&attr) != 0
61 || pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
62 fprintf(stderr, "Thread attribute init or setdetachstate failed\n");
63 exit(2);
64 }
65 creation_res = GC_pthread_create(&t, &attr, entry, NULL);
66 (void)pthread_attr_destroy(&attr);
67 if (res == GC_SUCCESS)
68 GC_unregister_my_thread ();
69
70 return arg ? (void*)(GC_word)creation_res : 0;
71 }
72
73 void on_thread_exit (void *v)
74 {
75 GC_call_with_stack_base (on_thread_exit_inner, v);
76 }
77
78 void make_key (void)
79 {
80 pthread_key_create (&key, on_thread_exit);
81 }
82
83 #ifndef NTHREADS
84 # define NTHREADS 5
85 #endif
86
87 #define NTHREADS_INNER (NTHREADS * 6) /* number of threads to create */
88
89 int main(void)
90 {
91 int i;
92
93 GC_INIT();
94 if (GC_get_find_leak())
95 printf("This test program is not designed for leak detection mode\n");
96 # ifdef GC_SOLARIS_THREADS
97 make_key();
98 # else
99 pthread_once (&key_once, make_key);
100 # endif
101 for (i = 0; i < NTHREADS_INNER; i++) {
102 pthread_t t;
103
104 if (GC_pthread_create(&t, NULL, entry, NULL) == 0) {
105 void *res;
106 int code = (i & 1) != 0 ? GC_pthread_join(t, &res)
107 : GC_pthread_detach(t);
108
109 if (code != 0) {
110 fprintf(stderr, "Thread %s failed, errno= %d\n",
111 (i & 1) != 0 ? "join" : "detach", code);
112 exit(2);
113 }
114 }
115 }
116 return 0;
117 }
118
119 #endif /* !SKIP_THREADKEY_TEST */