]> git.saurik.com Git - apple/libpthread.git/blob - tests/pthread_exit.c
libpthread-218.1.3.tar.gz
[apple/libpthread.git] / tests / pthread_exit.c
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include <darwintest.h>
8
9 #define STACK_SIZE 32768
10 #define THREAD_DEPTH 2000
11
12 static unsigned int glob = 0;
13 static unsigned int i;
14
15 static pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER;
16
17 static void *
18 thread_exit(__unused void *arg)
19 {
20 unsigned int count;
21
22 sleep(5);
23 pthread_mutex_lock(&count_lock);
24 count = ++glob;
25 pthread_mutex_unlock(&count_lock);
26
27 T_QUIET; T_EXPECT_NE(pthread_mach_thread_np(pthread_self()), (mach_port_t)0, NULL);
28
29 if (count == THREAD_DEPTH){
30 T_PASS("all the threads survived main thread exit");
31 T_END;
32 }
33 return NULL;
34 }
35
36 T_DECL(pthread_exit, "pthread_exit")
37 {
38 int j;
39 pthread_t th[THREAD_DEPTH];
40
41 T_LOG("Creating threads %d..%d", i, i+THREAD_DEPTH-1);
42 for (j = 0; j < THREAD_DEPTH; j++) {
43 pthread_attr_t attr;
44
45 pthread_attr_init(&attr);
46 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
47 T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_create(&th[j], &attr, thread_exit, (void *)&glob), NULL);
48 pthread_attr_destroy(&attr);
49 }
50 pthread_exit(pthread_self());
51 T_FAIL("Zombie walks");
52 }
53
54 static void *
55 thread_stub(__unused void *arg)
56 {
57 return NULL;
58 }
59
60 T_DECL(pthread_exit_private_stacks, "pthread_exit with private stacks", T_META_CHECK_LEAKS(NO))
61 {
62 int j;
63 pthread_t th[THREAD_DEPTH];
64 void *stacks[THREAD_DEPTH];
65
66 for (j = 0; j < THREAD_DEPTH; j++) {
67 T_QUIET; T_ASSERT_NOTNULL((stacks[j] = malloc(STACK_SIZE)), NULL);
68 }
69
70 for (i=0;i < 20; i++) {
71 for (j = 0; j < THREAD_DEPTH; j++) {
72 pthread_attr_t attr;
73 pthread_attr_init(&attr);
74 pthread_attr_setstack(&attr, stacks[j], STACK_SIZE);
75 T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_create(&th[j], &attr, thread_stub, (void *)&glob), NULL);
76 pthread_attr_destroy(&attr);
77 }
78 for (j = 0; j < THREAD_DEPTH; j++) {
79 T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_join(th[j], NULL), NULL);
80 }
81 T_PASS("Created threads %d..%d", i*THREAD_DEPTH, (i+1)*THREAD_DEPTH-1);
82 }
83
84 }
85
86 T_DECL(pthread_exit_detached, "pthread_exit with detached threads")
87 {
88 int j;
89 pthread_t th[THREAD_DEPTH];
90
91 for (i=0;i < 20; i++) {
92 for (j = 0; j < THREAD_DEPTH; j++) {
93 pthread_attr_t attr;
94 pthread_attr_init(&attr);
95 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
96 T_QUIET; T_ASSERT_POSIX_SUCCESS(pthread_create(&th[j], &attr, thread_stub, (void *)&glob), NULL);
97 pthread_attr_destroy(&attr);
98 }
99 sleep(1);
100 T_PASS("Created threads %d..%d", i*THREAD_DEPTH, (i+1)*THREAD_DEPTH-1);
101 }
102 T_PASS("Success!");
103 }