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