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.
38typedef 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_QOS_CLASS_MASK	0x00ffff00
44#define _PTHREAD_PRIORITY_QOS_CLASS_SHIFT	(8ull)
45#define _PTHREAD_PRIORITY_PRIORITY_MASK		0x000000ff
46#define _PTHREAD_PRIORITY_PRIORITY_SHIFT	(0)
47
48#define _PTHREAD_PRIORITY_OVERCOMMIT_FLAG	0x80000000
49#define _PTHREAD_PRIORITY_INHERIT_FLAG		0x40000000
50#define _PTHREAD_PRIORITY_ROOTQUEUE_FLAG	0x20000000
51#define _PTHREAD_PRIORITY_ENFORCE_FLAG		0x10000000
52#define _PTHREAD_PRIORITY_OVERRIDE_FLAG		0x08000000
53
54// redeffed here to avoid leaving __QOS_ENUM defined in the public header
55#define __QOS_ENUM(name, type, ...) enum { __VA_ARGS__ }; typedef type name##_t
56#define __QOS_AVAILABLE_STARTING(x, y)
57
58#if defined(__has_feature) && defined(__has_extension)
59#if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums)
60#undef __QOS_ENUM
61#define __QOS_ENUM(name, type, ...) typedef enum : type { __VA_ARGS__ } name##_t
62#endif
63#if __has_feature(enumerator_attributes)
64#undef __QOS_AVAILABLE_STARTING
65#define __QOS_AVAILABLE_STARTING __OSX_AVAILABLE_STARTING
66#endif
67#endif
68
69__QOS_ENUM(_pthread_set_flags, unsigned int,
70   _PTHREAD_SET_SELF_QOS_FLAG
71		   __QOS_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0) = 0x1,
72   _PTHREAD_SET_SELF_VOUCHER_FLAG
73		   __QOS_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0) = 0x2,
74   _PTHREAD_SET_SELF_FIXEDPRIORITY_FLAG
75		   __QOS_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0) = 0x4,
76);
77
78#undef __QOS_ENUM
79#undef __QOS_AVAILABLE_STARTING
80
81#ifndef KERNEL
82
83__BEGIN_DECLS
84
85/*!
86 * @function pthread_set_qos_class_np
87 *
88 * @abstract
89 * Sets the requested QOS class and relative priority of the current thread.
90 *
91 * @discussion
92 * The QOS class and relative priority represent an overall combination of
93 * system quality of service attributes on a thread.
94 *
95 * Subsequent calls to interfaces such as pthread_setschedparam() that are
96 * incompatible or in conflict with the QOS class system will unset the QOS
97 * class requested with this interface and pthread_get_qos_class_np() will
98 * return QOS_CLASS_UNSPECIFIED thereafter. A thread so modified is permanently
99 * opted-out of the QOS class system and calls to this function to request a QOS
100 * class for such a thread will fail and return EPERM.
101 *
102 * @param __pthread
103 * The current thread as returned by pthread_self().
104 * EINVAL will be returned if any other thread is provided.
105 *
106 * @param __qos_class
107 * A QOS class value:
108 *	- QOS_CLASS_USER_INTERACTIVE
109 *	- QOS_CLASS_USER_INITIATED
110 *	- QOS_CLASS_DEFAULT
111 *	- QOS_CLASS_UTILITY
112 *	- QOS_CLASS_BACKGROUND
113 *	- QOS_CLASS_MAINTENANCE
114 * EINVAL will be returned if any other value is provided.
115 *
116 * @param __relative_priority
117 * A relative priority within the QOS class. This value is a negative offset
118 * from the maximum supported scheduler priority for the given class.
119 * EINVAL will be returned if the value is greater than zero or less than
120 * QOS_MIN_RELATIVE_PRIORITY.
121 *
122 * @return
123 * Zero if successful, othwerise an errno value.
124 */
125__OSX_AVAILABLE_BUT_DEPRECATED_MSG(__MAC_10_10, __MAC_10_10, __IPHONE_8_0, __IPHONE_8_0, \
126		"Use pthread_set_qos_class_self_np() instead")
127int
128pthread_set_qos_class_np(pthread_t __pthread,
129						 qos_class_t __qos_class,
130						 int __relative_priority);
131
132/* Private interfaces for libdispatch to encode/decode specific values of pthread_priority_t. */
133
134// Encode a class+priority pair into a pthread_priority_t,
135__OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
136pthread_priority_t
137_pthread_qos_class_encode(qos_class_t qos_class, int relative_priority, unsigned long flags);
138
139// Decode a pthread_priority_t into a class+priority pair.
140__OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
141qos_class_t
142_pthread_qos_class_decode(pthread_priority_t priority, int *relative_priority, unsigned long *flags);
143
144// Encode a legacy workqueue API priority into a pthread_priority_t
145__OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
146pthread_priority_t
147_pthread_qos_class_encode_workqueue(int queue_priority, unsigned long flags);
148
149#if __DARWIN_C_LEVEL >= __DARWIN_C_FULL
150// Set QoS or voucher, or both, on pthread_self()
151__OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
152int
153_pthread_set_properties_self(_pthread_set_flags_t flags, pthread_priority_t priority, mach_port_t voucher);
154
155// Set self to fixed priority without disturbing QoS or priority
156__OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
157int
158pthread_set_fixedpriority_self(void);
159
160#endif
161
162__END_DECLS
163
164#endif // KERNEL
165
166#endif //_QOS_PRIVATE_H
167