xref: /xnu-12377.1.9/tests/thread_policy.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1*f6217f89SApple OSS Distributions #include <darwintest.h>
2*f6217f89SApple OSS Distributions #include <mach/thread_policy_private.h>
3*f6217f89SApple OSS Distributions #include <mach/mach.h>
4*f6217f89SApple OSS Distributions #include <dispatch/dispatch.h>
5*f6217f89SApple OSS Distributions #include <sys/qos_private.h>
6*f6217f89SApple OSS Distributions 
7*f6217f89SApple OSS Distributions static qos_class_t
thread_qos_to_qos_class(uint32_t thread_qos)8*f6217f89SApple OSS Distributions thread_qos_to_qos_class(uint32_t thread_qos)
9*f6217f89SApple OSS Distributions {
10*f6217f89SApple OSS Distributions 	switch (thread_qos) {
11*f6217f89SApple OSS Distributions 	case THREAD_QOS_MAINTENANCE:
12*f6217f89SApple OSS Distributions 		return QOS_CLASS_MAINTENANCE;
13*f6217f89SApple OSS Distributions 	case THREAD_QOS_BACKGROUND:
14*f6217f89SApple OSS Distributions 		return QOS_CLASS_BACKGROUND;
15*f6217f89SApple OSS Distributions 	case THREAD_QOS_UTILITY:
16*f6217f89SApple OSS Distributions 		return QOS_CLASS_UTILITY;
17*f6217f89SApple OSS Distributions 	case THREAD_QOS_LEGACY:
18*f6217f89SApple OSS Distributions 		return QOS_CLASS_DEFAULT;
19*f6217f89SApple OSS Distributions 	case THREAD_QOS_USER_INITIATED:
20*f6217f89SApple OSS Distributions 		return QOS_CLASS_USER_INITIATED;
21*f6217f89SApple OSS Distributions 	case THREAD_QOS_USER_INTERACTIVE:
22*f6217f89SApple OSS Distributions 		return QOS_CLASS_USER_INTERACTIVE;
23*f6217f89SApple OSS Distributions 	default:
24*f6217f89SApple OSS Distributions 		return QOS_CLASS_UNSPECIFIED;
25*f6217f89SApple OSS Distributions 	}
26*f6217f89SApple OSS Distributions }
27*f6217f89SApple OSS Distributions 
28*f6217f89SApple OSS Distributions static qos_class_t
get_thread_requested_qos(void)29*f6217f89SApple OSS Distributions get_thread_requested_qos(void)
30*f6217f89SApple OSS Distributions {
31*f6217f89SApple OSS Distributions 	mach_msg_type_number_t count = THREAD_REQUESTED_STATE_POLICY_COUNT;
32*f6217f89SApple OSS Distributions 	struct thread_requested_qos_policy requested_policy;
33*f6217f89SApple OSS Distributions 	boolean_t get_default = FALSE;
34*f6217f89SApple OSS Distributions 	mach_port_t thread_port = pthread_mach_thread_np(pthread_self());
35*f6217f89SApple OSS Distributions 
36*f6217f89SApple OSS Distributions 	kern_return_t kr = thread_policy_get(thread_port, THREAD_REQUESTED_STATE_POLICY,
37*f6217f89SApple OSS Distributions 	    (thread_policy_t)&requested_policy, &count, &get_default);
38*f6217f89SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_policy_get");
39*f6217f89SApple OSS Distributions 
40*f6217f89SApple OSS Distributions 	return thread_qos_to_qos_class(requested_policy.thrq_base_qos);
41*f6217f89SApple OSS Distributions }
42*f6217f89SApple OSS Distributions 
43*f6217f89SApple OSS Distributions T_DECL(thread_policy_requested_state, "THREAD_REQUESTED_STATE_POLICY", T_META_ASROOT(NO))
44*f6217f89SApple OSS Distributions {
45*f6217f89SApple OSS Distributions 	qos_class_t main_thread_qos = get_thread_requested_qos();
46*f6217f89SApple OSS Distributions 	T_ASSERT_EQ(main_thread_qos, qos_class_main(), "main thead requested qos matches");
47*f6217f89SApple OSS Distributions }
48