]> git.saurik.com Git - apple/libpthread.git/blob - private/qos_private.h
libpthread-138.10.4.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_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 (~0xffffff)
43 #define _PTHREAD_PRIORITY_FLAGS_SHIFT (24ull)
44 #define _PTHREAD_PRIORITY_QOS_CLASS_MASK 0x00ffff00
45 #define _PTHREAD_PRIORITY_QOS_CLASS_SHIFT (8ull)
46 #define _PTHREAD_PRIORITY_PRIORITY_MASK 0x000000ff
47 #define _PTHREAD_PRIORITY_PRIORITY_SHIFT (0)
48
49 #define _PTHREAD_PRIORITY_OVERCOMMIT_FLAG 0x80000000
50 #define _PTHREAD_PRIORITY_INHERIT_FLAG 0x40000000
51 #define _PTHREAD_PRIORITY_ROOTQUEUE_FLAG 0x20000000
52 #define _PTHREAD_PRIORITY_ENFORCE_FLAG 0x10000000
53 #define _PTHREAD_PRIORITY_OVERRIDE_FLAG 0x08000000
54
55 // libdispatch defines the following, so it's not safe to use for anything we
56 // expect to be passed in from userspace
57 //#define _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG 0x04000000
58
59 // The event manager flag indicates that this thread/request is for a event
60 // manager thread. There can only ever be one event manager thread at a time and
61 // it is brought up at the highest of all event manager priorities passed to the
62 // kext.
63 #define _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG 0x02000000
64
65 // Used to indicate to the pthread kext that the provided event manager thread
66 // priority is actually a scheduling priority not a QoS. We can have ROOTQUEUE_FLAG
67 // perform double duty because it's never provided to the kernel.
68 #define _PTHREAD_PRIORITY_SCHED_PRI_FLAG 0x20000000
69
70 // redeffed here to avoid leaving __QOS_ENUM defined in the public header
71 #define __QOS_ENUM(name, type, ...) enum { __VA_ARGS__ }; typedef type name##_t
72 #define __QOS_AVAILABLE_STARTING(x, y)
73
74 #if defined(__has_feature) && defined(__has_extension)
75 #if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums)
76 #undef __QOS_ENUM
77 #define __QOS_ENUM(name, type, ...) typedef enum : type { __VA_ARGS__ } name##_t
78 #endif
79 #if __has_feature(enumerator_attributes)
80 #undef __QOS_AVAILABLE_STARTING
81 #define __QOS_AVAILABLE_STARTING __OSX_AVAILABLE_STARTING
82 #endif
83 #endif
84
85 __QOS_ENUM(_pthread_set_flags, unsigned int,
86 _PTHREAD_SET_SELF_QOS_FLAG
87 __QOS_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0) = 0x1,
88 _PTHREAD_SET_SELF_VOUCHER_FLAG
89 __QOS_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0) = 0x2,
90 _PTHREAD_SET_SELF_FIXEDPRIORITY_FLAG
91 __QOS_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0) = 0x4,
92 _PTHREAD_SET_SELF_TIMESHARE_FLAG
93 __QOS_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0) = 0x8,
94 );
95
96 #undef __QOS_ENUM
97 #undef __QOS_AVAILABLE_STARTING
98
99 #ifndef KERNEL
100
101 __BEGIN_DECLS
102
103 /*!
104 * @function pthread_set_qos_class_np
105 *
106 * @abstract
107 * Sets the requested QOS class and relative priority of the current thread.
108 *
109 * @discussion
110 * The QOS class and relative priority represent an overall combination of
111 * system quality of service attributes on a thread.
112 *
113 * Subsequent calls to interfaces such as pthread_setschedparam() that are
114 * incompatible or in conflict with the QOS class system will unset the QOS
115 * class requested with this interface and pthread_get_qos_class_np() will
116 * return QOS_CLASS_UNSPECIFIED thereafter. A thread so modified is permanently
117 * opted-out of the QOS class system and calls to this function to request a QOS
118 * class for such a thread will fail and return EPERM.
119 *
120 * @param __pthread
121 * The current thread as returned by pthread_self().
122 * EINVAL will be returned if any other thread is provided.
123 *
124 * @param __qos_class
125 * A QOS class value:
126 * - QOS_CLASS_USER_INTERACTIVE
127 * - QOS_CLASS_USER_INITIATED
128 * - QOS_CLASS_DEFAULT
129 * - QOS_CLASS_UTILITY
130 * - QOS_CLASS_BACKGROUND
131 * - QOS_CLASS_MAINTENANCE
132 * EINVAL will be returned if any other value is provided.
133 *
134 * @param __relative_priority
135 * A relative priority within the QOS class. This value is a negative offset
136 * from the maximum supported scheduler priority for the given class.
137 * EINVAL will be returned if the value is greater than zero or less than
138 * QOS_MIN_RELATIVE_PRIORITY.
139 *
140 * @return
141 * Zero if successful, othwerise an errno value.
142 */
143 __OSX_AVAILABLE_BUT_DEPRECATED_MSG(__MAC_10_10, __MAC_10_10, __IPHONE_8_0, __IPHONE_8_0, \
144 "Use pthread_set_qos_class_self_np() instead")
145 int
146 pthread_set_qos_class_np(pthread_t __pthread,
147 qos_class_t __qos_class,
148 int __relative_priority);
149
150 /* Private interfaces for libdispatch to encode/decode specific values of pthread_priority_t. */
151
152 // Encode a class+priority pair into a pthread_priority_t,
153 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
154 pthread_priority_t
155 _pthread_qos_class_encode(qos_class_t qos_class, int relative_priority, unsigned long flags);
156
157 // Decode a pthread_priority_t into a class+priority pair.
158 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
159 qos_class_t
160 _pthread_qos_class_decode(pthread_priority_t priority, int *relative_priority, unsigned long *flags);
161
162 // Encode a legacy workqueue API priority into a pthread_priority_t
163 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
164 pthread_priority_t
165 _pthread_qos_class_encode_workqueue(int queue_priority, unsigned long flags);
166
167 #if __DARWIN_C_LEVEL >= __DARWIN_C_FULL
168 // Set QoS or voucher, or both, on pthread_self()
169 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
170 int
171 _pthread_set_properties_self(_pthread_set_flags_t flags, pthread_priority_t priority, mach_port_t voucher);
172
173 // Set self to fixed priority without disturbing QoS or priority
174 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
175 int
176 pthread_set_fixedpriority_self(void);
177
178 // Inverse of pthread_set_fixedpriority_self()
179 __OSX_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0)
180 int
181 pthread_set_timeshare_self(void);
182
183 #endif
184
185 __END_DECLS
186
187 #endif // KERNEL
188
189 #endif //_QOS_PRIVATE_H