10 #include <sys/sysctl.h>
12 #include <mach/mach_time.h>
15 #include <pthread/tsd_private.h>
16 #include <pthread/qos_private.h>
17 #include <pthread/workqueue_private.h>
19 #include <dispatch/dispatch.h>
21 #include <darwintest.h>
22 #include <darwintest_utils.h>
24 extern void __exit(int) __attribute__((noreturn
));
26 static void __attribute__((noreturn
))
27 run_add_timer_termination(void)
29 const int SOURCES
= 32;
30 static unsigned int time_to_sleep
; time_to_sleep
= (unsigned int)(arc4random() % 5000 + 500);
32 static int pipes
[SOURCES
][2];
33 static dispatch_source_t s
[SOURCES
];
34 for (int i
= 0; i
< SOURCES
; i
++) {
36 s
[i
] = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, (uintptr_t)pipes
[i
][0], 0, NULL
);
37 dispatch_source_set_event_handler(s
[i
], ^{
40 dispatch_resume(s
[i
]);
43 dispatch_async(dispatch_get_global_queue(0,0), ^{
44 for (int i
= 1; i
< SOURCES
; i
++){
45 write(pipes
[i
][1], &SOURCES
, 1);
51 usleep(time_to_sleep
);
55 T_DECL(add_timer_termination
, "termination during add timer",
56 T_META_CHECK_LEAKS(NO
))
58 const int ROUNDS
= 128;
59 const int TIMEOUT
= 5;
60 for (int i
= 0; i
< ROUNDS
; i
++){
62 T_QUIET
; T_ASSERT_POSIX_SUCCESS(pid
, "fork");
63 if (pid
== 0) { // child
64 run_add_timer_termination();
66 bool success
= dt_waitpid(pid
, NULL
, NULL
, TIMEOUT
);
67 T_ASSERT_TRUE(success
, "Child %d exits successfully", i
);