conformance fixes
[libc-test.git] / src / functional / pthread_tsd.c
blob6e6360d918a2b0866a0b097e36ced2dff661e152
1 #include <pthread.h>
2 #include <string.h>
3 #include "test.h"
5 #define TESTC(c, m) ( (c) || (t_error("%s failed (" m ")\n", #c), 0) )
6 #define TESTR(r, f, m) ( \
7 ((r) = (f)) == 0 || (t_error("%s failed: %s (" m ")\n", #f, strerror(r)), 0) )
9 static pthread_key_t k1, k2;
11 static void dtor(void *p)
13 *(int *)p = 1;
16 static void *start(void *arg)
18 int *p = arg;
19 if (pthread_setspecific(k1, p) || pthread_setspecific(k2, p+1))
20 return arg;
21 return 0;
24 int main(void)
26 pthread_t td;
27 int r;
28 void *res;
29 int foo[2], bar[2];
31 /* Test POSIX thread-specific data */
32 TESTR(r, pthread_key_create(&k1, dtor), "failed to create key");
33 TESTR(r, pthread_key_create(&k2, dtor), "failed to create key");
34 foo[0] = foo[1] = 0;
35 TESTR(r, pthread_setspecific(k1, bar), "failed to set tsd");
36 TESTR(r, pthread_setspecific(k2, bar+1), "failed to set tsd");
37 TESTR(r, pthread_create(&td, 0, start, foo), "failed to create thread");
38 TESTR(r, pthread_join(td, &res), "failed to join");
39 TESTC(res == 0, "pthread_setspecific failed in thread");
40 TESTC(foo[0] == 1, "dtor failed to run");
41 TESTC(foo[1] == 1, "dtor failed to run");
42 TESTC(pthread_getspecific(k1) == bar, "tsd corrupted");
43 TESTC(pthread_getspecific(k2) == bar+1, "tsd corrupted");
44 TESTR(r, pthread_setspecific(k1, 0), "failed to clear tsd");
45 TESTR(r, pthread_setspecific(k2, 0), "failed to clear tsd");
46 TESTR(r, pthread_key_delete(k1), "failed to destroy key");
47 TESTR(r, pthread_key_delete(k2), "failed to destroy key");
48 return t_status;