]> git.saurik.com Git - apple/libpthread.git/blob - tests/detach.c
libpthread-301.1.6.tar.gz
[apple/libpthread.git] / tests / detach.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4 #include <unistd.h>
5 #include <dispatch/dispatch.h>
6 #include <sys/mman.h>
7
8 #include "darwintest_defaults.h"
9
10 static void*
11 thread_routine(void *loc)
12 {
13 uintptr_t i = (uintptr_t)loc;
14
15 switch (i % 3) {
16 case 0:
17 usleep(1000);
18 break;
19 case 1:
20 pthread_exit(pthread_self());
21 __builtin_unreachable();
22 case 2:
23 break;
24 }
25 return NULL;
26 }
27
28 T_DECL(pthread_detach, "Test creating and detaching threads in a loop",
29 T_META_CHECK_LEAKS(NO), T_META_ALL_VALID_ARCHS(YES))
30 {
31 const size_t count = 32;
32 pthread_t ths[count];
33
34 for (size_t i = 0; i < 100; i++) {
35 for (size_t j = 0; j < count; j++) {
36 T_ASSERT_POSIX_ZERO(pthread_create(&ths[j], NULL,
37 thread_routine, (void *)j), "thread creation");
38 T_ASSERT_POSIX_ZERO(pthread_detach(ths[j]), "thread detach");
39 }
40 usleep(50000);
41 }
42 }