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