1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * mach eventlink: Tests mach eventlink kernel synchronization primitive.
3*1b191cb5SApple OSS Distributions */
4*1b191cb5SApple OSS Distributions
5*1b191cb5SApple OSS Distributions #include <darwintest.h>
6*1b191cb5SApple OSS Distributions #include <darwintest_multiprocess.h>
7*1b191cb5SApple OSS Distributions
8*1b191cb5SApple OSS Distributions #include <pthread.h>
9*1b191cb5SApple OSS Distributions #include <launch.h>
10*1b191cb5SApple OSS Distributions #include <mach/mach.h>
11*1b191cb5SApple OSS Distributions #include <mach/message.h>
12*1b191cb5SApple OSS Distributions #include <mach/mach_voucher.h>
13*1b191cb5SApple OSS Distributions #include <pthread/workqueue_private.h>
14*1b191cb5SApple OSS Distributions #include <voucher/ipc_pthread_priority_types.h>
15*1b191cb5SApple OSS Distributions #include <servers/bootstrap.h>
16*1b191cb5SApple OSS Distributions #include <stdlib.h>
17*1b191cb5SApple OSS Distributions #include <sys/event.h>
18*1b191cb5SApple OSS Distributions #include <unistd.h>
19*1b191cb5SApple OSS Distributions #include <crt_externs.h>
20*1b191cb5SApple OSS Distributions #include <signal.h>
21*1b191cb5SApple OSS Distributions #include <sys/types.h>
22*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
23*1b191cb5SApple OSS Distributions #include <libkern/OSAtomic.h>
24*1b191cb5SApple OSS Distributions #include <sys/wait.h>
25*1b191cb5SApple OSS Distributions #include <spawn.h>
26*1b191cb5SApple OSS Distributions #include <spawn_private.h>
27*1b191cb5SApple OSS Distributions #include <mach/mach_eventlink.h>
28*1b191cb5SApple OSS Distributions
29*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.mach_eventlink"),
30*1b191cb5SApple OSS Distributions T_META_RUN_CONCURRENTLY(true));
31*1b191cb5SApple OSS Distributions
32*1b191cb5SApple OSS Distributions static kern_return_t
test_eventlink_create(mach_port_t * port_pair)33*1b191cb5SApple OSS Distributions test_eventlink_create(mach_port_t *port_pair)
34*1b191cb5SApple OSS Distributions {
35*1b191cb5SApple OSS Distributions kern_return_t kr;
36*1b191cb5SApple OSS Distributions
37*1b191cb5SApple OSS Distributions kr = mach_eventlink_create(mach_task_self(), MELC_OPTION_NO_COPYIN, port_pair);
38*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_create");
39*1b191cb5SApple OSS Distributions
40*1b191cb5SApple OSS Distributions return kr;
41*1b191cb5SApple OSS Distributions }
42*1b191cb5SApple OSS Distributions
43*1b191cb5SApple OSS Distributions static pthread_t
thread_create_for_test(void * (* function)(void *),void * arg)44*1b191cb5SApple OSS Distributions thread_create_for_test(void * (*function)(void *), void *arg)
45*1b191cb5SApple OSS Distributions {
46*1b191cb5SApple OSS Distributions pthread_t pthread;
47*1b191cb5SApple OSS Distributions pthread_attr_t attr;
48*1b191cb5SApple OSS Distributions
49*1b191cb5SApple OSS Distributions pthread_attr_init(&attr);
50*1b191cb5SApple OSS Distributions pthread_create(&pthread, &attr, function, arg);
51*1b191cb5SApple OSS Distributions
52*1b191cb5SApple OSS Distributions T_LOG("pthread created\n");
53*1b191cb5SApple OSS Distributions return pthread;
54*1b191cb5SApple OSS Distributions }
55*1b191cb5SApple OSS Distributions
56*1b191cb5SApple OSS Distributions static void *
while1loop(void * arg)57*1b191cb5SApple OSS Distributions while1loop(void *arg)
58*1b191cb5SApple OSS Distributions {
59*1b191cb5SApple OSS Distributions arg = NULL;
60*1b191cb5SApple OSS Distributions while (1) {
61*1b191cb5SApple OSS Distributions ;
62*1b191cb5SApple OSS Distributions }
63*1b191cb5SApple OSS Distributions return NULL;
64*1b191cb5SApple OSS Distributions }
65*1b191cb5SApple OSS Distributions
66*1b191cb5SApple OSS Distributions static void *
test_eventlink_wait_with_timeout(void * arg)67*1b191cb5SApple OSS Distributions test_eventlink_wait_with_timeout(void *arg)
68*1b191cb5SApple OSS Distributions {
69*1b191cb5SApple OSS Distributions kern_return_t kr;
70*1b191cb5SApple OSS Distributions mach_port_t eventlink_port = (mach_port_t) (uintptr_t)arg;
71*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
72*1b191cb5SApple OSS Distributions uint64_t ticks = mach_absolute_time();
73*1b191cb5SApple OSS Distributions uint64_t count = 1;
74*1b191cb5SApple OSS Distributions
75*1b191cb5SApple OSS Distributions /* Associate thread with eventlink port */
76*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(eventlink_port, self, 0, 0, 0, 0, MELA_OPTION_NONE);
77*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate");
78*1b191cb5SApple OSS Distributions
79*1b191cb5SApple OSS Distributions /* Wait on the eventlink with timeout */
80*1b191cb5SApple OSS Distributions kr = mach_eventlink_wait_until(eventlink_port, &count, MELSW_OPTION_NONE,
81*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, ticks + 5000);
82*1b191cb5SApple OSS Distributions
83*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_OPERATION_TIMED_OUT, "mach_eventlink_wait_until returned expected error");
84*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)0, "mach_eventlink_wait_until returned correct count value");
85*1b191cb5SApple OSS Distributions
86*1b191cb5SApple OSS Distributions return NULL;
87*1b191cb5SApple OSS Distributions }
88*1b191cb5SApple OSS Distributions
89*1b191cb5SApple OSS Distributions static void *
test_eventlink_wait_no_wait(void * arg)90*1b191cb5SApple OSS Distributions test_eventlink_wait_no_wait(void *arg)
91*1b191cb5SApple OSS Distributions {
92*1b191cb5SApple OSS Distributions kern_return_t kr;
93*1b191cb5SApple OSS Distributions mach_port_t eventlink_port = (mach_port_t) (uintptr_t)arg;
94*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
95*1b191cb5SApple OSS Distributions uint64_t count = 1;
96*1b191cb5SApple OSS Distributions
97*1b191cb5SApple OSS Distributions /* Associate thread with eventlink port */
98*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(eventlink_port, self, 0, 0, 0, 0, MELA_OPTION_NONE);
99*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate");
100*1b191cb5SApple OSS Distributions
101*1b191cb5SApple OSS Distributions /* Wait on the eventlink */
102*1b191cb5SApple OSS Distributions kr = mach_eventlink_wait_until(eventlink_port, &count, MELSW_OPTION_NO_WAIT,
103*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
104*1b191cb5SApple OSS Distributions
105*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_OPERATION_TIMED_OUT, "mach_eventlink_wait_until returned expected error");
106*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)0, "mach_eventlink_wait_until returned correct count value");
107*1b191cb5SApple OSS Distributions
108*1b191cb5SApple OSS Distributions return NULL;
109*1b191cb5SApple OSS Distributions }
110*1b191cb5SApple OSS Distributions
111*1b191cb5SApple OSS Distributions static void *
test_eventlink_wait_destroy(void * arg)112*1b191cb5SApple OSS Distributions test_eventlink_wait_destroy(void *arg)
113*1b191cb5SApple OSS Distributions {
114*1b191cb5SApple OSS Distributions kern_return_t kr;
115*1b191cb5SApple OSS Distributions mach_port_t eventlink_port = (mach_port_t) (uintptr_t)arg;
116*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
117*1b191cb5SApple OSS Distributions uint64_t count = 1;
118*1b191cb5SApple OSS Distributions
119*1b191cb5SApple OSS Distributions /* Associate thread with eventlink port */
120*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(eventlink_port, self, 0, 0, 0, 0, MELA_OPTION_NONE);
121*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate");
122*1b191cb5SApple OSS Distributions
123*1b191cb5SApple OSS Distributions /* Wait on the eventlink */
124*1b191cb5SApple OSS Distributions kr = mach_eventlink_wait_until(eventlink_port, &count, MELSW_OPTION_NONE,
125*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
126*1b191cb5SApple OSS Distributions
127*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_TERMINATED, "mach_eventlink_wait_until returned expected error");
128*1b191cb5SApple OSS Distributions
129*1b191cb5SApple OSS Distributions return NULL;
130*1b191cb5SApple OSS Distributions }
131*1b191cb5SApple OSS Distributions
132*1b191cb5SApple OSS Distributions static void *
test_eventlink_wait_for_signal(void * arg)133*1b191cb5SApple OSS Distributions test_eventlink_wait_for_signal(void *arg)
134*1b191cb5SApple OSS Distributions {
135*1b191cb5SApple OSS Distributions kern_return_t kr;
136*1b191cb5SApple OSS Distributions mach_port_t eventlink_port = (mach_port_t) (uintptr_t)arg;
137*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
138*1b191cb5SApple OSS Distributions uint64_t count = 0;
139*1b191cb5SApple OSS Distributions
140*1b191cb5SApple OSS Distributions /* Associate thread with eventlink port */
141*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(eventlink_port, self, 0, 0, 0, 0, MELA_OPTION_NONE);
142*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate");
143*1b191cb5SApple OSS Distributions
144*1b191cb5SApple OSS Distributions /* Wait on the eventlink */
145*1b191cb5SApple OSS Distributions kr = mach_eventlink_wait_until(eventlink_port, &count, MELSW_OPTION_NONE,
146*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
147*1b191cb5SApple OSS Distributions
148*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_wait_until");
149*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_wait_until returned correct count value");
150*1b191cb5SApple OSS Distributions
151*1b191cb5SApple OSS Distributions return NULL;
152*1b191cb5SApple OSS Distributions }
153*1b191cb5SApple OSS Distributions
154*1b191cb5SApple OSS Distributions static void *
test_eventlink_wait_then_signal(void * arg)155*1b191cb5SApple OSS Distributions test_eventlink_wait_then_signal(void *arg)
156*1b191cb5SApple OSS Distributions {
157*1b191cb5SApple OSS Distributions kern_return_t kr;
158*1b191cb5SApple OSS Distributions mach_port_t eventlink_port = (mach_port_t) (uintptr_t)arg;
159*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
160*1b191cb5SApple OSS Distributions uint64_t count = 0;
161*1b191cb5SApple OSS Distributions
162*1b191cb5SApple OSS Distributions /* Associate thread with eventlink port */
163*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(eventlink_port, self, 0, 0, 0, 0, MELA_OPTION_NONE);
164*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate");
165*1b191cb5SApple OSS Distributions
166*1b191cb5SApple OSS Distributions /* Wait on the eventlink */
167*1b191cb5SApple OSS Distributions kr = mach_eventlink_wait_until(eventlink_port, &count, MELSW_OPTION_NONE,
168*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
169*1b191cb5SApple OSS Distributions
170*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_wait_until");
171*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_wait_until returned correct count value");
172*1b191cb5SApple OSS Distributions
173*1b191cb5SApple OSS Distributions /* Signal the eventlink to wakeup other side */
174*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal(eventlink_port, 0);
175*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_signal");
176*1b191cb5SApple OSS Distributions
177*1b191cb5SApple OSS Distributions return NULL;
178*1b191cb5SApple OSS Distributions }
179*1b191cb5SApple OSS Distributions
180*1b191cb5SApple OSS Distributions static void *
test_eventlink_wait_then_wait_signal_with_no_wait(void * arg)181*1b191cb5SApple OSS Distributions test_eventlink_wait_then_wait_signal_with_no_wait(void *arg)
182*1b191cb5SApple OSS Distributions {
183*1b191cb5SApple OSS Distributions kern_return_t kr;
184*1b191cb5SApple OSS Distributions mach_port_t eventlink_port = (mach_port_t) (uintptr_t)arg;
185*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
186*1b191cb5SApple OSS Distributions uint64_t count = 0;
187*1b191cb5SApple OSS Distributions
188*1b191cb5SApple OSS Distributions /* Associate thread with eventlink port */
189*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(eventlink_port, self, 0, 0, 0, 0, MELA_OPTION_NONE);
190*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate");
191*1b191cb5SApple OSS Distributions
192*1b191cb5SApple OSS Distributions /* Wait on the eventlink */
193*1b191cb5SApple OSS Distributions kr = mach_eventlink_wait_until(eventlink_port, &count, MELSW_OPTION_NONE,
194*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
195*1b191cb5SApple OSS Distributions
196*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_wait_until");
197*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_wait_until returned correct count value");
198*1b191cb5SApple OSS Distributions
199*1b191cb5SApple OSS Distributions /* Signal wait the eventlink */
200*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal_wait_until(eventlink_port, &count, 0, MELSW_OPTION_NO_WAIT,
201*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
202*1b191cb5SApple OSS Distributions
203*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_OPERATION_TIMED_OUT, "mach_eventlink_wait_until returned expected error");
204*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_wait_until returned correct count value");
205*1b191cb5SApple OSS Distributions
206*1b191cb5SApple OSS Distributions return NULL;
207*1b191cb5SApple OSS Distributions }
208*1b191cb5SApple OSS Distributions
209*1b191cb5SApple OSS Distributions static void *
test_eventlink_wait_then_wait_signal_with_prepost(void * arg)210*1b191cb5SApple OSS Distributions test_eventlink_wait_then_wait_signal_with_prepost(void *arg)
211*1b191cb5SApple OSS Distributions {
212*1b191cb5SApple OSS Distributions kern_return_t kr;
213*1b191cb5SApple OSS Distributions mach_port_t eventlink_port = (mach_port_t) (uintptr_t)arg;
214*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
215*1b191cb5SApple OSS Distributions uint64_t count = 0;
216*1b191cb5SApple OSS Distributions
217*1b191cb5SApple OSS Distributions /* Associate thread with eventlink port */
218*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(eventlink_port, self, 0, 0, 0, 0, MELA_OPTION_NONE);
219*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate");
220*1b191cb5SApple OSS Distributions
221*1b191cb5SApple OSS Distributions /* Wait on the eventlink */
222*1b191cb5SApple OSS Distributions kr = mach_eventlink_wait_until(eventlink_port, &count, MELSW_OPTION_NONE,
223*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
224*1b191cb5SApple OSS Distributions
225*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_wait_until");
226*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_wait_until returned correct count value");
227*1b191cb5SApple OSS Distributions
228*1b191cb5SApple OSS Distributions /* Signal wait the eventlink with stale counter value */
229*1b191cb5SApple OSS Distributions count = 0;
230*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal_wait_until(eventlink_port, &count, 0, MELSW_OPTION_NONE,
231*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
232*1b191cb5SApple OSS Distributions
233*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_signal_wait_until");
234*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_wait_until returned correct count value");
235*1b191cb5SApple OSS Distributions
236*1b191cb5SApple OSS Distributions return NULL;
237*1b191cb5SApple OSS Distributions }
238*1b191cb5SApple OSS Distributions
239*1b191cb5SApple OSS Distributions static void *
test_eventlink_wait_then_signal_loop(void * arg)240*1b191cb5SApple OSS Distributions test_eventlink_wait_then_signal_loop(void *arg)
241*1b191cb5SApple OSS Distributions {
242*1b191cb5SApple OSS Distributions kern_return_t kr;
243*1b191cb5SApple OSS Distributions mach_port_t eventlink_port = (mach_port_t) (uintptr_t)arg;
244*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
245*1b191cb5SApple OSS Distributions uint64_t count = 0;
246*1b191cb5SApple OSS Distributions int i;
247*1b191cb5SApple OSS Distributions
248*1b191cb5SApple OSS Distributions /* Associate thread with eventlink port */
249*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(eventlink_port, self, 0, 0, 0, 0, MELA_OPTION_NONE);
250*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate");
251*1b191cb5SApple OSS Distributions
252*1b191cb5SApple OSS Distributions /* Wait on the eventlink */
253*1b191cb5SApple OSS Distributions kr = mach_eventlink_wait_until(eventlink_port, &count, MELSW_OPTION_NONE,
254*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
255*1b191cb5SApple OSS Distributions
256*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_wait_until");
257*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_wait_until returned correct count value");
258*1b191cb5SApple OSS Distributions
259*1b191cb5SApple OSS Distributions for (i = 1; i < 100; i++) {
260*1b191cb5SApple OSS Distributions /* Signal wait the eventlink */
261*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal_wait_until(eventlink_port, &count, 0, MELSW_OPTION_NONE,
262*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
263*1b191cb5SApple OSS Distributions
264*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_signal_wait_until");
265*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)(i + 1), "mach_eventlink_wait_until returned correct count value");
266*1b191cb5SApple OSS Distributions }
267*1b191cb5SApple OSS Distributions
268*1b191cb5SApple OSS Distributions /* Signal the eventlink to wakeup other side */
269*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal(eventlink_port, 0);
270*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_signal");
271*1b191cb5SApple OSS Distributions
272*1b191cb5SApple OSS Distributions return NULL;
273*1b191cb5SApple OSS Distributions }
274*1b191cb5SApple OSS Distributions
275*1b191cb5SApple OSS Distributions /*
276*1b191cb5SApple OSS Distributions * Test 1: Create ipc eventlink kernel object.
277*1b191cb5SApple OSS Distributions *
278*1b191cb5SApple OSS Distributions * Calls eventlink creates which returns a pair of eventlink port objects.
279*1b191cb5SApple OSS Distributions */
280*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_create, "eventlink create test", T_META_ASROOT(YES))
281*1b191cb5SApple OSS Distributions {
282*1b191cb5SApple OSS Distributions kern_return_t kr;
283*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
284*1b191cb5SApple OSS Distributions
285*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
286*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
287*1b191cb5SApple OSS Distributions return;
288*1b191cb5SApple OSS Distributions }
289*1b191cb5SApple OSS Distributions
290*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
291*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
292*1b191cb5SApple OSS Distributions }
293*1b191cb5SApple OSS Distributions
294*1b191cb5SApple OSS Distributions /*
295*1b191cb5SApple OSS Distributions * Test 2: Create ipc eventlink kernel object and call eventlink destroy
296*1b191cb5SApple OSS Distributions *
297*1b191cb5SApple OSS Distributions * Calls eventlink creates which returns a pair of eventlink port objects.
298*1b191cb5SApple OSS Distributions * Calls eventlink destroy on eventlink port pair.
299*1b191cb5SApple OSS Distributions */
300*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_destroy, "eventlink destroy test", T_META_ASROOT(YES))
301*1b191cb5SApple OSS Distributions {
302*1b191cb5SApple OSS Distributions kern_return_t kr;
303*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
304*1b191cb5SApple OSS Distributions
305*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
306*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
307*1b191cb5SApple OSS Distributions return;
308*1b191cb5SApple OSS Distributions }
309*1b191cb5SApple OSS Distributions
310*1b191cb5SApple OSS Distributions kr = mach_eventlink_destroy(port_pair[0]);
311*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_destroy");
312*1b191cb5SApple OSS Distributions kr = mach_eventlink_destroy(port_pair[1]);
313*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_destroy");
314*1b191cb5SApple OSS Distributions }
315*1b191cb5SApple OSS Distributions
316*1b191cb5SApple OSS Distributions /*
317*1b191cb5SApple OSS Distributions * Test 3: Associate threads to eventlink object.
318*1b191cb5SApple OSS Distributions *
319*1b191cb5SApple OSS Distributions * Create eventlink object pair and associate threads to each side and then
320*1b191cb5SApple OSS Distributions * disassociate threads and check for error conditions.
321*1b191cb5SApple OSS Distributions */
322*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_associate, "eventlink associate test", T_META_ASROOT(YES))
323*1b191cb5SApple OSS Distributions {
324*1b191cb5SApple OSS Distributions kern_return_t kr;
325*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
326*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
327*1b191cb5SApple OSS Distributions mach_port_t other_thread = MACH_PORT_NULL;
328*1b191cb5SApple OSS Distributions pthread_t pthread;
329*1b191cb5SApple OSS Distributions
330*1b191cb5SApple OSS Distributions /* eventlink associate to NULL eventlink object */
331*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(MACH_PORT_NULL, self, 0, 0, 0, 0, MELA_OPTION_NONE);
332*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, MACH_SEND_INVALID_DEST, "mach_eventlink_associate with null eventlink returned expected error");
333*1b191cb5SApple OSS Distributions
334*1b191cb5SApple OSS Distributions /* eventlink disassociate to NULL eventlink object */
335*1b191cb5SApple OSS Distributions kr = mach_eventlink_disassociate(MACH_PORT_NULL, MELD_OPTION_NONE);
336*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, MACH_SEND_INVALID_DEST, "mach_eventlink_disassociate with null eventlink returned expected error");
337*1b191cb5SApple OSS Distributions
338*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
339*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
340*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
341*1b191cb5SApple OSS Distributions return;
342*1b191cb5SApple OSS Distributions }
343*1b191cb5SApple OSS Distributions
344*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(while1loop, NULL);
345*1b191cb5SApple OSS Distributions other_thread = pthread_mach_thread_np(pthread);
346*1b191cb5SApple OSS Distributions
347*1b191cb5SApple OSS Distributions for (int i = 0; i < 3; i++) {
348*1b191cb5SApple OSS Distributions /* Associate thread to eventlink objects */
349*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[0], self, 0, 0, 0, 0, MELA_OPTION_NONE);
350*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate for object 1");
351*1b191cb5SApple OSS Distributions
352*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[1], other_thread, 0, 0, 0, 0, MELA_OPTION_NONE);
353*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate for object 2");
354*1b191cb5SApple OSS Distributions
355*1b191cb5SApple OSS Distributions /* Try to associate again with diff threads, expect failure */
356*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[0], other_thread, 0, 0, 0, 0, MELA_OPTION_NONE);
357*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_NAME_EXISTS, "mach_eventlink_associate for associated "
358*1b191cb5SApple OSS Distributions "objects returned expected error");
359*1b191cb5SApple OSS Distributions
360*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[1], self, 0, 0, 0, 0, MELA_OPTION_NONE);
361*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_NAME_EXISTS, "mach_eventlink_associate for associated "
362*1b191cb5SApple OSS Distributions "objects return expected error");
363*1b191cb5SApple OSS Distributions
364*1b191cb5SApple OSS Distributions /* Try to disassociate the threads */
365*1b191cb5SApple OSS Distributions kr = mach_eventlink_disassociate(port_pair[0], MELD_OPTION_NONE);
366*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_disassociate for object 1");
367*1b191cb5SApple OSS Distributions
368*1b191cb5SApple OSS Distributions kr = mach_eventlink_disassociate(port_pair[1], MELD_OPTION_NONE);
369*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_disassociate for object 2");
370*1b191cb5SApple OSS Distributions
371*1b191cb5SApple OSS Distributions /* Try to disassociate the threads again, expect failure */
372*1b191cb5SApple OSS Distributions kr = mach_eventlink_disassociate(port_pair[0], MELD_OPTION_NONE);
373*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT, "mach_eventlink_disassociate for "
374*1b191cb5SApple OSS Distributions "disassociated objects returned expected error");
375*1b191cb5SApple OSS Distributions
376*1b191cb5SApple OSS Distributions kr = mach_eventlink_disassociate(port_pair[1], MELD_OPTION_NONE);
377*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT, "mach_eventlink_disassociate for "
378*1b191cb5SApple OSS Distributions "disassociated objects returned expected error");
379*1b191cb5SApple OSS Distributions }
380*1b191cb5SApple OSS Distributions
381*1b191cb5SApple OSS Distributions kr = mach_eventlink_destroy(port_pair[0]);
382*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_destroy");
383*1b191cb5SApple OSS Distributions
384*1b191cb5SApple OSS Distributions /* Try disassociate on other end of destoryed eventlink pair */
385*1b191cb5SApple OSS Distributions kr = mach_eventlink_disassociate(port_pair[1], MELD_OPTION_NONE);
386*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_TERMINATED, "mach_eventlink_disassociate for "
387*1b191cb5SApple OSS Distributions "terminated object returned expected error");
388*1b191cb5SApple OSS Distributions
389*1b191cb5SApple OSS Distributions kr = mach_eventlink_destroy(port_pair[1]);
390*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_destroy");
391*1b191cb5SApple OSS Distributions }
392*1b191cb5SApple OSS Distributions
393*1b191cb5SApple OSS Distributions /*
394*1b191cb5SApple OSS Distributions * Test 4: Test eventlink wait with timeout.
395*1b191cb5SApple OSS Distributions *
396*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads and test eventlink wait with timeout.
397*1b191cb5SApple OSS Distributions */
398*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_timeout, "eventlink wait timeout test", T_META_ASROOT(YES))
399*1b191cb5SApple OSS Distributions {
400*1b191cb5SApple OSS Distributions kern_return_t kr;
401*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
402*1b191cb5SApple OSS Distributions pthread_t pthread;
403*1b191cb5SApple OSS Distributions
404*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
405*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
406*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
407*1b191cb5SApple OSS Distributions return;
408*1b191cb5SApple OSS Distributions }
409*1b191cb5SApple OSS Distributions
410*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_with_timeout, (void *)(uintptr_t)port_pair[0]);
411*1b191cb5SApple OSS Distributions sleep(10);
412*1b191cb5SApple OSS Distributions
413*1b191cb5SApple OSS Distributions /* destroy the eventlink object, the wake status of thread will check if the test passsed or failed */
414*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
415*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
416*1b191cb5SApple OSS Distributions
417*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
418*1b191cb5SApple OSS Distributions }
419*1b191cb5SApple OSS Distributions
420*1b191cb5SApple OSS Distributions /*
421*1b191cb5SApple OSS Distributions * Test 5: Test eventlink wait with no wait.
422*1b191cb5SApple OSS Distributions *
423*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads and test eventlink wait with no wait flag.
424*1b191cb5SApple OSS Distributions */
425*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_no_wait, "eventlink wait no wait test", T_META_ASROOT(YES))
426*1b191cb5SApple OSS Distributions {
427*1b191cb5SApple OSS Distributions kern_return_t kr;
428*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
429*1b191cb5SApple OSS Distributions pthread_t pthread;
430*1b191cb5SApple OSS Distributions
431*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
432*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
433*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
434*1b191cb5SApple OSS Distributions return;
435*1b191cb5SApple OSS Distributions }
436*1b191cb5SApple OSS Distributions
437*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_no_wait, (void *)(uintptr_t)port_pair[0]);
438*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
439*1b191cb5SApple OSS Distributions
440*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
441*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
442*1b191cb5SApple OSS Distributions }
443*1b191cb5SApple OSS Distributions
444*1b191cb5SApple OSS Distributions /*
445*1b191cb5SApple OSS Distributions * Test 6: Test eventlink wait and destroy.
446*1b191cb5SApple OSS Distributions *
447*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads and destroy the port.
448*1b191cb5SApple OSS Distributions */
449*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_and_destroy, "eventlink wait and destroy", T_META_ASROOT(YES))
450*1b191cb5SApple OSS Distributions {
451*1b191cb5SApple OSS Distributions kern_return_t kr;
452*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
453*1b191cb5SApple OSS Distributions pthread_t pthread;
454*1b191cb5SApple OSS Distributions
455*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
456*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
457*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
458*1b191cb5SApple OSS Distributions return;
459*1b191cb5SApple OSS Distributions }
460*1b191cb5SApple OSS Distributions
461*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_destroy, (void *)(uintptr_t)port_pair[0]);
462*1b191cb5SApple OSS Distributions
463*1b191cb5SApple OSS Distributions sleep(5);
464*1b191cb5SApple OSS Distributions
465*1b191cb5SApple OSS Distributions /* Increase the send right count for port before destroy to make sure no sender does not fire on destroy */
466*1b191cb5SApple OSS Distributions kr = mach_port_mod_refs(mach_task_self(), port_pair[0], MACH_PORT_RIGHT_SEND, 2);
467*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_mod_refs");
468*1b191cb5SApple OSS Distributions
469*1b191cb5SApple OSS Distributions /* Destroy the port for thread to wakeup */
470*1b191cb5SApple OSS Distributions kr = mach_eventlink_destroy(port_pair[0]);
471*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_destroy");
472*1b191cb5SApple OSS Distributions
473*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
474*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
475*1b191cb5SApple OSS Distributions }
476*1b191cb5SApple OSS Distributions
477*1b191cb5SApple OSS Distributions
478*1b191cb5SApple OSS Distributions /*
479*1b191cb5SApple OSS Distributions * Test 7: Test eventlink wait and destroy remote side.
480*1b191cb5SApple OSS Distributions *
481*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads, wait and destroy the remote eventlink port.
482*1b191cb5SApple OSS Distributions */
483*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_and_destroy_remote, "eventlink wait and remote destroy", T_META_ASROOT(YES))
484*1b191cb5SApple OSS Distributions {
485*1b191cb5SApple OSS Distributions kern_return_t kr;
486*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
487*1b191cb5SApple OSS Distributions pthread_t pthread;
488*1b191cb5SApple OSS Distributions
489*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
490*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
491*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
492*1b191cb5SApple OSS Distributions return;
493*1b191cb5SApple OSS Distributions }
494*1b191cb5SApple OSS Distributions
495*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_destroy, (void *)(uintptr_t)port_pair[0]);
496*1b191cb5SApple OSS Distributions
497*1b191cb5SApple OSS Distributions sleep(5);
498*1b191cb5SApple OSS Distributions
499*1b191cb5SApple OSS Distributions /* Increase the send right count for port before destroy to make sure no sender does not fire on destroy */
500*1b191cb5SApple OSS Distributions kr = mach_port_mod_refs(mach_task_self(), port_pair[1], MACH_PORT_RIGHT_SEND, 2);
501*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_mod_refs");
502*1b191cb5SApple OSS Distributions
503*1b191cb5SApple OSS Distributions /* Destroy the port for thread to wakeup */
504*1b191cb5SApple OSS Distributions kr = mach_eventlink_destroy(port_pair[1]);
505*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_destroy");
506*1b191cb5SApple OSS Distributions
507*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
508*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
509*1b191cb5SApple OSS Distributions }
510*1b191cb5SApple OSS Distributions
511*1b191cb5SApple OSS Distributions /*
512*1b191cb5SApple OSS Distributions * Test 8: Test eventlink wait and deallocate port.
513*1b191cb5SApple OSS Distributions *
514*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads, wait and deallocate the eventlink port.
515*1b191cb5SApple OSS Distributions */
516*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_and_deallocate, "eventlink wait and deallocate", T_META_ASROOT(YES))
517*1b191cb5SApple OSS Distributions {
518*1b191cb5SApple OSS Distributions kern_return_t kr;
519*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
520*1b191cb5SApple OSS Distributions pthread_t pthread;
521*1b191cb5SApple OSS Distributions
522*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
523*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
524*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
525*1b191cb5SApple OSS Distributions return;
526*1b191cb5SApple OSS Distributions }
527*1b191cb5SApple OSS Distributions
528*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_destroy, (void *)(uintptr_t)port_pair[0]);
529*1b191cb5SApple OSS Distributions
530*1b191cb5SApple OSS Distributions sleep(5);
531*1b191cb5SApple OSS Distributions
532*1b191cb5SApple OSS Distributions /* Destroy the port for thread to wakeup */
533*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
534*1b191cb5SApple OSS Distributions
535*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
536*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
537*1b191cb5SApple OSS Distributions }
538*1b191cb5SApple OSS Distributions
539*1b191cb5SApple OSS Distributions /*
540*1b191cb5SApple OSS Distributions * Test 9: Test eventlink wait and disassociate.
541*1b191cb5SApple OSS Distributions *
542*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads, wait and disassociate thread from the eventlink port.
543*1b191cb5SApple OSS Distributions */
544*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_and_disassociate, "eventlink wait and disassociate", T_META_ASROOT(YES))
545*1b191cb5SApple OSS Distributions {
546*1b191cb5SApple OSS Distributions kern_return_t kr;
547*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
548*1b191cb5SApple OSS Distributions pthread_t pthread;
549*1b191cb5SApple OSS Distributions
550*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
551*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
552*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
553*1b191cb5SApple OSS Distributions return;
554*1b191cb5SApple OSS Distributions }
555*1b191cb5SApple OSS Distributions
556*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_destroy, (void *)(uintptr_t)port_pair[0]);
557*1b191cb5SApple OSS Distributions
558*1b191cb5SApple OSS Distributions sleep(5);
559*1b191cb5SApple OSS Distributions
560*1b191cb5SApple OSS Distributions /* Disassociate thread from eventlink for thread to wakeup */
561*1b191cb5SApple OSS Distributions kr = mach_eventlink_disassociate(port_pair[0], MELD_OPTION_NONE);
562*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_disassociate");
563*1b191cb5SApple OSS Distributions
564*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
565*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
566*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
567*1b191cb5SApple OSS Distributions }
568*1b191cb5SApple OSS Distributions
569*1b191cb5SApple OSS Distributions /*
570*1b191cb5SApple OSS Distributions * Test 10: Test eventlink wait and signal.
571*1b191cb5SApple OSS Distributions *
572*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads and test wait signal.
573*1b191cb5SApple OSS Distributions */
574*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_and_signal, "eventlink wait and signal", T_META_ASROOT(YES))
575*1b191cb5SApple OSS Distributions {
576*1b191cb5SApple OSS Distributions kern_return_t kr;
577*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
578*1b191cb5SApple OSS Distributions pthread_t pthread;
579*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
580*1b191cb5SApple OSS Distributions
581*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
582*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
583*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
584*1b191cb5SApple OSS Distributions return;
585*1b191cb5SApple OSS Distributions }
586*1b191cb5SApple OSS Distributions
587*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_for_signal, (void *)(uintptr_t)port_pair[0]);
588*1b191cb5SApple OSS Distributions
589*1b191cb5SApple OSS Distributions sleep(5);
590*1b191cb5SApple OSS Distributions
591*1b191cb5SApple OSS Distributions /* Associate thread and signal the eventlink */
592*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[1], self, 0, 0, 0, 0, MELA_OPTION_NONE);
593*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate for object 2");
594*1b191cb5SApple OSS Distributions
595*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal(port_pair[1], 0);
596*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_signal for object 2");
597*1b191cb5SApple OSS Distributions
598*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
599*1b191cb5SApple OSS Distributions
600*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
601*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
602*1b191cb5SApple OSS Distributions }
603*1b191cb5SApple OSS Distributions
604*1b191cb5SApple OSS Distributions /*
605*1b191cb5SApple OSS Distributions * Test 11: Test eventlink wait_signal.
606*1b191cb5SApple OSS Distributions *
607*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads and test wait_signal.
608*1b191cb5SApple OSS Distributions */
609*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_signal, "eventlink wait_signal", T_META_ASROOT(YES))
610*1b191cb5SApple OSS Distributions {
611*1b191cb5SApple OSS Distributions kern_return_t kr;
612*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
613*1b191cb5SApple OSS Distributions pthread_t pthread;
614*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
615*1b191cb5SApple OSS Distributions uint64_t count = 0;
616*1b191cb5SApple OSS Distributions
617*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
618*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
619*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
620*1b191cb5SApple OSS Distributions return;
621*1b191cb5SApple OSS Distributions }
622*1b191cb5SApple OSS Distributions
623*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_then_signal, (void *)(uintptr_t)port_pair[0]);
624*1b191cb5SApple OSS Distributions
625*1b191cb5SApple OSS Distributions sleep(5);
626*1b191cb5SApple OSS Distributions
627*1b191cb5SApple OSS Distributions /* Associate thread and wait_signal the eventlink */
628*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[1], self, 0, 0, 0, 0, MELA_OPTION_NONE);
629*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate for object 2");
630*1b191cb5SApple OSS Distributions
631*1b191cb5SApple OSS Distributions /* Wait on the eventlink with timeout */
632*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal_wait_until(port_pair[1], &count, 0, MELSW_OPTION_NONE,
633*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
634*1b191cb5SApple OSS Distributions
635*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_signal_wait_until");
636*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_signal_wait_until returned correct count value");
637*1b191cb5SApple OSS Distributions
638*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
639*1b191cb5SApple OSS Distributions
640*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
641*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
642*1b191cb5SApple OSS Distributions }
643*1b191cb5SApple OSS Distributions
644*1b191cb5SApple OSS Distributions /*
645*1b191cb5SApple OSS Distributions * Test 12: Test eventlink wait_signal with no wait.
646*1b191cb5SApple OSS Distributions *
647*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads and test wait_signal with no wait.
648*1b191cb5SApple OSS Distributions */
649*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_signal_no_wait, "eventlink wait_signal with no wait", T_META_ASROOT(YES))
650*1b191cb5SApple OSS Distributions {
651*1b191cb5SApple OSS Distributions kern_return_t kr;
652*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
653*1b191cb5SApple OSS Distributions pthread_t pthread;
654*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
655*1b191cb5SApple OSS Distributions uint64_t count = 0;
656*1b191cb5SApple OSS Distributions
657*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
658*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
659*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
660*1b191cb5SApple OSS Distributions return;
661*1b191cb5SApple OSS Distributions }
662*1b191cb5SApple OSS Distributions
663*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_then_wait_signal_with_no_wait, (void *)(uintptr_t)port_pair[0]);
664*1b191cb5SApple OSS Distributions
665*1b191cb5SApple OSS Distributions sleep(5);
666*1b191cb5SApple OSS Distributions
667*1b191cb5SApple OSS Distributions /* Associate thread and wait_signal the eventlink */
668*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[1], self, 0, 0, 0, 0, MELA_OPTION_NONE);
669*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate for object 2");
670*1b191cb5SApple OSS Distributions
671*1b191cb5SApple OSS Distributions /* Wait on the eventlink with timeout */
672*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal_wait_until(port_pair[1], &count, 0, MELSW_OPTION_NONE,
673*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
674*1b191cb5SApple OSS Distributions
675*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_signal_wait_until");
676*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_signal_wait_until returned correct count value");
677*1b191cb5SApple OSS Distributions
678*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
679*1b191cb5SApple OSS Distributions
680*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
681*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
682*1b191cb5SApple OSS Distributions }
683*1b191cb5SApple OSS Distributions
684*1b191cb5SApple OSS Distributions /*
685*1b191cb5SApple OSS Distributions * Test 13: Test eventlink wait_signal with prepost.
686*1b191cb5SApple OSS Distributions *
687*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads and test wait_signal with prepost.
688*1b191cb5SApple OSS Distributions */
689*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_signal_prepost, "eventlink wait_signal with prepost", T_META_ASROOT(YES))
690*1b191cb5SApple OSS Distributions {
691*1b191cb5SApple OSS Distributions kern_return_t kr;
692*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
693*1b191cb5SApple OSS Distributions pthread_t pthread;
694*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
695*1b191cb5SApple OSS Distributions uint64_t count = 0;
696*1b191cb5SApple OSS Distributions
697*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
698*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
699*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
700*1b191cb5SApple OSS Distributions return;
701*1b191cb5SApple OSS Distributions }
702*1b191cb5SApple OSS Distributions
703*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_then_wait_signal_with_prepost, (void *)(uintptr_t)port_pair[0]);
704*1b191cb5SApple OSS Distributions
705*1b191cb5SApple OSS Distributions sleep(5);
706*1b191cb5SApple OSS Distributions
707*1b191cb5SApple OSS Distributions /* Associate thread and wait_signal the eventlink */
708*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[1], self, 0, 0, 0, 0, MELA_OPTION_NONE);
709*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate for object 2");
710*1b191cb5SApple OSS Distributions
711*1b191cb5SApple OSS Distributions /* Wait on the eventlink with timeout */
712*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal_wait_until(port_pair[1], &count, 0, MELSW_OPTION_NONE,
713*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
714*1b191cb5SApple OSS Distributions
715*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_signal_wait_until");
716*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_signal_wait_until returned correct count value");
717*1b191cb5SApple OSS Distributions
718*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
719*1b191cb5SApple OSS Distributions
720*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
721*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
722*1b191cb5SApple OSS Distributions }
723*1b191cb5SApple OSS Distributions
724*1b191cb5SApple OSS Distributions /*
725*1b191cb5SApple OSS Distributions * Test 14: Test eventlink wait_signal with associate on wait option.
726*1b191cb5SApple OSS Distributions *
727*1b191cb5SApple OSS Distributions * Create an eventlink object, set associate on wait on one side and test wait_signal.
728*1b191cb5SApple OSS Distributions */
729*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_signal_associate_on_wait, "eventlink wait_signal associate on wait", T_META_ASROOT(YES))
730*1b191cb5SApple OSS Distributions {
731*1b191cb5SApple OSS Distributions kern_return_t kr;
732*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
733*1b191cb5SApple OSS Distributions pthread_t pthread;
734*1b191cb5SApple OSS Distributions uint64_t count = 0;
735*1b191cb5SApple OSS Distributions
736*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
737*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
738*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
739*1b191cb5SApple OSS Distributions return;
740*1b191cb5SApple OSS Distributions }
741*1b191cb5SApple OSS Distributions
742*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_then_signal, (void *)(uintptr_t)port_pair[0]);
743*1b191cb5SApple OSS Distributions
744*1b191cb5SApple OSS Distributions sleep(5);
745*1b191cb5SApple OSS Distributions
746*1b191cb5SApple OSS Distributions /* Set associate on wait and wait_signal the eventlink */
747*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[1], MACH_PORT_NULL, 0, 0, 0, 0, MELA_OPTION_ASSOCIATE_ON_WAIT);
748*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate with associate on wait for object 2");
749*1b191cb5SApple OSS Distributions
750*1b191cb5SApple OSS Distributions /* Wait on the eventlink with timeout */
751*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal_wait_until(port_pair[1], &count, 0, MELSW_OPTION_NONE,
752*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
753*1b191cb5SApple OSS Distributions
754*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_signal_wait_until");
755*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)1, "mach_eventlink_signal_wait_until returned correct count value");
756*1b191cb5SApple OSS Distributions
757*1b191cb5SApple OSS Distributions /* Remove associate on wait option */
758*1b191cb5SApple OSS Distributions kr = mach_eventlink_disassociate(port_pair[1], MELD_OPTION_NONE);
759*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_disassociate");
760*1b191cb5SApple OSS Distributions
761*1b191cb5SApple OSS Distributions /* Wait on the eventlink with timeout */
762*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal_wait_until(port_pair[1], &count, 0, MELSW_OPTION_NONE,
763*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
764*1b191cb5SApple OSS Distributions
765*1b191cb5SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT, "mach_eventlink_wait_until returned expected error");
766*1b191cb5SApple OSS Distributions
767*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
768*1b191cb5SApple OSS Distributions
769*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
770*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
771*1b191cb5SApple OSS Distributions }
772*1b191cb5SApple OSS Distributions
773*1b191cb5SApple OSS Distributions /*
774*1b191cb5SApple OSS Distributions * Test 15: Test eventlink wait_signal_loop.
775*1b191cb5SApple OSS Distributions *
776*1b191cb5SApple OSS Distributions * Create an eventlink object, associate threads and test wait_signal in a loop.
777*1b191cb5SApple OSS Distributions */
778*1b191cb5SApple OSS Distributions T_DECL(test_eventlink_wait_signal_loop, "eventlink wait_signal in loop", T_META_ASROOT(YES))
779*1b191cb5SApple OSS Distributions {
780*1b191cb5SApple OSS Distributions kern_return_t kr;
781*1b191cb5SApple OSS Distributions mach_port_t port_pair[2];
782*1b191cb5SApple OSS Distributions pthread_t pthread;
783*1b191cb5SApple OSS Distributions mach_port_t self = mach_thread_self();
784*1b191cb5SApple OSS Distributions uint64_t count = 0;
785*1b191cb5SApple OSS Distributions int i;
786*1b191cb5SApple OSS Distributions
787*1b191cb5SApple OSS Distributions /* Create an eventlink and associate threads to it */
788*1b191cb5SApple OSS Distributions kr = test_eventlink_create(port_pair);
789*1b191cb5SApple OSS Distributions if (kr != KERN_SUCCESS) {
790*1b191cb5SApple OSS Distributions return;
791*1b191cb5SApple OSS Distributions }
792*1b191cb5SApple OSS Distributions
793*1b191cb5SApple OSS Distributions pthread = thread_create_for_test(test_eventlink_wait_then_signal_loop, (void *)(uintptr_t)port_pair[0]);
794*1b191cb5SApple OSS Distributions
795*1b191cb5SApple OSS Distributions /* Associate thread and wait_signal the eventlink */
796*1b191cb5SApple OSS Distributions kr = mach_eventlink_associate(port_pair[1], self, 0, 0, 0, 0, MELA_OPTION_NONE);
797*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_eventlink_associate for object 2");
798*1b191cb5SApple OSS Distributions
799*1b191cb5SApple OSS Distributions for (i = 0; i < 100; i++) {
800*1b191cb5SApple OSS Distributions /* Wait on the eventlink with timeout */
801*1b191cb5SApple OSS Distributions kr = mach_eventlink_signal_wait_until(port_pair[1], &count, 0, MELSW_OPTION_NONE,
802*1b191cb5SApple OSS Distributions KERN_CLOCK_MACH_ABSOLUTE_TIME, 0);
803*1b191cb5SApple OSS Distributions
804*1b191cb5SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "main thread: mach_eventlink_signal_wait_until");
805*1b191cb5SApple OSS Distributions T_EXPECT_EQ(count, (uint64_t)(i + 1), "main thread: mach_eventlink_signal_wait_until returned correct count value");
806*1b191cb5SApple OSS Distributions }
807*1b191cb5SApple OSS Distributions
808*1b191cb5SApple OSS Distributions pthread_join(pthread, NULL);
809*1b191cb5SApple OSS Distributions
810*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[0]);
811*1b191cb5SApple OSS Distributions mach_port_deallocate(mach_task_self(), port_pair[1]);
812*1b191cb5SApple OSS Distributions }
813