]> git.saurik.com Git - apple/libpthread.git/blob - private/qos_private.h
469bf79ed7244dc3ac1425252f6f855b25354e8a
[apple/libpthread.git] / private / qos_private.h
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef _QOS_PRIVATE_H
25 #define _QOS_PRIVATE_H
26
27 #include <pthread/qos.h>
28 #include <sys/qos_private.h>
29
30 #if __DARWIN_C_LEVEL >= __DARWIN_C_FULL
31 // allow __DARWIN_C_LEVEL to turn off the use of mach_port_t
32 #include <mach/port.h>
33 #endif
34
35 // pthread_priority_t is an on opaque integer that is guaranteed to be ordered such that
36 // combations of QoS classes and relative priorities are ordered numerically, according to
37 // their combined priority.
38 typedef unsigned long pthread_priority_t;
39
40 // masks for splitting the handling the contents of a pthread_priority_t, the mapping from
41 // qos_class_t to the class bits, however, is intentionally not exposed.
42 #define _PTHREAD_PRIORITY_FLAGS_MASK 0xff000000
43 #define _PTHREAD_PRIORITY_FLAGS_SHIFT (24ull)
44 #define _PTHREAD_PRIORITY_ENCODING_MASK 0x00a00000
45 #define _PTHREAD_PRIORITY_ENCODING_SHIFT (22ull)
46 #define _PTHREAD_PRIORITY_ENCODING_V0 0x00000000
47 #define _PTHREAD_PRIORITY_ENCODING_V1 0x00400000 /* unused */
48 #define _PTHREAD_PRIORITY_ENCODING_V2 0x00800000 /* unused */
49 #define _PTHREAD_PRIORITY_ENCODING_V3 0x00a00000 /* unused */
50 #define _PTHREAD_PRIORITY_QOS_CLASS_MASK 0x003fff00
51 #define _PTHREAD_PRIORITY_QOS_CLASS_SHIFT (8ull)
52 #define _PTHREAD_PRIORITY_PRIORITY_MASK 0x000000ff
53 #define _PTHREAD_PRIORITY_PRIORITY_SHIFT (0)
54
55 #define _PTHREAD_PRIORITY_OVERCOMMIT_FLAG 0x80000000
56 #define _PTHREAD_PRIORITY_INHERIT_FLAG 0x40000000
57 #define _PTHREAD_PRIORITY_ROOTQUEUE_FLAG 0x20000000
58 // Used to indicate to the pthread kext that the provided event manager thread
59 // priority is actually a scheduling priority not a QoS. We can have ROOTQUEUE_FLAG
60 // perform double duty because it's never provided to the kernel.
61 #define _PTHREAD_PRIORITY_SCHED_PRI_FLAG 0x20000000
62 #define _PTHREAD_PRIORITY_ENFORCE_FLAG 0x10000000
63 #define _PTHREAD_PRIORITY_OVERRIDE_FLAG 0x08000000
64
65 // libdispatch defines the following, so it's not safe to use for anything we
66 // expect to be passed in from userspace
67 //#define _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG 0x04000000
68
69 // The event manager flag indicates that this thread/request is for a event
70 // manager thread. There can only ever be one event manager thread at a time and
71 // it is brought up at the highest of all event manager priorities passed to the
72 // kext.
73 #define _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG 0x02000000
74 #define _PTHREAD_PRIORITY_NEEDS_UNBIND_FLAG 0x01000000
75
76 // redeffed here to avoid leaving __QOS_ENUM defined in the public header
77 #define __QOS_ENUM(name, type, ...) enum { __VA_ARGS__ }; typedef type name##_t
78 #define __QOS_AVAILABLE_10_10
79 #define __QOS_AVAILABLE_10_11
80 #define __QOS_AVAILABLE_10_12
81
82 #if defined(__has_feature) && defined(__has_extension)
83 #if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums)
84 #undef __QOS_ENUM
85 #define __QOS_ENUM(name, type, ...) typedef enum : type { __VA_ARGS__ } name##_t
86 #endif
87 #if __has_feature(enumerator_attributes)
88 #undef __QOS_AVAILABLE_10_10
89 #define __QOS_AVAILABLE_10_10 __OSX_AVAILABLE(10.10) __IOS_AVAILABLE(8.0)
90 #undef __QOS_AVAILABLE_10_11
91 #define __QOS_AVAILABLE_10_11 __OSX_AVAILABLE(10.11) __IOS_AVAILABLE(9.0) __WATCHOS_AVAILABLE(2.0) __TVOS_AVAILABLE(9.0)
92 #undef __QOS_AVAILABLE_10_12
93 #define __QOS_AVAILABLE_10_12 __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0)
94 #endif
95 #endif
96
97 __QOS_ENUM(_pthread_set_flags, unsigned int,
98 _PTHREAD_SET_SELF_QOS_FLAG __QOS_AVAILABLE_10_10 = 0x1,
99 _PTHREAD_SET_SELF_VOUCHER_FLAG __QOS_AVAILABLE_10_10 = 0x2,
100 _PTHREAD_SET_SELF_FIXEDPRIORITY_FLAG __QOS_AVAILABLE_10_11 = 0x4,
101 _PTHREAD_SET_SELF_TIMESHARE_FLAG __QOS_AVAILABLE_10_11 = 0x8,
102 _PTHREAD_SET_SELF_WQ_KEVENT_UNBIND __QOS_AVAILABLE_10_12 = 0x10,
103 );
104
105 #undef __QOS_ENUM
106 #undef __QOS_AVAILABLE_10_10
107 #undef __QOS_AVAILABLE_10_11
108 #undef __QOS_AVAILABLE_10_12
109
110 #ifndef KERNEL
111
112 __BEGIN_DECLS
113
114 /*!
115 * @function pthread_set_qos_class_np
116 *
117 * @abstract
118 * Sets the requested QOS class and relative priority of the current thread.
119 *
120 * @discussion
121 * The QOS class and relative priority represent an overall combination of
122 * system quality of service attributes on a thread.
123 *
124 * Subsequent calls to interfaces such as pthread_setschedparam() that are
125 * incompatible or in conflict with the QOS class system will unset the QOS
126 * class requested with this interface and pthread_get_qos_class_np() will
127 * return QOS_CLASS_UNSPECIFIED thereafter. A thread so modified is permanently
128 * opted-out of the QOS class system and calls to this function to request a QOS
129 * class for such a thread will fail and return EPERM.
130 *
131 * @param __pthread
132 * The current thread as returned by pthread_self().
133 * EINVAL will be returned if any other thread is provided.
134 *
135 * @param __qos_class
136 * A QOS class value:
137 * - QOS_CLASS_USER_INTERACTIVE
138 * - QOS_CLASS_USER_INITIATED
139 * - QOS_CLASS_DEFAULT
140 * - QOS_CLASS_UTILITY
141 * - QOS_CLASS_BACKGROUND
142 * - QOS_CLASS_MAINTENANCE
143 * EINVAL will be returned if any other value is provided.
144 *
145 * @param __relative_priority
146 * A relative priority within the QOS class. This value is a negative offset
147 * from the maximum supported scheduler priority for the given class.
148 * EINVAL will be returned if the value is greater than zero or less than
149 * QOS_MIN_RELATIVE_PRIORITY.
150 *
151 * @return
152 * Zero if successful, othwerise an errno value.
153 */
154 __OSX_AVAILABLE_BUT_DEPRECATED_MSG(__MAC_10_10, __MAC_10_10, __IPHONE_8_0, __IPHONE_8_0, \
155 "Use pthread_set_qos_class_self_np() instead")
156 int
157 pthread_set_qos_class_np(pthread_t __pthread,
158 qos_class_t __qos_class,
159 int __relative_priority);
160
161 /* Private interfaces for libdispatch to encode/decode specific values of pthread_priority_t. */
162
163 // Encode a class+priority pair into a pthread_priority_t,
164 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
165 pthread_priority_t
166 _pthread_qos_class_encode(qos_class_t qos_class, int relative_priority, unsigned long flags);
167
168 // Decode a pthread_priority_t into a class+priority pair.
169 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
170 qos_class_t
171 _pthread_qos_class_decode(pthread_priority_t priority, int *relative_priority, unsigned long *flags);
172
173 // Encode a legacy workqueue API priority into a pthread_priority_t
174 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
175 pthread_priority_t
176 _pthread_qos_class_encode_workqueue(int queue_priority, unsigned long flags);
177
178 #if __DARWIN_C_LEVEL >= __DARWIN_C_FULL
179 // Set QoS or voucher, or both, on pthread_self()
180 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
181 int
182 _pthread_set_properties_self(_pthread_set_flags_t flags, pthread_priority_t priority, mach_port_t voucher);
183
184 // Set self to fixed priority without disturbing QoS or priority
185 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
186 int
187 pthread_set_fixedpriority_self(void);
188
189 // Inverse of pthread_set_fixedpriority_self()
190 __OSX_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0)
191 int
192 pthread_set_timeshare_self(void);
193
194 #endif
195
196 __END_DECLS
197
198 #endif // KERNEL
199
200 #endif //_QOS_PRIVATE_H