xref: /xnu-8020.140.41/tests/workq/workq_sigprof.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
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 !defined(__arm__)
14 
15 T_GLOBAL_META(
16 	T_META_NAMESPACE("xnu.workq"),
17 	T_META_RADAR_COMPONENT_NAME("xnu"),
18 	T_META_RADAR_COMPONENT_VERSION("workq"),
19 	T_META_RUN_CONCURRENTLY(true));
20 
21 
22 static pthread_t workq_thread;
23 static bool signal_received;
24 
25 static void
signal_handler(int sig __unused,siginfo_t * b __unused,void * unused __unused)26 signal_handler(int sig __unused, siginfo_t *b __unused, void* unused __unused)
27 {
28 	if (pthread_self() == workq_thread) {
29 		signal_received = true;
30 	}
31 }
32 
33 static void
workq_block(void * unused __unused)34 workq_block(void *unused __unused)
35 {
36 	workq_thread = pthread_self();
37 
38 	/*
39 	 *  sigset_t set;
40 	 *  sigemptyset(&set);
41 	 *  sigaddset(&set, SIGPROF);
42 	 *  pthread_sigmask(SIG_UNBLOCK, &set, NULL);
43 	 */
44 
45 	uint64_t spin_start = mach_absolute_time();
46 	while (mach_absolute_time() - spin_start < 30 * NSEC_PER_SEC) {
47 		if (signal_received) {
48 			T_PASS("Got SIGPROF!");
49 			T_END;
50 		}
51 	}
52 }
53 
54 T_DECL(workq_sigprof, "test that workqueue threads can receive sigprof")
55 {
56 	struct sigaction sa = {
57 		.sa_sigaction = signal_handler
58 	};
59 	sigfillset(&sa.sa_mask);
60 	T_ASSERT_POSIX_ZERO(sigaction(SIGPROF, &sa, NULL), NULL);
61 
62 	dispatch_queue_t q = dispatch_get_global_queue(0, 0);
63 	dispatch_async_f(q, NULL, workq_block);
64 
65 	struct itimerval timerval = {
66 		.it_interval = {.tv_usec = 10000},
67 		.it_value = {.tv_usec = 10000}
68 	};
69 	T_ASSERT_POSIX_ZERO(setitimer(ITIMER_PROF, &timerval, NULL), NULL);
70 
71 	dispatch_main();
72 }
73 
74 #else //!defined(__arm__)
75 
76 T_DECL(workq_sigprof, "test that workqueue threads can receive sigprof")
77 {
78 	T_EXPECTFAIL;
79 	T_FAIL("<rdar://problem/25864196> setitimer/sigprof not supported on 32bit arm platforms");
80 }
81 
82 #endif //!defined(__arm__)
83