]> git.saurik.com Git - apple/libpthread.git/blob - tests/pthread_setspecific.c
libpthread-218.30.1.tar.gz
[apple/libpthread.git] / tests / pthread_setspecific.c
1 /*
2 * @OSF_COPYRIGHT@
3 *
4 */
5 /*
6 * HISTORY
7 * $Log: pthread_test3.c,v $
8 * Revision 1.1.4.2 1996/10/03 17:53:38 emcmanus
9 * Changed fprintf(stderr...) to printf(...) to allow building with
10 * PURE_MACH includes.
11 * [1996/10/03 16:17:34 emcmanus]
12 *
13 * Revision 1.1.4.1 1996/10/01 07:36:02 emcmanus
14 * Copied from rt3_merge.
15 * Include <stdlib.h> for malloc() prototype.
16 * [1996/10/01 07:35:53 emcmanus]
17 *
18 * Revision 1.1.2.1 1996/09/27 13:12:15 gdt
19 * Add support for thread specific data
20 * [1996/09/27 13:11:17 gdt]
21 *
22 * $EndLog$
23 */
24
25 /*
26 * Test POSIX Thread Specific Data
27 */
28
29 #include <stdio.h>
30 #include <pthread.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include <darwintest.h>
35
36 static pthread_key_t key;
37
38 static void *
39 thread(void * arg)
40 {
41 char * msg;
42 T_LOG("thread %lx here: %s\n", (uintptr_t)pthread_self(), (char *)arg);
43 msg = malloc(256);
44 sprintf(msg, "This is thread specific data for %lx\n", (uintptr_t)pthread_self());
45 T_ASSERT_POSIX_ZERO(pthread_setspecific(key, msg), NULL);
46 return (arg);
47 }
48
49 static void
50 grim_reaper(void * param)
51 {
52 T_LOG("grim_reaper - self: %lx, param: %lx value: %s", (uintptr_t)pthread_self(), (uintptr_t)param, (char *)param);
53 free(param);
54 }
55
56 T_DECL(pthread_setspecific, "pthread_setspecific",
57 T_META_ALL_VALID_ARCHS(YES))
58 {
59 void * thread_res;
60 pthread_t t1, t2;
61 T_ASSERT_POSIX_ZERO(pthread_key_create(&key, grim_reaper), NULL);
62 T_ASSERT_POSIX_ZERO(pthread_create(&t1, (pthread_attr_t *)NULL, thread, "thread #1 arg"), NULL);
63 T_ASSERT_POSIX_ZERO(pthread_create(&t2, (pthread_attr_t *)NULL, thread, "thread #2 arg"), NULL);
64 T_ASSERT_POSIX_ZERO(pthread_join(t1, &thread_res), NULL);
65 T_ASSERT_POSIX_ZERO(pthread_join(t2, &thread_res), NULL);
66 }