7 #include <darwintest.h>
9 #define STACK_SIZE 32768
10 #define THREAD_DEPTH 2000
12 static unsigned int glob
= 0;
13 static unsigned int i
;
15 static pthread_mutex_t count_lock
= PTHREAD_MUTEX_INITIALIZER
;
18 thread_exit(__unused
void *arg
)
23 pthread_mutex_lock(&count_lock
);
25 pthread_mutex_unlock(&count_lock
);
27 T_QUIET
; T_EXPECT_NE(pthread_mach_thread_np(pthread_self()), (mach_port_t
)0, NULL
);
29 if (count
== THREAD_DEPTH
){
30 T_PASS("all the threads survived main thread exit");
36 T_DECL(pthread_exit
, "pthread_exit")
39 pthread_t th
[THREAD_DEPTH
];
41 T_LOG("Creating threads %d..%d", i
, i
+THREAD_DEPTH
-1);
42 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
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
);
50 pthread_exit(pthread_self());
51 T_FAIL("Zombie walks");
55 thread_stub(__unused
void *arg
)
60 T_DECL(pthread_exit_private_stacks
, "pthread_exit with private stacks", T_META_CHECK_LEAKS(NO
))
63 pthread_t th
[THREAD_DEPTH
];
64 void *stacks
[THREAD_DEPTH
];
66 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
67 T_QUIET
; T_ASSERT_NOTNULL((stacks
[j
] = malloc(STACK_SIZE
)), NULL
);
70 for (i
=0;i
< 20; i
++) {
71 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
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
);
78 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
79 T_QUIET
; T_ASSERT_POSIX_SUCCESS(pthread_join(th
[j
], NULL
), NULL
);
81 T_PASS("Created threads %d..%d", i
*THREAD_DEPTH
, (i
+1)*THREAD_DEPTH
-1);
86 T_DECL(pthread_exit_detached
, "pthread_exit with detached threads")
89 pthread_t th
[THREAD_DEPTH
];
91 for (i
=0;i
< 20; i
++) {
92 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
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
);
100 T_PASS("Created threads %d..%d", i
*THREAD_DEPTH
, (i
+1)*THREAD_DEPTH
-1);