]> git.saurik.com Git - apple/libpthread.git/blob - private/qos_private.h
libpthread-301.30.1.tar.gz
[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.h> /* qos_class_t */
29 #include <sys/qos_private.h>
30
31 #if __DARWIN_C_LEVEL >= __DARWIN_C_FULL
32 // allow __DARWIN_C_LEVEL to turn off the use of mach_port_t
33 #include <mach/port.h>
34 #endif
35
36 // pthread_priority_t is an on opaque integer that is guaranteed to be ordered such that
37 // combations of QoS classes and relative priorities are ordered numerically, according to
38 // their combined priority.
39 typedef unsigned long pthread_priority_t;
40
41 // masks for splitting the handling the contents of a pthread_priority_t, the mapping from
42 // qos_class_t to the class bits, however, is intentionally not exposed.
43 #define _PTHREAD_PRIORITY_FLAGS_MASK 0xff000000
44 #define _PTHREAD_PRIORITY_FLAGS_SHIFT (24ull)
45 #define _PTHREAD_PRIORITY_ENCODING_MASK 0x00a00000
46 #define _PTHREAD_PRIORITY_ENCODING_SHIFT (22ull)
47 #define _PTHREAD_PRIORITY_ENCODING_V0 0x00000000
48 #define _PTHREAD_PRIORITY_ENCODING_V1 0x00400000 /* unused */
49 #define _PTHREAD_PRIORITY_ENCODING_V2 0x00800000 /* unused */
50 #define _PTHREAD_PRIORITY_ENCODING_V3 0x00a00000 /* unused */
51 #define _PTHREAD_PRIORITY_QOS_CLASS_MASK 0x003fff00
52 #define _PTHREAD_PRIORITY_QOS_CLASS_SHIFT (8ull)
53 #define _PTHREAD_PRIORITY_PRIORITY_MASK 0x000000ff
54 #define _PTHREAD_PRIORITY_PRIORITY_SHIFT (0)
55
56 #define _PTHREAD_PRIORITY_OVERCOMMIT_FLAG 0x80000000
57 #define _PTHREAD_PRIORITY_INHERIT_FLAG 0x40000000
58 #define _PTHREAD_PRIORITY_ROOTQUEUE_FLAG 0x20000000
59 // Used to indicate to the pthread kext that the provided event manager thread
60 // priority is actually a scheduling priority not a QoS. We can have ROOTQUEUE_FLAG
61 // perform double duty because it's never provided to the kernel.
62 #define _PTHREAD_PRIORITY_SCHED_PRI_FLAG 0x20000000
63 #define _PTHREAD_PRIORITY_SCHED_PRI_MASK 0x0000ffff
64 #define _PTHREAD_PRIORITY_ENFORCE_FLAG 0x10000000
65 #define _PTHREAD_PRIORITY_OVERRIDE_FLAG 0x08000000
66
67 // libdispatch defines the following, so it's not safe to use for anything we
68 // expect to be passed in from userspace
69 #define _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG 0x04000000
70
71 // The event manager flag indicates that this thread/request is for a event
72 // manager thread. There can only ever be one event manager thread at a time and
73 // it is brought up at the highest of all event manager priorities passed to the
74 // kext.
75 #define _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG 0x02000000
76 #define _PTHREAD_PRIORITY_NEEDS_UNBIND_FLAG 0x01000000
77
78 // redeffed here to avoid leaving __QOS_ENUM defined in the public header
79 #define __QOS_ENUM(name, type, ...) enum { __VA_ARGS__ }; typedef type name##_t
80 #define __QOS_AVAILABLE_10_10
81 #define __QOS_AVAILABLE_10_11
82 #define __QOS_AVAILABLE_10_12
83
84 #if defined(__has_feature) && defined(__has_extension)
85 #if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums)
86 #undef __QOS_ENUM
87 #define __QOS_ENUM(name, type, ...) typedef enum : type { __VA_ARGS__ } name##_t
88 #endif
89 #if __has_feature(enumerator_attributes)
90 #undef __QOS_AVAILABLE_10_10
91 #define __QOS_AVAILABLE_10_10 __API_AVAILABLE(macos(10.10), ios(8.0))
92 #undef __QOS_AVAILABLE_10_11
93 #define __QOS_AVAILABLE_10_11 __API_AVAILABLE(macos(10.11), ios(9.0), tvos(9.0), watchos(2.0))
94 #undef __QOS_AVAILABLE_10_12
95 #define __QOS_AVAILABLE_10_12 __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
96 #endif
97 #endif
98
99 __QOS_ENUM(_pthread_set_flags, unsigned int,
100 _PTHREAD_SET_SELF_QOS_FLAG __QOS_AVAILABLE_10_10 = 0x1,
101 _PTHREAD_SET_SELF_VOUCHER_FLAG __QOS_AVAILABLE_10_10 = 0x2,
102 _PTHREAD_SET_SELF_FIXEDPRIORITY_FLAG __QOS_AVAILABLE_10_11 = 0x4,
103 _PTHREAD_SET_SELF_TIMESHARE_FLAG __QOS_AVAILABLE_10_11 = 0x8,
104 _PTHREAD_SET_SELF_WQ_KEVENT_UNBIND __QOS_AVAILABLE_10_12 = 0x10,
105 );
106
107 #undef __QOS_ENUM
108 #undef __QOS_AVAILABLE_10_10
109 #undef __QOS_AVAILABLE_10_11
110 #undef __QOS_AVAILABLE_10_12
111
112 #ifndef KERNEL
113
114 __BEGIN_DECLS
115
116 /*!
117 * @function pthread_set_qos_class_np
118 *
119 * @abstract
120 * Sets the requested QOS class and relative priority of the current thread.
121 *
122 * @discussion
123 * The QOS class and relative priority represent an overall combination of
124 * system quality of service attributes on a thread.
125 *
126 * Subsequent calls to interfaces such as pthread_setschedparam() that are
127 * incompatible or in conflict with the QOS class system will unset the QOS
128 * class requested with this interface and pthread_get_qos_class_np() will
129 * return QOS_CLASS_UNSPECIFIED thereafter. A thread so modified is permanently
130 * opted-out of the QOS class system and calls to this function to request a QOS
131 * class for such a thread will fail and return EPERM.
132 *
133 * @param __pthread
134 * The current thread as returned by pthread_self().
135 * EINVAL will be returned if any other thread is provided.
136 *
137 * @param __qos_class
138 * A QOS class value:
139 * - QOS_CLASS_USER_INTERACTIVE
140 * - QOS_CLASS_USER_INITIATED
141 * - QOS_CLASS_DEFAULT
142 * - QOS_CLASS_UTILITY
143 * - QOS_CLASS_BACKGROUND
144 * - QOS_CLASS_MAINTENANCE
145 * EINVAL will be returned if any other value is provided.
146 *
147 * @param __relative_priority
148 * A relative priority within the QOS class. This value is a negative offset
149 * from the maximum supported scheduler priority for the given class.
150 * EINVAL will be returned if the value is greater than zero or less than
151 * QOS_MIN_RELATIVE_PRIORITY.
152 *
153 * @return
154 * Zero if successful, othwerise an errno value.
155 */
156 __API_DEPRECATED_WITH_REPLACEMENT("pthread_set_qos_class_self_np", macos(10.10, 10.10), ios(8.0, 8.0))
157 int
158 pthread_set_qos_class_np(pthread_t __pthread,
159 qos_class_t __qos_class,
160 int __relative_priority);
161
162 /* Private interfaces for libdispatch to encode/decode specific values of pthread_priority_t. */
163
164 // Encode a class+priority pair into a pthread_priority_t,
165 __API_AVAILABLE(macos(10.10), ios(8.0))
166 pthread_priority_t
167 _pthread_qos_class_encode(qos_class_t qos_class, int relative_priority, unsigned long flags);
168
169 // Decode a pthread_priority_t into a class+priority pair.
170 __API_AVAILABLE(macos(10.10), ios(8.0))
171 qos_class_t
172 _pthread_qos_class_decode(pthread_priority_t priority, int *relative_priority, unsigned long *flags);
173
174 // Encode a legacy workqueue API priority into a pthread_priority_t. This API
175 // is deprecated and can be removed when the simulator no longer uses it.
176 __API_DEPRECATED("no longer used", macos(10.10, 10.13), ios(8.0, 11.0))
177 pthread_priority_t
178 _pthread_qos_class_encode_workqueue(int queue_priority, unsigned long flags);
179
180 #if __DARWIN_C_LEVEL >= __DARWIN_C_FULL
181
182 // Set QoS or voucher, or both, on pthread_self()
183 __API_AVAILABLE(macos(10.10), ios(8.0))
184 int
185 _pthread_set_properties_self(_pthread_set_flags_t flags, pthread_priority_t priority, mach_port_t voucher);
186
187 // Set self to fixed priority without disturbing QoS or priority
188 __API_AVAILABLE(macos(10.10), ios(8.0))
189 int
190 pthread_set_fixedpriority_self(void);
191
192 // Inverse of pthread_set_fixedpriority_self()
193 __API_AVAILABLE(macos(10.10), ios(8.0))
194 int
195 pthread_set_timeshare_self(void);
196
197 /*!
198 * @const PTHREAD_MAX_PARALLELISM_PHYSICAL
199 * Flag that can be used with pthread_qos_max_parallelism() and
200 * pthread_time_constraint_max_parallelism() to ask for a count of physical
201 * compute units available for parallelism (default is logical).
202 */
203 #define PTHREAD_MAX_PARALLELISM_PHYSICAL 0x1
204
205 /*!
206 * @function pthread_qos_max_parallelism
207 *
208 * @abstract
209 * Returns the number of compute units available for parallel computation at
210 * a specified QoS class.
211 *
212 * @param qos
213 * The specified QoS class.
214 *
215 * @param flags
216 * 0 or PTHREAD_MAX_PARALLELISM_PHYSICAL.
217 *
218 * @return
219 * The number of compute units available for parallel computation for the
220 * specified QoS, or -1 on failure (with errno set accordingly).
221 */
222 __API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
223 int
224 pthread_qos_max_parallelism(qos_class_t qos, unsigned long flags);
225
226 /*!
227 * @function pthread_time_constraint_max_parallelism()
228 *
229 * @abstract
230 * Returns the number of compute units available for parallel computation on
231 * realtime threads.
232 *
233 * @param flags
234 * 0 or PTHREAD_MAX_PARALLELISM_PHYSICAL.
235 *
236 * @return
237 * The number of compute units available for parallel computation on realtime
238 * threads, or -1 on failure (with errno set accordingly).
239 */
240 __API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
241 int
242 pthread_time_constraint_max_parallelism(unsigned long flags);
243
244 #endif // __DARWIN_C_LEVEL >= __DARWIN_C_FULL
245
246 __END_DECLS
247
248 #endif // KERNEL
249
250 #endif //_QOS_PRIVATE_H