]> git.saurik.com Git - apple/libpthread.git/blob - tests/pthread_introspection.c
libpthread-218.51.1.tar.gz
[apple/libpthread.git] / tests / pthread_introspection.c
1 #include <stdatomic.h>
2 #include <pthread.h>
3 #include <pthread/introspection_private.h>
4 #include <dispatch/dispatch.h>
5
6 #include <darwintest.h>
7
8 static pthread_introspection_hook_t prev_pthread_introspection_hook;
9
10 #define THREAD_COUNT 3
11
12 static void my_pthread_introspection_hook(unsigned int event, pthread_t thread,
13 void *addr, size_t size)
14 {
15 static atomic_int create_count;
16 static atomic_int terminate_count;
17
18 uint64_t tid;
19 pthread_threadid_np(NULL, &tid);
20
21 if (event == PTHREAD_INTROSPECTION_THREAD_CREATE) {
22 T_LOG("event = PTHREAD_INTROSPECTION_THREAD_CREATE, thread = %p:%lld, addr = %p, size = 0x%zx", thread, tid, addr, size);
23 create_count++;
24 } else if (event == PTHREAD_INTROSPECTION_THREAD_TERMINATE) {
25 T_LOG("event = PTHREAD_INTROSPECTION_THREAD_TERMINATE, thread = %p:%lld, addr = %p, size = 0x%zx", thread, tid, addr, size);
26 terminate_count++;
27 T_ASSERT_GE(create_count, THREAD_COUNT, NULL);
28 T_PASS("Got termination events");
29 T_END;
30 }
31
32 if (prev_pthread_introspection_hook != NULL){
33 prev_pthread_introspection_hook(event, thread, addr, size);
34 }
35 }
36
37 T_DECL(PR_25679871, "PR-25679871",
38 T_META_TIMEOUT(30), T_META_ALL_VALID_ARCHS(YES))
39 {
40 prev_pthread_introspection_hook = pthread_introspection_hook_install(&my_pthread_introspection_hook);
41
42 // minus two that come up in dispatch internally, one that comes after this block
43 for (int i = 0; i < THREAD_COUNT - 3; i++) {
44 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
45 sleep(3);
46 });
47 }
48 dispatch_queue_t serial_queue = dispatch_queue_create("test queue", NULL);
49 __block dispatch_block_t looping_block = ^{
50 static int count;
51 if (count < 20) {
52 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 50 * NSEC_PER_MSEC), serial_queue, looping_block);
53 }
54 };
55 dispatch_async(serial_queue, looping_block);
56
57 sleep(30);
58
59 T_FAIL("Why are we still alive?");
60 }