]> git.saurik.com Git - apple/libpthread.git/blob - tests/tsd.c
libpthread-137.1.1.tar.gz
[apple/libpthread.git] / tests / tsd.c
1 #include <assert.h>
2 #include <pthread.h>
3 #include <stdio.h>
4
5 void *ptr = NULL;
6
7 void destructor(void *value)
8 {
9 ptr = value;
10 }
11
12 void *thread(void *param)
13 {
14 int res;
15
16 pthread_key_t key = *(pthread_key_t *)param;
17 res = pthread_setspecific(key, (void *)0x12345678);
18 assert(res == 0);
19 void *value = pthread_getspecific(key);
20
21 pthread_key_t key2;
22 res = pthread_key_create(&key, NULL);
23 assert(res == 0);
24 res = pthread_setspecific(key, (void *)0x55555555);
25 assert(res == 0);
26
27 return value;
28 }
29
30 int main(int argc, char *argv[])
31 {
32 int res;
33 pthread_key_t key;
34
35 res = pthread_key_create(&key, destructor);
36 assert(res == 0);
37 printf("key = %ld\n", key);
38
39 pthread_t p = NULL;
40 res = pthread_create(&p, NULL, thread, &key);
41 assert(res == 0);
42
43 void *value = NULL;
44 res = pthread_join(p, &value);
45 printf("value = %p\n", value);
46 printf("ptr = %p\n", ptr);
47
48 assert(ptr == value);
49
50 res = pthread_key_delete(key);
51 assert(res == 0);
52
53 return 0;
54 }
55