]> git.saurik.com Git - apple/libpthread.git/blob - tests/join.c
libpthread-138.10.4.tar.gz
[apple/libpthread.git] / tests / join.c
1 #include <assert.h>
2 #include <pthread.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <mach/mach.h>
7
8 #define WAITTIME (100 * 1000)
9
10 static inline void*
11 test(void)
12 {
13 static uintptr_t idx;
14 printf("Join %lu\n", ++idx);
15 return (void*)idx;
16 }
17
18 static void *
19 thread(void *param)
20 {
21 usleep(WAITTIME);
22 return param;
23 }
24
25 static void *
26 thread1(void *param)
27 {
28 int res;
29 pthread_t p = param;
30
31 usleep(WAITTIME);
32 res = pthread_join(p, NULL);
33 assert(res == 0);
34 printf("Done\n");
35 return 0;
36 }
37
38 __attribute((noreturn))
39 int
40 main(void)
41 {
42 int res;
43 kern_return_t kr;
44 pthread_t p = NULL;
45 void *param, *value;
46
47 param = test();
48 res = pthread_create(&p, NULL, thread, param);
49 assert(res == 0);
50 value = NULL;
51 res = pthread_join(p, &value);
52 assert(res == 0);
53 assert(param == value);
54
55 param = test();
56 res = pthread_create(&p, NULL, thread, param);
57 assert(res == 0);
58 usleep(3 * WAITTIME);
59 value = NULL;
60 res = pthread_join(p, &value);
61 assert(res == 0);
62 assert(param == value);
63
64 param = test();
65 res = pthread_create_suspended_np(&p, NULL, thread, param);
66 assert(res == 0);
67 kr = thread_resume(pthread_mach_thread_np(p));
68 assert(kr == 0);
69 value = NULL;
70 res = pthread_join(p, &value);
71 assert(res == 0);
72 assert(param == value);
73
74 param = test();
75 res = pthread_create_suspended_np(&p, NULL, thread, param);
76 assert(res == 0);
77 kr = thread_resume(pthread_mach_thread_np(p));
78 assert(kr == 0);
79 usleep(3 * WAITTIME);
80 value = NULL;
81 res = pthread_join(p, &value);
82 assert(res == 0);
83 assert(param == value);
84
85 test();
86 param = pthread_self();
87 res = pthread_create_suspended_np(&p, NULL, thread1, param);
88 assert(res == 0);
89 res = pthread_detach(p);
90 assert(res == 0);
91 kr = thread_resume(pthread_mach_thread_np(p));
92 assert(kr == 0);
93 pthread_exit(0);
94 }
95