3 #include <pthread/introspection_private.h> 
   4 #include <dispatch/dispatch.h> 
   6 #include <darwintest.h> 
   8 static pthread_introspection_hook_t prev_pthread_introspection_hook
; 
  10 #define THREAD_COUNT 3 
  12 static void my_pthread_introspection_hook(unsigned int event
, pthread_t thread
, 
  13                 void *addr
, size_t size
) 
  15         static atomic_int create_count
; 
  16         static atomic_int terminate_count
; 
  19         pthread_threadid_np(NULL
, &tid
); 
  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
); 
  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
); 
  27                 T_ASSERT_GE(create_count
, THREAD_COUNT
, NULL
); 
  28                 T_PASS("Got termination events"); 
  32         if (prev_pthread_introspection_hook 
!= NULL
){ 
  33                 prev_pthread_introspection_hook(event
, thread
, addr
, size
); 
  37 T_DECL(PR_25679871
, "PR-25679871", 
  38                 T_META_TIMEOUT(30), T_META_ALL_VALID_ARCHS(YES
)) 
  40         prev_pthread_introspection_hook 
= pthread_introspection_hook_install(&my_pthread_introspection_hook
); 
  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), ^{ 
  48         dispatch_queue_t serial_queue 
= dispatch_queue_create("test queue", NULL
); 
  49         __block dispatch_block_t looping_block 
= ^{ 
  52                         dispatch_after(dispatch_time(DISPATCH_TIME_NOW
, 50 * NSEC_PER_MSEC
), serial_queue
, looping_block
); 
  55         dispatch_async(serial_queue
, looping_block
); 
  59         T_FAIL("Why are we still alive?");