• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <pthread.h>
2 #include <string.h>
3 #include "test.h"
4 
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) )
8 
9 static pthread_key_t k1, k2;
10 
dtor(void * p)11 static void dtor(void *p)
12 {
13 	*(int *)p = 1;
14 }
15 
start(void * arg)16 static void *start(void *arg)
17 {
18 	int *p = arg;
19 	if (pthread_setspecific(k1, p) || pthread_setspecific(k2, p+1))
20 		return arg;
21 	return 0;
22 }
23 
main(void)24 int main(void)
25 {
26 	pthread_t td;
27 	int r;
28 	void *res;
29 	int foo[2], bar[2];
30 
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;
49 }
50