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