]> git.saurik.com Git - apple/libpthread.git/blame - kern/kern_policy.c
libpthread-138.10.4.tar.gz
[apple/libpthread.git] / kern / kern_policy.c
CommitLineData
f1a1da6c
A
1/*
2 * Copyright (c) 2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include "kern_internal.h"
30#include <kern/debug.h>
964d3577 31#include <kern/assert.h>
f1a1da6c
A
32
33pthread_priority_t
34pthread_qos_class_get_priority(int qos)
35{
36 /* Map the buckets we have in pthread_priority_t into a QoS tier. */
37 switch (qos) {
38 case THREAD_QOS_USER_INTERACTIVE: return _pthread_priority_make_newest(QOS_CLASS_USER_INTERACTIVE, 0, 0);
39 case THREAD_QOS_USER_INITIATED: return _pthread_priority_make_newest(QOS_CLASS_USER_INITIATED, 0, 0);
40 case THREAD_QOS_LEGACY: return _pthread_priority_make_newest(QOS_CLASS_DEFAULT, 0, 0);
41 case THREAD_QOS_UTILITY: return _pthread_priority_make_newest(QOS_CLASS_UTILITY, 0, 0);
42 case THREAD_QOS_BACKGROUND: return _pthread_priority_make_newest(QOS_CLASS_BACKGROUND, 0, 0);
43 case THREAD_QOS_MAINTENANCE: return _pthread_priority_make_newest(QOS_CLASS_MAINTENANCE, 0, 0);
44 default: return _pthread_priority_make_newest(QOS_CLASS_UNSPECIFIED, 0, 0);
45 }
46}
47
48int
49pthread_priority_get_qos_class(pthread_priority_t priority)
50{
51 /* Map the buckets we have in pthread_priority_t into a QoS tier. */
52 switch (_pthread_priority_get_qos_newest(priority)) {
53 case QOS_CLASS_USER_INTERACTIVE: return THREAD_QOS_USER_INTERACTIVE;
54 case QOS_CLASS_USER_INITIATED: return THREAD_QOS_USER_INITIATED;
55 case QOS_CLASS_DEFAULT: return THREAD_QOS_LEGACY;
56 case QOS_CLASS_UTILITY: return THREAD_QOS_UTILITY;
57 case QOS_CLASS_BACKGROUND: return THREAD_QOS_BACKGROUND;
58 case QOS_CLASS_MAINTENANCE: return THREAD_QOS_MAINTENANCE;
59 default: return THREAD_QOS_UNSPECIFIED;
60 }
61}
62
63pthread_priority_t
64pthread_priority_from_class_index(int index)
65{
66 qos_class_t qos;
67 switch (index) {
68 case 0: qos = QOS_CLASS_USER_INTERACTIVE; break;
69 case 1: qos = QOS_CLASS_USER_INITIATED; break;
70 case 2: qos = QOS_CLASS_DEFAULT; break;
71 case 3: qos = QOS_CLASS_UTILITY; break;
72 case 4: qos = QOS_CLASS_BACKGROUND; break;
73 case 5: qos = QOS_CLASS_MAINTENANCE; break;
964d3577 74 case 6: assert(index != 6); // EVENT_MANAGER should be handled specially
f1a1da6c
A
75 default:
76 /* Return the utility band if we don't understand the input. */
77 qos = QOS_CLASS_UTILITY;
78 }
79
80 pthread_priority_t priority;
81 priority = _pthread_priority_make_newest(qos, 0, 0);
82
83 return priority;
84}
85
86int
964d3577
A
87qos_get_class_index(int qos){
88 switch (qos){
f1a1da6c
A
89 case QOS_CLASS_USER_INTERACTIVE: return 0;
90 case QOS_CLASS_USER_INITIATED: return 1;
91 case QOS_CLASS_DEFAULT: return 2;
92 case QOS_CLASS_UTILITY: return 3;
93 case QOS_CLASS_BACKGROUND: return 4;
94 case QOS_CLASS_MAINTENANCE: return 5;
95 default:
96 /* Return the utility band if we don't understand the input. */
97 return 2;
98 }
99}
964d3577
A
100
101int
102pthread_priority_get_class_index(pthread_priority_t priority)
103{
104 return qos_get_class_index(_pthread_priority_get_qos_newest(priority));
105}
106
107integer_t
108_thread_qos_from_pthread_priority(unsigned long priority, unsigned long *flags){
109 if (flags){
110 *flags = (int)_pthread_priority_get_flags(priority) >> _PTHREAD_PRIORITY_FLAGS_SHIFT;
111 }
112 return pthread_priority_get_qos_class(priority);
113}