]> git.saurik.com Git - apple/libpthread.git/blob - kern/kern_policy.c
libpthread-105.40.1.tar.gz
[apple/libpthread.git] / kern / kern_policy.c
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>
31
32 pthread_priority_t
33 pthread_qos_class_get_priority(int qos)
34 {
35 /* Map the buckets we have in pthread_priority_t into a QoS tier. */
36 switch (qos) {
37 case THREAD_QOS_USER_INTERACTIVE: return _pthread_priority_make_newest(QOS_CLASS_USER_INTERACTIVE, 0, 0);
38 case THREAD_QOS_USER_INITIATED: return _pthread_priority_make_newest(QOS_CLASS_USER_INITIATED, 0, 0);
39 case THREAD_QOS_LEGACY: return _pthread_priority_make_newest(QOS_CLASS_DEFAULT, 0, 0);
40 case THREAD_QOS_UTILITY: return _pthread_priority_make_newest(QOS_CLASS_UTILITY, 0, 0);
41 case THREAD_QOS_BACKGROUND: return _pthread_priority_make_newest(QOS_CLASS_BACKGROUND, 0, 0);
42 case THREAD_QOS_MAINTENANCE: return _pthread_priority_make_newest(QOS_CLASS_MAINTENANCE, 0, 0);
43 default: return _pthread_priority_make_newest(QOS_CLASS_UNSPECIFIED, 0, 0);
44 }
45 }
46
47 int
48 pthread_priority_get_qos_class(pthread_priority_t priority)
49 {
50 /* Map the buckets we have in pthread_priority_t into a QoS tier. */
51 switch (_pthread_priority_get_qos_newest(priority)) {
52 case QOS_CLASS_USER_INTERACTIVE: return THREAD_QOS_USER_INTERACTIVE;
53 case QOS_CLASS_USER_INITIATED: return THREAD_QOS_USER_INITIATED;
54 case QOS_CLASS_DEFAULT: return THREAD_QOS_LEGACY;
55 case QOS_CLASS_UTILITY: return THREAD_QOS_UTILITY;
56 case QOS_CLASS_BACKGROUND: return THREAD_QOS_BACKGROUND;
57 case QOS_CLASS_MAINTENANCE: return THREAD_QOS_MAINTENANCE;
58 default: return THREAD_QOS_UNSPECIFIED;
59 }
60 }
61
62 pthread_priority_t
63 pthread_priority_from_class_index(int index)
64 {
65 qos_class_t qos;
66 switch (index) {
67 case 0: qos = QOS_CLASS_USER_INTERACTIVE; break;
68 case 1: qos = QOS_CLASS_USER_INITIATED; break;
69 case 2: qos = QOS_CLASS_DEFAULT; break;
70 case 3: qos = QOS_CLASS_UTILITY; break;
71 case 4: qos = QOS_CLASS_BACKGROUND; break;
72 case 5: qos = QOS_CLASS_MAINTENANCE; break;
73 default:
74 /* Return the utility band if we don't understand the input. */
75 qos = QOS_CLASS_UTILITY;
76 }
77
78 pthread_priority_t priority;
79 priority = _pthread_priority_make_newest(qos, 0, 0);
80
81 return priority;
82 }
83
84 int
85 pthread_priority_get_class_index(pthread_priority_t priority)
86 {
87 switch (_pthread_priority_get_qos_newest(priority)) {
88 case QOS_CLASS_USER_INTERACTIVE: return 0;
89 case QOS_CLASS_USER_INITIATED: return 1;
90 case QOS_CLASS_DEFAULT: return 2;
91 case QOS_CLASS_UTILITY: return 3;
92 case QOS_CLASS_BACKGROUND: return 4;
93 case QOS_CLASS_MAINTENANCE: return 5;
94 default:
95 /* Return the utility band if we don't understand the input. */
96 return 2;
97 }
98 }