1*1b191cb5SApple OSS Distributions /* test that the header doesn't implicitly depend on others */
2*1b191cb5SApple OSS Distributions #include <sys/work_interval.h>
3*1b191cb5SApple OSS Distributions
4*1b191cb5SApple OSS Distributions #include <stdlib.h>
5*1b191cb5SApple OSS Distributions #include <stdio.h>
6*1b191cb5SApple OSS Distributions #include <unistd.h>
7*1b191cb5SApple OSS Distributions #include <errno.h>
8*1b191cb5SApple OSS Distributions #include <err.h>
9*1b191cb5SApple OSS Distributions #include <string.h>
10*1b191cb5SApple OSS Distributions #include <pthread.h>
11*1b191cb5SApple OSS Distributions
12*1b191cb5SApple OSS Distributions #include <mach/mach.h>
13*1b191cb5SApple OSS Distributions
14*1b191cb5SApple OSS Distributions #include <darwintest.h>
15*1b191cb5SApple OSS Distributions
16*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
17*1b191cb5SApple OSS Distributions T_META_RUN_CONCURRENTLY(true));
18*1b191cb5SApple OSS Distributions
19*1b191cb5SApple OSS Distributions static mach_port_t port = MACH_PORT_NULL;
20*1b191cb5SApple OSS Distributions
21*1b191cb5SApple OSS Distributions static void *
joining_thread_fn(__unused void * arg)22*1b191cb5SApple OSS Distributions joining_thread_fn(__unused void *arg)
23*1b191cb5SApple OSS Distributions {
24*1b191cb5SApple OSS Distributions int ret = 0;
25*1b191cb5SApple OSS Distributions kern_return_t kr = KERN_SUCCESS;
26*1b191cb5SApple OSS Distributions
27*1b191cb5SApple OSS Distributions ret = work_interval_join_port(port);
28*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port, another thread");
29*1b191cb5SApple OSS Distributions
30*1b191cb5SApple OSS Distributions kr = mach_port_deallocate(mach_task_self(), port);
31*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_deallocate of port, another thread");
32*1b191cb5SApple OSS Distributions
33*1b191cb5SApple OSS Distributions /* deliberately exit with joined work interval */
34*1b191cb5SApple OSS Distributions return NULL;
35*1b191cb5SApple OSS Distributions }
36*1b191cb5SApple OSS Distributions
37*1b191cb5SApple OSS Distributions T_DECL(work_interval, "work interval interface")
38*1b191cb5SApple OSS Distributions {
39*1b191cb5SApple OSS Distributions int ret = 0;
40*1b191cb5SApple OSS Distributions work_interval_t handle = NULL;
41*1b191cb5SApple OSS Distributions uint64_t now = mach_absolute_time();
42*1b191cb5SApple OSS Distributions kern_return_t kr = KERN_SUCCESS;
43*1b191cb5SApple OSS Distributions
44*1b191cb5SApple OSS Distributions ret = work_interval_create(NULL, 0);
45*1b191cb5SApple OSS Distributions T_ASSERT_EQ(errno, EINVAL, "create with null errno EINVAL");
46*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, -1, "create with null returns -1");
47*1b191cb5SApple OSS Distributions
48*1b191cb5SApple OSS Distributions /* Binary must be entitled for this to succeed */
49*1b191cb5SApple OSS Distributions ret = work_interval_create(&handle, 0);
50*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create, no flags");
51*1b191cb5SApple OSS Distributions
52*1b191cb5SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
53*1b191cb5SApple OSS Distributions T_ASSERT_EQ(errno, EINVAL, "work_interval_copy_port on non-joinable interval errno EINVAL");
54*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, -1, "work_interval_copy_port on non-joinable interval returns -1");
55*1b191cb5SApple OSS Distributions
56*1b191cb5SApple OSS Distributions ret = work_interval_notify(handle, now - 1000, now, now + 1000, now + 2000, 0);
57*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_notify, no flags");
58*1b191cb5SApple OSS Distributions
59*1b191cb5SApple OSS Distributions ret = work_interval_destroy(handle);
60*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_destroy, no flags");
61*1b191cb5SApple OSS Distributions
62*1b191cb5SApple OSS Distributions uint32_t flags[] = {
63*1b191cb5SApple OSS Distributions WORK_INTERVAL_FLAG_JOINABLE,
64*1b191cb5SApple OSS Distributions WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_FLAG_GROUP,
65*1b191cb5SApple OSS Distributions };
66*1b191cb5SApple OSS Distributions
67*1b191cb5SApple OSS Distributions for (uint32_t i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
68*1b191cb5SApple OSS Distributions ret = work_interval_create(&handle, flags[i]);
69*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create, joinable");
70*1b191cb5SApple OSS Distributions
71*1b191cb5SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
72*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_copy_port, joinable");
73*1b191cb5SApple OSS Distributions
74*1b191cb5SApple OSS Distributions ret = work_interval_notify(handle, now - 1000, now, now + 1000, now + 2000, 0);
75*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, -1, "work_interval_notify on non-joined thread returns -1");
76*1b191cb5SApple OSS Distributions T_ASSERT_EQ(errno, EINVAL, "work_interval_copy_port on non-joined thread errno EINVAL");
77*1b191cb5SApple OSS Distributions
78*1b191cb5SApple OSS Distributions ret = work_interval_join_port(port);
79*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port, joinable");
80*1b191cb5SApple OSS Distributions
81*1b191cb5SApple OSS Distributions ret = work_interval_notify(handle, now - 1000, now, now + 1000, now + 2000, 0);
82*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_notify, on joined thread");
83*1b191cb5SApple OSS Distributions
84*1b191cb5SApple OSS Distributions ret = work_interval_join_port(port);
85*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port, join the same interval after destroy");
86*1b191cb5SApple OSS Distributions
87*1b191cb5SApple OSS Distributions kr = mach_port_deallocate(mach_task_self(), port);
88*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_deallocate of port");
89*1b191cb5SApple OSS Distributions
90*1b191cb5SApple OSS Distributions ret = work_interval_notify(handle, now - 1000, now, now + 1000, now + 2000, 0);
91*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_notify, on joined thread after destroy");
92*1b191cb5SApple OSS Distributions
93*1b191cb5SApple OSS Distributions ret = work_interval_destroy(handle);
94*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_destroy, joinable, on joined thread");
95*1b191cb5SApple OSS Distributions
96*1b191cb5SApple OSS Distributions ret = work_interval_leave();
97*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_leave, on destroyed work interval");
98*1b191cb5SApple OSS Distributions }
99*1b191cb5SApple OSS Distributions
100*1b191cb5SApple OSS Distributions ret = work_interval_create(&handle, WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_FLAG_GROUP);
101*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create, joinable");
102*1b191cb5SApple OSS Distributions
103*1b191cb5SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
104*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_copy_port, joinable");
105*1b191cb5SApple OSS Distributions
106*1b191cb5SApple OSS Distributions ret = work_interval_join_port(port);
107*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port, join before handing to another thread");
108*1b191cb5SApple OSS Distributions
109*1b191cb5SApple OSS Distributions pthread_t joining_thread;
110*1b191cb5SApple OSS Distributions
111*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&joining_thread, NULL, joining_thread_fn, NULL), "pthread_create");
112*1b191cb5SApple OSS Distributions
113*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_join(joining_thread, NULL), "pthread_join");
114*1b191cb5SApple OSS Distributions
115*1b191cb5SApple OSS Distributions ret = work_interval_leave();
116*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_leave");
117*1b191cb5SApple OSS Distributions
118*1b191cb5SApple OSS Distributions ret = work_interval_destroy(handle);
119*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_destroy");
120*1b191cb5SApple OSS Distributions }
121*1b191cb5SApple OSS Distributions
122*1b191cb5SApple OSS Distributions static mach_timebase_info_data_t timebase_info;
123*1b191cb5SApple OSS Distributions
124*1b191cb5SApple OSS Distributions static uint64_t
nanos_to_abs(uint64_t nanos)125*1b191cb5SApple OSS Distributions nanos_to_abs(uint64_t nanos)
126*1b191cb5SApple OSS Distributions {
127*1b191cb5SApple OSS Distributions mach_timebase_info(&timebase_info);
128*1b191cb5SApple OSS Distributions return nanos * timebase_info.denom / timebase_info.numer;
129*1b191cb5SApple OSS Distributions }
130*1b191cb5SApple OSS Distributions
131*1b191cb5SApple OSS Distributions static void
set_realtime(pthread_t thread)132*1b191cb5SApple OSS Distributions set_realtime(pthread_t thread)
133*1b191cb5SApple OSS Distributions {
134*1b191cb5SApple OSS Distributions kern_return_t kr;
135*1b191cb5SApple OSS Distributions thread_time_constraint_policy_data_t pol;
136*1b191cb5SApple OSS Distributions
137*1b191cb5SApple OSS Distributions mach_port_t target_thread = pthread_mach_thread_np(thread);
138*1b191cb5SApple OSS Distributions T_ASSERT_NOTNULL(target_thread, "pthread_mach_thread_np");
139*1b191cb5SApple OSS Distributions
140*1b191cb5SApple OSS Distributions /* 1s 100ms 10ms */
141*1b191cb5SApple OSS Distributions pol.period = (uint32_t)nanos_to_abs(1000000000);
142*1b191cb5SApple OSS Distributions pol.constraint = (uint32_t)nanos_to_abs(100000000);
143*1b191cb5SApple OSS Distributions pol.computation = (uint32_t)nanos_to_abs(10000000);
144*1b191cb5SApple OSS Distributions
145*1b191cb5SApple OSS Distributions pol.preemptible = 0; /* Ignored by OS */
146*1b191cb5SApple OSS Distributions kr = thread_policy_set(target_thread, THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t) &pol,
147*1b191cb5SApple OSS Distributions THREAD_TIME_CONSTRAINT_POLICY_COUNT);
148*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "thread_policy_set(THREAD_TIME_CONSTRAINT_POLICY)");
149*1b191cb5SApple OSS Distributions }
150*1b191cb5SApple OSS Distributions
151*1b191cb5SApple OSS Distributions static void
set_nonrealtime(pthread_t thread)152*1b191cb5SApple OSS Distributions set_nonrealtime(pthread_t thread)
153*1b191cb5SApple OSS Distributions {
154*1b191cb5SApple OSS Distributions kern_return_t kr;
155*1b191cb5SApple OSS Distributions thread_standard_policy_data_t pol = {0};
156*1b191cb5SApple OSS Distributions
157*1b191cb5SApple OSS Distributions mach_port_t target_thread = pthread_mach_thread_np(thread);
158*1b191cb5SApple OSS Distributions T_ASSERT_NOTNULL(target_thread, "pthread_mach_thread_np");
159*1b191cb5SApple OSS Distributions
160*1b191cb5SApple OSS Distributions kr = thread_policy_set(target_thread, THREAD_STANDARD_POLICY, (thread_policy_t) &pol,
161*1b191cb5SApple OSS Distributions THREAD_STANDARD_POLICY_COUNT);
162*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "thread_policy_set(THREAD_STANDARD_POLICY)");
163*1b191cb5SApple OSS Distributions }
164*1b191cb5SApple OSS Distributions
165*1b191cb5SApple OSS Distributions T_DECL(work_interval_audio_realtime_only, "joining RT threads to audio work interval", T_META_ASROOT(YES))
166*1b191cb5SApple OSS Distributions {
167*1b191cb5SApple OSS Distributions int ret = 0;
168*1b191cb5SApple OSS Distributions work_interval_t handle = NULL;
169*1b191cb5SApple OSS Distributions kern_return_t kr = KERN_SUCCESS;
170*1b191cb5SApple OSS Distributions
171*1b191cb5SApple OSS Distributions uint32_t flags = WORK_INTERVAL_FLAG_GROUP | WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_TYPE_COREAUDIO;
172*1b191cb5SApple OSS Distributions
173*1b191cb5SApple OSS Distributions ret = work_interval_create(&handle, flags);
174*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create, joinable");
175*1b191cb5SApple OSS Distributions
176*1b191cb5SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
177*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_copy_port, joinable");
178*1b191cb5SApple OSS Distributions
179*1b191cb5SApple OSS Distributions ret = work_interval_join_port(port);
180*1b191cb5SApple OSS Distributions T_EXPECT_POSIX_FAILURE(ret, EINVAL, "work_interval_join_port for audio on non-RT thread");
181*1b191cb5SApple OSS Distributions
182*1b191cb5SApple OSS Distributions set_realtime(pthread_self());
183*1b191cb5SApple OSS Distributions
184*1b191cb5SApple OSS Distributions ret = work_interval_join_port(port);
185*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_join_port for audio on RT thread");
186*1b191cb5SApple OSS Distributions
187*1b191cb5SApple OSS Distributions ret = work_interval_leave();
188*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_leave");
189*1b191cb5SApple OSS Distributions
190*1b191cb5SApple OSS Distributions ret = work_interval_destroy(handle);
191*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_destroy");
192*1b191cb5SApple OSS Distributions
193*1b191cb5SApple OSS Distributions kr = mach_port_deallocate(mach_task_self(), port);
194*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_deallocate of port");
195*1b191cb5SApple OSS Distributions
196*1b191cb5SApple OSS Distributions set_nonrealtime(pthread_self());
197*1b191cb5SApple OSS Distributions }
198*1b191cb5SApple OSS Distributions
199*1b191cb5SApple OSS Distributions T_DECL(work_interval_get_flags, "querying a port for create flags")
200*1b191cb5SApple OSS Distributions {
201*1b191cb5SApple OSS Distributions int ret = 0;
202*1b191cb5SApple OSS Distributions work_interval_t handle = NULL;
203*1b191cb5SApple OSS Distributions uint32_t flags = WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_FLAG_GROUP | WORK_INTERVAL_TYPE_COREAUDIO;
204*1b191cb5SApple OSS Distributions
205*1b191cb5SApple OSS Distributions ret = work_interval_create(&handle, flags);
206*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_create(AUDIO)");
207*1b191cb5SApple OSS Distributions
208*1b191cb5SApple OSS Distributions ret = work_interval_copy_port(handle, &port);
209*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "work_interval_copy_port");
210*1b191cb5SApple OSS Distributions T_ASSERT_TRUE(MACH_PORT_VALID(port), "port from copy port is a valid port");
211*1b191cb5SApple OSS Distributions
212*1b191cb5SApple OSS Distributions uint32_t expected_flags = 0;
213*1b191cb5SApple OSS Distributions
214*1b191cb5SApple OSS Distributions ret = work_interval_get_flags_from_port(port, &expected_flags);
215*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, 0, "work_interval_get_flags_from_port");
216*1b191cb5SApple OSS Distributions
217*1b191cb5SApple OSS Distributions T_ASSERT_EQ(expected_flags, flags, "Flags match with what work interval was created with");
218*1b191cb5SApple OSS Distributions
219*1b191cb5SApple OSS Distributions mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, -1);
220*1b191cb5SApple OSS Distributions work_interval_destroy(handle);
221*1b191cb5SApple OSS Distributions
222*1b191cb5SApple OSS Distributions // Negative test
223*1b191cb5SApple OSS Distributions
224*1b191cb5SApple OSS Distributions mach_port_t fake_port = MACH_PORT_NULL;
225*1b191cb5SApple OSS Distributions ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &fake_port);
226*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, 0, "successfully allocated a port");
227*1b191cb5SApple OSS Distributions T_ASSERT_TRUE(MACH_PORT_VALID(fake_port), "allocated port is valid");
228*1b191cb5SApple OSS Distributions
229*1b191cb5SApple OSS Distributions ret = mach_port_insert_right(mach_task_self(), fake_port, fake_port, MACH_MSG_TYPE_MAKE_SEND);
230*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, 0, "successfully inserted a send right");
231*1b191cb5SApple OSS Distributions
232*1b191cb5SApple OSS Distributions ret = work_interval_get_flags_from_port(fake_port, &expected_flags);
233*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, -1, "query port failed as expected");
234*1b191cb5SApple OSS Distributions
235*1b191cb5SApple OSS Distributions mach_port_mod_refs(mach_task_self(), fake_port, MACH_PORT_RIGHT_SEND, -1);
236*1b191cb5SApple OSS Distributions mach_port_mod_refs(mach_task_self(), fake_port, MACH_PORT_RIGHT_RECEIVE, -1);
237*1b191cb5SApple OSS Distributions }
238