2 * turnstile_multihop: Tests turnstile and multi hop priority propagation.
9 #include <darwintest.h>
10 #include <darwintest_multiprocess.h>
12 #include <dispatch/dispatch.h>
15 #include <mach/mach.h>
16 #include <mach/message.h>
17 #include <mach/mach_voucher.h>
18 #include <pthread/workqueue_private.h>
19 #include <voucher/ipc_pthread_priority_types.h>
20 #include <servers/bootstrap.h>
22 #include <sys/event.h>
24 #include <crt_externs.h>
26 #include <sys/types.h>
27 #include <sys/sysctl.h>
28 #include <libkern/OSAtomic.h>
31 #include "turnstile_multihop_helper.h"
33 T_GLOBAL_META(T_META_NAMESPACE("xnu.turnstile_multihop"));
35 #define HELPER_TIMEOUT_SECS (3000)
38 mach_msg_header_t header
;
40 mach_msg_port_descriptor_t port_descriptor
;
43 static boolean_t spin_for_ever
= false;
46 thread_create_at_qos(qos_class_t qos
, void * (*function
)(void *));
48 nanoseconds_to_absolutetime(uint64_t nanoseconds
);
50 sched_create_load_at_qos(qos_class_t qos
, void **load_token
);
52 sched_terminate_load(void *load_token
) __unused
;
53 static void do_work(int num
);
55 dispatch_sync_cancel(mach_port_t owner_thread
, qos_class_t promote_qos
);
57 static void *sched_load_thread(void *);
59 struct load_token_context
{
60 volatile int threads_should_exit
;
66 static struct mach_timebase_info sched_mti
;
67 static pthread_once_t sched_mti_once_control
= PTHREAD_ONCE_INIT
;
72 mach_timebase_info(&sched_mti
);
75 nanoseconds_to_absolutetime(uint64_t nanoseconds
)
77 pthread_once(&sched_mti_once_control
, sched_mti_init
);
79 return (uint64_t)(nanoseconds
* (((double)sched_mti
.denom
) / ((double)sched_mti
.numer
)));
83 sched_create_load_at_qos(qos_class_t qos
, void **load_token
)
85 struct load_token_context
*context
= NULL
;
88 size_t ncpu_size
= sizeof(ncpu
);
93 ret
= sysctlbyname("hw.ncpu", &ncpu
, &ncpu_size
, NULL
, 0);
95 T_LOG("sysctlbyname(hw.ncpu)");
99 T_QUIET
; T_LOG("%s: Detected %d CPUs\n", __FUNCTION__
, ncpu
);
102 T_QUIET
; T_LOG("%s: Will create %d threads\n", __FUNCTION__
, nthreads
);
104 ret
= pthread_attr_init(&attr
);
105 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_attr_init");
107 if (&pthread_attr_set_qos_class_np
) {
108 ret
= pthread_attr_set_qos_class_np(&attr
, qos
, 0);
109 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_attr_set_qos_class_np");
112 context
= calloc(1, sizeof(*context
));
113 if (context
== NULL
) {
114 T_QUIET
; T_LOG("calloc returned error"); return ENOMEM
;
117 context
->threads_should_exit
= 0;
118 context
->thread_count
= nthreads
;
120 context
->threads
= calloc((unsigned int)nthreads
, sizeof(pthread_t
));
124 for (i
= 0; i
< nthreads
; i
++) {
125 ret
= pthread_create(&context
->threads
[i
], &attr
, sched_load_thread
, context
);
126 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_create");
127 T_QUIET
; T_LOG("%s: Created thread %d (%p)\n", __FUNCTION__
, i
, (void *)context
->threads
[i
]);
130 ret
= pthread_attr_destroy(&attr
);
131 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_attr_destroy");
133 *load_token
= context
;
139 sched_load_thread(void *arg
)
141 struct load_token_context
*context
= (struct load_token_context
*)arg
;
143 T_QUIET
; T_LOG("%s: Thread started %p\n", __FUNCTION__
, (void *)pthread_self());
145 while (!context
->threads_should_exit
) {
146 uint64_t start
= mach_absolute_time();
147 uint64_t end
= start
+ nanoseconds_to_absolutetime(900ULL * NSEC_PER_MSEC
);
149 while ((mach_absolute_time() < end
) && !context
->threads_should_exit
) {
154 T_QUIET
; T_LOG("%s: Thread terminating %p\n", __FUNCTION__
, (void *)pthread_self());
160 sched_terminate_load(void *load_token
)
164 struct load_token_context
*context
= (struct load_token_context
*)load_token
;
166 context
->threads_should_exit
= 1;
169 for (i
= 0; i
< context
->thread_count
; i
++) {
170 T_QUIET
; T_LOG("%s: Joining thread %d (%p)\n", __FUNCTION__
, i
, (void *)context
->threads
[i
]);
171 ret
= pthread_join(context
->threads
[i
], NULL
);
172 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_join");
175 free(context
->threads
);
182 // Find the first num primes, simply as a means of doing work
186 volatile int i
= 3, count
, c
;
188 for (count
= 2; count
<= num
;) {
189 for (c
= 2; c
<= i
; c
++) {
201 #pragma mark pthread callbacks
204 worker_cb(pthread_priority_t __unused priority
)
206 T_FAIL("a worker thread was created");
210 event_cb(void ** __unused events
, int * __unused nevents
)
212 T_FAIL("a kevent routine was called instead of workloop");
216 get_user_promotion_basepri(void)
218 mach_msg_type_number_t count
= THREAD_POLICY_STATE_COUNT
;
219 struct thread_policy_state thread_policy
;
220 boolean_t get_default
= FALSE
;
221 mach_port_t thread_port
= pthread_mach_thread_np(pthread_self());
223 kern_return_t kr
= thread_policy_get(thread_port
, THREAD_POLICY_STATE
,
224 (thread_policy_t
)&thread_policy
, &count
, &get_default
);
225 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_policy_get");
226 return thread_policy
.thps_user_promotion_basepri
;
229 #define LISTENER_WLID 0x100
230 #define CONN_WLID 0x200
233 register_port_options(void)
235 return MACH_RCV_MSG
| MACH_RCV_LARGE
| MACH_RCV_LARGE_IDENTITY
|
236 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX
) |
237 MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0
) |
242 register_port(uint64_t wlid
, mach_port_t port
)
246 struct kevent_qos_s kev
= {
248 .filter
= EVFILT_MACHPORT
,
249 .flags
= EV_ADD
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
,
250 .fflags
= register_port_options(),
252 .qos
= (int32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE
, 0, 0)
255 struct kevent_qos_s kev_err
= { 0 };
257 /* Setup workloop for mach msg rcv */
258 r
= kevent_id(wlid
, &kev
, 1, &kev_err
, 1, NULL
,
259 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
261 T_QUIET
; T_ASSERT_POSIX_SUCCESS(r
, "kevent_id");
262 T_QUIET
; T_ASSERT_EQ(r
, 0, "no errors returned from kevent_id");
266 * Basic WL handler callback, it checks the
267 * effective Qos of the servicer thread.
270 workloop_cb_test_intransit(uint64_t *workloop_id
, void **eventslist
, int *events
)
272 static bool got_peer
;
274 struct kevent_qos_s
*kev
= eventslist
[0];
275 mach_msg_header_t
*hdr
;
276 struct test_msg
*tmsg
;
278 T_LOG("Workloop handler %s called. Received message on 0x%llx",
279 __func__
, *workloop_id
);
281 /* Skip the test if we can't check Qos */
282 if (geteuid() != 0) {
283 T_SKIP("kevent_qos test requires root privileges to run.");
286 T_QUIET
; T_ASSERT_EQ(*events
, 1, "should have one event");
288 hdr
= (mach_msg_header_t
*)kev
->ext
[0];
289 T_ASSERT_NOTNULL(hdr
, "has a message");
290 T_ASSERT_EQ(hdr
->msgh_size
, (uint32_t)sizeof(struct test_msg
), "of the right size");
291 tmsg
= (struct test_msg
*)hdr
;
293 switch (*workloop_id
) {
295 T_LOG("Registering peer connection");
296 T_QUIET
; T_ASSERT_FALSE(got_peer
, "Should not have seen peer yet");
301 T_LOG("Received message on peer");
309 T_LOG("Do some CPU work.");
312 /* Check if the override now is IN + 60 boost */
313 T_EXPECT_EFFECTIVE_QOS_EQ(QOS_CLASS_USER_INITIATED
,
314 "dispatch_source event handler QoS should be QOS_CLASS_USER_INITIATED");
315 T_EXPECT_EQ(get_user_promotion_basepri(), 60u,
316 "dispatch_source event handler should be overridden at 60");
318 if (*workloop_id
== LISTENER_WLID
) {
319 register_port(CONN_WLID
, tmsg
->port_descriptor
.name
);
321 kev
->flags
= EV_ADD
| EV_ENABLE
| EV_UDATA_SPECIFIC
| EV_DISPATCH
| EV_VANISHED
;
322 kev
->fflags
= register_port_options();
323 kev
->ext
[0] = kev
->ext
[1] = kev
->ext
[2] = kev
->ext
[3] = 0;
326 /* this will unblock the waiter */
327 mach_msg_destroy(hdr
);
333 run_client_server(const char *server_name
, const char *client_name
)
335 dt_helper_t helpers
[] = {
336 dt_launchd_helper_domain("com.apple.xnu.test.turnstile_multihop.plist",
337 server_name
, NULL
, LAUNCH_SYSTEM_DOMAIN
),
338 dt_fork_helper(client_name
)
340 dt_run_helpers(helpers
, 2, HELPER_TIMEOUT_SECS
);
343 #pragma mark Mach receive
345 #define TURNSTILE_MULTIHOP_SERVICE_NAME "com.apple.xnu.test.turnstile_multihop"
348 get_server_port(void)
351 kern_return_t kr
= bootstrap_check_in(bootstrap_port
,
352 TURNSTILE_MULTIHOP_SERVICE_NAME
, &port
);
353 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "server bootstrap_check_in");
357 static mach_voucher_t
358 create_pthpriority_voucher(mach_msg_priority_t qos
)
360 char voucher_buf
[sizeof(mach_voucher_attr_recipe_data_t
) + sizeof(ipc_pthread_priority_value_t
)];
362 mach_voucher_t voucher
= MACH_PORT_NULL
;
364 ipc_pthread_priority_value_t ipc_pthread_priority_value
=
365 (ipc_pthread_priority_value_t
)qos
;
367 mach_voucher_attr_raw_recipe_array_t recipes
;
368 mach_voucher_attr_raw_recipe_size_t recipe_size
= 0;
369 mach_voucher_attr_recipe_t recipe
=
370 (mach_voucher_attr_recipe_t
)&voucher_buf
[recipe_size
];
372 recipe
->key
= MACH_VOUCHER_ATTR_KEY_PTHPRIORITY
;
373 recipe
->command
= MACH_VOUCHER_ATTR_PTHPRIORITY_CREATE
;
374 recipe
->previous_voucher
= MACH_VOUCHER_NULL
;
375 memcpy((char *)&recipe
->content
[0], &ipc_pthread_priority_value
, sizeof(ipc_pthread_priority_value
));
376 recipe
->content_size
= sizeof(ipc_pthread_priority_value_t
);
377 recipe_size
+= sizeof(mach_voucher_attr_recipe_data_t
) + recipe
->content_size
;
379 recipes
= (mach_voucher_attr_raw_recipe_array_t
)&voucher_buf
[0];
381 ret
= host_create_mach_voucher(mach_host_self(),
386 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "client host_create_mach_voucher");
392 mach_port_t send_port
,
393 mach_port_t reply_port
,
394 mach_port_t msg_port
,
395 mach_msg_priority_t qos
,
396 mach_msg_option_t options
)
398 kern_return_t ret
= 0;
400 struct test_msg send_msg
= {
402 .msgh_remote_port
= send_port
,
403 .msgh_local_port
= reply_port
,
404 .msgh_bits
= MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND
,
405 reply_port
? MACH_MSG_TYPE_MAKE_SEND_ONCE
: 0,
406 MACH_MSG_TYPE_MOVE_SEND
,
407 MACH_MSGH_BITS_COMPLEX
),
409 .msgh_size
= sizeof(send_msg
),
412 .msgh_descriptor_count
= 1,
416 .disposition
= MACH_MSG_TYPE_MOVE_RECEIVE
,
417 .type
= MACH_MSG_PORT_DESCRIPTOR
,
421 if (options
& MACH_SEND_SYNC_USE_THRPRI
) {
422 send_msg
.header
.msgh_voucher_port
= create_pthpriority_voucher(qos
);
425 if (msg_port
== MACH_PORT_NULL
) {
426 send_msg
.body
.msgh_descriptor_count
= 0;
429 ret
= mach_msg(&(send_msg
.header
),
433 ((reply_port
? MACH_SEND_SYNC_OVERRIDE
: 0) | options
),
434 send_msg
.header
.msgh_size
,
440 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "client mach_msg");
445 mach_port_t rcv_port
,
446 mach_port_t notify_port
)
448 kern_return_t ret
= 0;
451 mach_msg_header_t header
;
452 mach_msg_body_t body
;
453 mach_msg_port_descriptor_t port_descriptor
;
457 .msgh_remote_port
= MACH_PORT_NULL
,
458 .msgh_local_port
= rcv_port
,
459 .msgh_size
= sizeof(rcv_msg
),
463 T_LOG("Client: Starting sync receive\n");
465 ret
= mach_msg(&(rcv_msg
.header
),
469 rcv_msg
.header
.msgh_size
,
475 static lock_t lock_DEF
;
476 static lock_t lock_IN
;
477 static lock_t lock_UI
;
479 static mach_port_t main_thread_port
;
480 static mach_port_t def_thread_port
;
481 static mach_port_t in_thread_port
;
482 static mach_port_t ui_thread_port
;
483 static mach_port_t sixty_thread_port
;
485 static uint64_t dispatch_sync_owner
;
488 get_pri(thread_t thread_port
)
492 thread_extended_info_data_t extended_info
;
493 mach_msg_type_number_t count
= THREAD_EXTENDED_INFO_COUNT
;
494 kr
= thread_info(thread_port
, THREAD_EXTENDED_INFO
,
495 (thread_info_t
)&extended_info
, &count
);
497 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
499 return extended_info
.pth_curpri
;
503 set_thread_name(const char *fn_name
)
507 thread_t thread_port
= pthread_mach_thread_np(pthread_self());
509 int pri
= get_pri(thread_port
);
511 snprintf(name
, sizeof(name
), "%s at pri %2d", fn_name
, pri
);
512 pthread_setname_np(name
);
516 thread_wait_to_block(mach_port_t thread_port
)
518 thread_extended_info_data_t extended_info
;
522 mach_msg_type_number_t count
= THREAD_EXTENDED_INFO_COUNT
;
523 kr
= thread_info(thread_port
, THREAD_EXTENDED_INFO
,
524 (thread_info_t
)&extended_info
, &count
);
526 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
528 if (extended_info
.pth_run_state
== TH_STATE_WAITING
) {
529 T_LOG("Target thread blocked\n");
532 thread_switch(thread_port
, SWITCH_OPTION_DEPRESS
, 0);
537 thread_wait_to_boost(mach_port_t thread_port
, mach_port_t yield_thread
, int priority
)
539 thread_extended_info_data_t extended_info
;
543 mach_msg_type_number_t count
= THREAD_EXTENDED_INFO_COUNT
;
544 kr
= thread_info(thread_port
, THREAD_EXTENDED_INFO
,
545 (thread_info_t
)&extended_info
, &count
);
547 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "thread_info");
549 if (extended_info
.pth_priority
>= priority
) {
550 T_LOG("Target thread boosted\n");
553 thread_switch(yield_thread
, SWITCH_OPTION_DEPRESS
, 0);
558 dispatch_sync_wait(mach_port_t owner_thread
, qos_class_t promote_qos
)
560 struct kevent_qos_s kev_err
[] = {{ 0 }};
566 action
= EV_ADD
| EV_DISABLE
;
567 fflags
= NOTE_WL_SYNC_WAIT
| NOTE_WL_DISCOVER_OWNER
;
569 dispatch_sync_owner
= owner_thread
;
571 struct kevent_qos_s kev
[] = {{
572 .ident
= mach_thread_self(),
573 .filter
= EVFILT_WORKLOOP
,
576 .udata
= (uintptr_t) &def_thread_port
,
577 .qos
= (int32_t)_pthread_qos_class_encode(promote_qos
, 0, 0),
578 .ext
[EV_EXTIDX_WL_MASK
] = mask
,
579 .ext
[EV_EXTIDX_WL_VALUE
] = dispatch_sync_owner
,
580 .ext
[EV_EXTIDX_WL_ADDR
] = (uint64_t)&dispatch_sync_owner
,
583 /* Setup workloop to fake dispatch sync wait on a workloop */
584 r
= kevent_id(30, kev
, 1, kev_err
, 1, NULL
,
585 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
586 T_QUIET
; T_LOG("dispatch_sync_wait returned\n");
590 dispatch_sync_cancel(mach_port_t owner_thread
, qos_class_t promote_qos
)
592 struct kevent_qos_s kev_err
[] = {{ 0 }};
598 action
= EV_DELETE
| EV_ENABLE
;
599 fflags
= NOTE_WL_SYNC_WAKE
| NOTE_WL_END_OWNERSHIP
;
601 dispatch_sync_owner
= owner_thread
;
603 struct kevent_qos_s kev
[] = {{
604 .ident
= def_thread_port
,
605 .filter
= EVFILT_WORKLOOP
,
608 .udata
= (uintptr_t) &def_thread_port
,
609 .qos
= (int32_t)_pthread_qos_class_encode(promote_qos
, 0, 0),
610 .ext
[EV_EXTIDX_WL_MASK
] = mask
,
611 .ext
[EV_EXTIDX_WL_VALUE
] = dispatch_sync_owner
,
612 .ext
[EV_EXTIDX_WL_ADDR
] = (uint64_t)&dispatch_sync_owner
,
615 /* Setup workloop to fake dispatch sync wake on a workloop */
616 r
= kevent_id(30, kev
, 1, kev_err
, 1, NULL
,
617 NULL
, KEVENT_FLAG_WORKLOOP
| KEVENT_FLAG_ERROR_EVENTS
);
618 T_QUIET
; T_LOG("dispatch_sync_cancel returned\n");
622 thread_at_sixty(void *arg __unused
)
625 struct sched_param param
;
628 uint64_t before_lock_time
, after_lock_time
;
630 sixty_thread_port
= mach_thread_self();
632 set_thread_name(__FUNCTION__
);
634 /* Change our priority to 60 */
635 ret
= pthread_getschedparam(pthread_self(), &policy
, ¶m
);
636 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_getschedparam");
638 param
.sched_priority
= 60;
640 ret
= pthread_setschedparam(pthread_self(), policy
, ¶m
);
641 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_setschedparam");
643 ret
= pthread_getschedparam(pthread_self(), &policy
, ¶m
);
644 T_QUIET
; T_ASSERT_MACH_SUCCESS(ret
, "pthread_getschedparam");
646 T_LOG("My priority is %d", param
.sched_priority
);
648 thread_wait_to_boost(in_thread_port
, ui_thread_port
, 46);
651 /* Schedule load at Default */
652 sched_create_load_at_qos(QOS_CLASS_DEFAULT
, &load_token
);
655 T_LOG("Thread at priority 60 trying to acquire UI lock");
657 before_lock_time
= mach_absolute_time();
658 ull_lock(&lock_UI
, 3, UL_UNFAIR_LOCK
, 0);
659 after_lock_time
= mach_absolute_time();
661 T_QUIET
; T_LOG("The time for priority 60 thread to acquire lock was %llu \n",
662 (after_lock_time
- before_lock_time
));
667 thread_at_ui(void *arg __unused
)
669 ui_thread_port
= mach_thread_self();
671 set_thread_name(__FUNCTION__
);
673 /* Grab the first ulock */
674 ull_lock(&lock_UI
, 2, UL_UNFAIR_LOCK
, 0);
676 thread_wait_to_boost(def_thread_port
, in_thread_port
, 37);
677 thread_create_at_qos(QOS_CLASS_USER_INTERACTIVE
, thread_at_sixty
);
679 T_LOG("Thread at UI priority trying to acquire IN lock");
680 ull_lock(&lock_IN
, 2, UL_UNFAIR_LOCK
, 0);
681 ull_unlock(&lock_UI
, 2, UL_UNFAIR_LOCK
, 0);
686 thread_at_in(void *arg __unused
)
688 in_thread_port
= mach_thread_self();
690 set_thread_name(__FUNCTION__
);
692 /* Grab the first ulock */
693 ull_lock(&lock_IN
, 2, UL_UNFAIR_LOCK
, 0);
695 T_LOG("Thread at IN priority got first lock ");
697 thread_wait_to_boost(main_thread_port
, def_thread_port
, 31);
699 /* Create a new thread at QOS_CLASS_USER_INTERACTIVE qos */
700 thread_create_at_qos(QOS_CLASS_USER_INTERACTIVE
, thread_at_ui
);
702 T_LOG("Thread at IN priority trying to acquire default lock");
703 ull_lock(&lock_DEF
, 1, UL_UNFAIR_LOCK
, 0);
704 ull_unlock(&lock_IN
, 2, UL_UNFAIR_LOCK
, 0);
709 thread_at_default(void *arg __unused
)
711 def_thread_port
= mach_thread_self();
713 set_thread_name(__FUNCTION__
);
715 /* Grab the first ulock */
716 ull_lock(&lock_DEF
, 1, UL_UNFAIR_LOCK
, 0);
718 T_LOG("Thread at DEFAULT priority got first lock ");
720 thread_wait_to_block(main_thread_port
);
722 /* Create a new thread at QOS_CLASS_USER_INITIATED qos */
723 thread_create_at_qos(QOS_CLASS_USER_INITIATED
, thread_at_in
);
725 T_LOG("Thread at Default priority trying to wait on dispatch sync for maintenance thread");
726 dispatch_sync_wait(main_thread_port
, QOS_CLASS_DEFAULT
);
727 ull_unlock(&lock_DEF
, 1, UL_UNFAIR_LOCK
, 0);
732 thread_at_maintenance(void *arg __unused
)
734 mach_port_t service_port
;
735 mach_port_t conn_port
;
736 mach_port_t special_reply_port
;
737 mach_port_options_t opts
= {
738 .flags
= MPO_INSERT_SEND_RIGHT
,
741 main_thread_port
= mach_thread_self();
743 set_thread_name(__FUNCTION__
);
745 kern_return_t kr
= bootstrap_look_up(bootstrap_port
,
746 TURNSTILE_MULTIHOP_SERVICE_NAME
, &service_port
);
747 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "client bootstrap_look_up");
749 kr
= mach_port_construct(mach_task_self(), &opts
, 0ull, &conn_port
);
750 T_QUIET
; T_ASSERT_MACH_SUCCESS(kr
, "mach_port_construct");
752 special_reply_port
= thread_get_special_reply_port();
753 T_QUIET
; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port
), "get_thread_special_reply_port");
755 /* Become the dispatch sync owner, dispatch_sync_owner will be set in dispatch_sync_wait function */
757 /* Send a sync message */
758 send(conn_port
, special_reply_port
, MACH_PORT_NULL
,
759 (uint32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE
, 0, 0), 0);
761 /* Send an async checkin message */
762 send(service_port
, MACH_PORT_NULL
, conn_port
,
763 (uint32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE
, 0, 0), 0);
765 /* Create a new thread at QOS_CLASS_DEFAULT qos */
766 thread_create_at_qos(QOS_CLASS_DEFAULT
, thread_at_default
);
768 /* Block on Sync IPC */
769 receive(special_reply_port
, service_port
);
771 T_LOG("received reply");
773 dispatch_sync_cancel(def_thread_port
, QOS_CLASS_DEFAULT
);
777 T_HELPER_DECL(three_ulock_sync_ipc_hop
,
778 "Create chain of 4 threads with 3 ulocks and 1 sync IPC at different qos")
780 thread_create_at_qos(QOS_CLASS_MAINTENANCE
, thread_at_maintenance
);
785 thread_create_at_qos(qos_class_t qos
, void * (*function
)(void *))
787 qos_class_t qos_thread
;
792 ret
= setpriority(PRIO_DARWIN_ROLE
, 0, PRIO_DARWIN_ROLE_UI_FOCAL
);
794 T_LOG("set priority failed\n");
797 pthread_attr_init(&attr
);
798 pthread_attr_set_qos_class_np(&attr
, qos
, 0);
799 pthread_create(&thread
, &attr
, function
, NULL
);
801 T_LOG("pthread created\n");
802 pthread_get_qos_class_np(thread
, &qos_thread
, NULL
);
805 #pragma mark Mach receive - kevent_qos
807 T_HELPER_DECL(server_kevent_id
,
808 "Reply with the QoS that a dispatch source event handler ran with")
810 T_QUIET
; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
812 (pthread_workqueue_function_workloop_t
)workloop_cb_test_intransit
, 0, 0), NULL
);
814 register_port(LISTENER_WLID
, get_server_port());
816 T_ASSERT_FAIL("should receive a message");
819 #define TEST_MULTIHOP(server_name, client_name, name) \
820 T_DECL(server_kevent_id_##name, \
821 "Event delivery using a kevent_id", \
822 T_META_ASROOT(YES)) \
824 run_client_server(server_name, client_name); \
827 #define TEST_MULTIHOP_SPIN(server_name, client_name, name) \
828 T_DECL(server_kevent_id_##name, \
829 "Event delivery using a kevent_id", \
830 T_META_ASROOT(YES), T_META_ENABLED(FALSE)) \
832 spin_for_ever = true; \
833 run_client_server(server_name, client_name); \
834 spin_for_ever = false; \
838 * Test 1: Test multihop priority boosting with ulocks, dispatch sync and sync IPC.
840 * Create thread's at different Qos and acquire a ulock and block on next ulock/dispatch sync
841 * creating a sync chain. The last hop the chain is blocked on Sync IPC.
843 TEST_MULTIHOP("server_kevent_id", "three_ulock_sync_ipc_hop", three_ulock_sync_ipc_hop
)
846 * Test 2: Test multihop priority boosting with ulocks, dispatch sync and sync IPC.
848 * Create thread's at different Qos and acquire a ulock and block on next ulock/dispatch sync
849 * creating a sync chain. The last hop the chain is blocked on Sync IPC.
850 * Before the last priority 60 thread blocks on ulock, it also starts spinforeverd at priority 31.
852 TEST_MULTIHOP_SPIN("server_kevent_id", "three_ulock_sync_ipc_hop", three_ulock_sync_ipc_hop_spin
)