3 #include <pthread/private.h>
8 #include "darwintest_defaults.h"
10 #define STACK_SIZE 32768
11 #define THREAD_DEPTH 2000
13 static unsigned int glob
= 0;
14 static unsigned int i
;
16 static pthread_mutex_t count_lock
= PTHREAD_MUTEX_INITIALIZER
;
19 thread_exit(__unused
void *arg
)
24 pthread_mutex_lock(&count_lock
);
26 pthread_mutex_unlock(&count_lock
);
28 T_QUIET
; T_EXPECT_NE(pthread_mach_thread_np(pthread_self()), (mach_port_t
)0, NULL
);
30 if (count
== THREAD_DEPTH
){
31 T_PASS("all the threads survived main thread exit");
37 T_DECL(pthread_exit
, "pthread_exit", T_META_LTEPHASE(LTE_INSTALLEDUSEROS
))
40 pthread_t th
[THREAD_DEPTH
];
42 T_LOG("Creating threads %d..%d", i
, i
+THREAD_DEPTH
-1);
43 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
46 pthread_attr_init(&attr
);
47 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
48 T_QUIET
; T_ASSERT_POSIX_SUCCESS(pthread_create(&th
[j
], &attr
, thread_exit
, (void *)&glob
), NULL
);
49 pthread_attr_destroy(&attr
);
51 pthread_exit(pthread_self());
52 T_FAIL("Zombie walks");
56 thread_stub(__unused
void *arg
)
61 T_DECL(pthread_exit_private_stacks
, "pthread_exit with private stacks",
62 T_META_CHECK_LEAKS(NO
))
65 pthread_t th
[THREAD_DEPTH
];
66 void *stacks
[THREAD_DEPTH
];
68 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
69 T_QUIET
; T_ASSERT_NOTNULL((stacks
[j
] = malloc(STACK_SIZE
)), NULL
);
72 for (i
=0;i
< 20; i
++) {
73 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
75 pthread_attr_init(&attr
);
76 pthread_attr_setstack(&attr
, stacks
[j
], STACK_SIZE
);
77 T_QUIET
; T_ASSERT_POSIX_SUCCESS(pthread_create(&th
[j
], &attr
, thread_stub
, (void *)&glob
), NULL
);
78 pthread_attr_destroy(&attr
);
80 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
81 T_QUIET
; T_ASSERT_POSIX_SUCCESS(pthread_join(th
[j
], NULL
), NULL
);
83 T_PASS("Created threads %d..%d", i
*THREAD_DEPTH
, (i
+1)*THREAD_DEPTH
-1);
88 T_DECL(pthread_exit_detached
, "pthread_exit with detached threads")
91 pthread_t th
[THREAD_DEPTH
];
93 for (i
=0;i
< 20; i
++) {
94 for (j
= 0; j
< THREAD_DEPTH
; j
++) {
96 pthread_attr_init(&attr
);
97 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
98 T_QUIET
; T_ASSERT_POSIX_SUCCESS(pthread_create(&th
[j
], &attr
, thread_stub
, (void *)&glob
), NULL
);
99 pthread_attr_destroy(&attr
);
102 T_PASS("Created threads %d..%d", i
*THREAD_DEPTH
, (i
+1)*THREAD_DEPTH
-1);
108 key_dtor(void *value
)
110 T_ASSERT_EQ(1, pthread_self_is_exiting_np(), "exiting");
113 T_DECL(pthread_self_is_exiting_np
, "pthread_self_is_exiting_np")
117 T_ASSERT_POSIX_ZERO(pthread_key_create(&key
, key_dtor
), NULL
);
118 pthread_setspecific(key
, (void *)-1);
119 T_ASSERT_EQ(0, pthread_self_is_exiting_np(), "not exiting");