]> git.saurik.com Git - apple/xnu.git/blob - tests/workq_sigprof.c
458307962fce582345ba9cbe8b45518d0a3339c1
[apple/xnu.git] / tests / workq_sigprof.c
1 #include <pthread.h>
2 #include <stdbool.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/time.h>
8 #include <mach/mach_time.h>
9 #include <dispatch/dispatch.h>
10
11 #include <darwintest.h>
12
13 #if !TARGET_OS_IPHONE
14
15 static pthread_t workq_thread;
16 static bool signal_received;
17
18 static void
19 signal_handler(int sig __unused, siginfo_t *b __unused, void* unused __unused)
20 {
21 if (pthread_self() == workq_thread) {
22 signal_received = true;
23 }
24 }
25
26 static void
27 workq_block(void *unused __unused)
28 {
29 workq_thread = pthread_self();
30
31 /*
32 * sigset_t set;
33 * sigemptyset(&set);
34 * sigaddset(&set, SIGPROF);
35 * pthread_sigmask(SIG_UNBLOCK, &set, NULL);
36 */
37
38 uint64_t spin_start = mach_absolute_time();
39 while (mach_absolute_time() - spin_start < 30 * NSEC_PER_SEC) {
40 if (signal_received) {
41 T_PASS("Got SIGPROF!");
42 T_END;
43 }
44 }
45 }
46
47 T_DECL(workq_sigprof, "test that workqueue threads can receive sigprof")
48 {
49 struct sigaction sa = {
50 .sa_sigaction = signal_handler
51 };
52 sigfillset(&sa.sa_mask);
53 T_ASSERT_POSIX_ZERO(sigaction(SIGPROF, &sa, NULL), NULL);
54
55 dispatch_queue_t q = dispatch_get_global_queue(0, 0);
56 dispatch_async_f(q, NULL, workq_block);
57
58 struct itimerval timerval = {
59 .it_interval = {.tv_usec = 10000},
60 .it_value = {.tv_usec = 10000}
61 };
62 T_ASSERT_POSIX_ZERO(setitimer(ITIMER_PROF, &timerval, NULL), NULL);
63
64 dispatch_main();
65 }
66
67 #else //!TARGET_OS_IPHONE
68
69 T_DECL(workq_sigprof, "test that workqueue threads can receive sigprof")
70 {
71 T_EXPECTFAIL;
72 T_FAIL("<rdar://problem/25864196> setitimer/sigprof doesn't seem to be delivered on embeded platforms");
73 }
74
75 #endif //!TARGET_OS_IPHONE