2 * Copyright (c) 2013-2014 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <sys/cdefs.h>
28 #include <Availability.h>
31 * @typedef qos_class_t
34 * An abstract thread quality of service (QOS) classification.
37 * Thread quality of service (QOS) classes are ordered abstract representations
38 * of the nature of work that is expected to be performed by a pthread, dispatch
39 * queue, or NSOperation. Each class specifies a maximum thread scheduling
40 * priority for that band (which may be used in combination with a relative
41 * priority offset within the band), as well as quality of service
42 * characteristics for timer latency, CPU throughput, I/O throughput, network
43 * socket traffic management behavior and more.
45 * A best effort is made to allocate available system resources to every QOS
46 * class. Quality of service degredation only occurs during system resource
47 * contention, proportionally to the QOS class. That said, QOS classes
48 * representing user-initiated work attempt to achieve peak throughput while
49 * QOS classes for other work attempt to achieve peak energy and thermal
50 * efficiency, even in the absence of contention. Finally, the use of QOS
51 * classes does not allow threads to supersede any limits that may be applied
52 * to the overall process.
56 * @constant QOS_CLASS_USER_INTERACTIVE
57 * @abstract A QOS class which indicates work performed by this thread
58 * is interactive with the user.
59 * @discussion Such work is requested to run at high priority relative to other
60 * work on the system. Specifying this QOS class is a request to run with
61 * nearly all available system CPU and I/O bandwidth even under contention.
62 * This is not an energy-efficient QOS class to use for large tasks. The use of
63 * this QOS class should be limited to critical interaction with the user such
64 * as handling events on the main event loop, view drawing, animation, etc.
66 * @constant QOS_CLASS_USER_INITIATED
67 * @abstract A QOS class which indicates work performed by this thread
68 * was initiated by the user and that the user is likely waiting for the
70 * @discussion Such work is requested to run at a priority below critical user-
71 * interactive work, but relatively higher than other work on the system. This
72 * is not an energy-efficient QOS class to use for large tasks and the use of
73 * this QOS class should be limited to operations where the user is immediately
74 * waiting for the results.
76 * @constant QOS_CLASS_DEFAULT
77 * @abstract A default QOS class used by the system in cases where more specific
78 * QOS class information is not available.
79 * @discussion Such work is requested to run at a priority below critical user-
80 * interactive and user-initiated work, but relatively higher than utility and
81 * background tasks. Threads created by pthread_create() without an attribute
82 * specifying a QOS class will default to QOS_CLASS_DEFAULT. This QOS class
83 * value is not intended to be used as a work classification, it should only be
84 * set when propagating or restoring QOS class values provided by the system.
86 * @constant QOS_CLASS_UTILITY
87 * @abstract A QOS class which indicates work performed by this thread
88 * may or may not be initiated by the user and that the user is unlikely to be
89 * immediately waiting for the results.
90 * @discussion Such work is requested to run at a priority below critical user-
91 * interactive and user-initiated work, but relatively higher than low-level
92 * system maintenance tasks. The use of this QOS class indicates the work should
93 * be run in an energy and thermally-efficient manner.
95 * @constant QOS_CLASS_BACKGROUND
96 * @abstract A QOS class which indicates work performed by this thread was not
97 * initiated by the user and that the user may be unaware of the results.
98 * @discussion Such work is requested to run at a priority below other work.
99 * The use of this QOS class indicates the work should be run in the most energy
100 * and thermally-efficient manner.
102 * @constant QOS_CLASS_UNSPECIFIED
103 * @abstract A QOS class value which indicates the absence or removal of QOS
105 * @discussion As an API return value, may indicate that threads or pthread
106 * attributes were configured with legacy API incompatible or in conflict with
107 * the QOS class system.
110 #define __QOS_ENUM(name, type, ...) enum { __VA_ARGS__ }; typedef type name##_t
111 #define __QOS_CLASS_AVAILABLE_STARTING(...)
113 #if defined(__has_feature) && defined(__has_extension)
114 #if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums)
116 #define __QOS_ENUM(name, type, ...) typedef enum : type { __VA_ARGS__ } name##_t
118 #if __has_feature(enumerator_attributes)
119 #undef __QOS_CLASS_AVAILABLE_STARTING
120 #define __QOS_CLASS_AVAILABLE_STARTING __OSX_AVAILABLE_STARTING
124 __QOS_ENUM(qos_class
, unsigned int,
125 QOS_CLASS_USER_INTERACTIVE
126 __QOS_CLASS_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
) = 0x21,
127 QOS_CLASS_USER_INITIATED
128 __QOS_CLASS_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
) = 0x19,
130 __QOS_CLASS_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
) = 0x15,
132 __QOS_CLASS_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
) = 0x11,
134 __QOS_CLASS_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
) = 0x09,
135 QOS_CLASS_UNSPECIFIED
136 __QOS_CLASS_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
) = 0x00,
142 * @constant QOS_MIN_RELATIVE_PRIORITY
143 * @abstract The minimum relative priority that may be specified within a
144 * QOS class. These priorities are relative only within a given QOS class
145 * and meaningful only for the current process.
147 #define QOS_MIN_RELATIVE_PRIORITY (-15)
149 /* Userspace (only) definitions */
156 * @function qos_class_self
159 * Returns the requested QOS class of the current thread.
162 * One of the QOS class values in qos_class_t.
164 __OSX_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
)
166 qos_class_self(void);
169 * @function qos_class_main
172 * Returns the initial requested QOS class of the main thread.
175 * The QOS class that the main thread of a process is created with depends on
176 * the type of process (e.g. application or daemon) and on how it has been
179 * This function returns that initial requested QOS class value chosen by the
180 * system to enable propagation of that classification to matching work not
181 * executing on the main thread.
184 * One of the QOS class values in qos_class_t.
186 __OSX_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
)
188 qos_class_main(void);