]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/priority.c
xnu-344.23.tar.gz
[apple/xnu.git] / osfmk / kern / priority.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * File: clock_prim.c
54 * Author: Avadis Tevanian, Jr.
55 * Date: 1986
56 *
57 * Clock primitives.
58 */
59
60 #include <cpus.h>
61
62 #include <mach/boolean.h>
63 #include <mach/kern_return.h>
64 #include <mach/machine.h>
65 #include <kern/host.h>
66 #include <kern/mach_param.h>
67 #include <kern/sched.h>
68 #include <kern/spl.h>
69 #include <kern/thread.h>
70 #include <kern/processor.h>
71 #include <machine/machparam.h>
72
73 /*
74 * thread_quantum_expire:
75 *
76 * Recalculate the quantum and priority for a thread.
77 */
78
79 void
80 thread_quantum_expire(
81 timer_call_param_t p0,
82 timer_call_param_t p1)
83 {
84 register processor_t myprocessor = p0;
85 register thread_t thread = p1;
86 spl_t s;
87
88 s = splsched();
89 thread_lock(thread);
90
91 /*
92 * Check for fail-safe trip.
93 */
94 if (!(thread->sched_mode & TH_MODE_TIMESHARE)) {
95 extern uint64_t max_unsafe_computation;
96 uint64_t new_computation;
97
98 new_computation = myprocessor->quantum_end;
99 new_computation -= thread->computation_epoch;
100 if (new_computation + thread->computation_metered >
101 max_unsafe_computation) {
102 extern uint32_t sched_safe_duration;
103
104 if (thread->sched_mode & TH_MODE_REALTIME) {
105 thread->priority = DEPRESSPRI;
106
107 thread->safe_mode |= TH_MODE_REALTIME;
108 thread->sched_mode &= ~TH_MODE_REALTIME;
109 }
110
111 thread->safe_release = sched_tick + sched_safe_duration;
112 thread->sched_mode |= (TH_MODE_FAILSAFE|TH_MODE_TIMESHARE);
113 thread->sched_mode &= ~TH_MODE_PREEMPT;
114 }
115 }
116
117 /*
118 * Recompute scheduled priority if appropriate.
119 */
120 if (thread->sched_stamp != sched_tick)
121 update_priority(thread);
122 else
123 if (thread->sched_mode & TH_MODE_TIMESHARE) {
124 thread_timer_delta(thread);
125 thread->sched_usage += thread->sched_delta;
126 thread->sched_delta = 0;
127
128 /*
129 * Adjust the scheduled priority if
130 * the thread has not been promoted
131 * and is not depressed.
132 */
133 if ( !(thread->sched_mode & TH_MODE_PROMOTED) &&
134 !(thread->sched_mode & TH_MODE_ISDEPRESSED) )
135 compute_my_priority(thread);
136 }
137
138 /*
139 * This quantum is up, give this thread another.
140 */
141 if (first_quantum(myprocessor))
142 myprocessor->slice_quanta--;
143
144 thread->current_quantum = (thread->sched_mode & TH_MODE_REALTIME)?
145 thread->realtime.computation: std_quantum;
146 myprocessor->quantum_end += thread->current_quantum;
147 timer_call_enter1(&myprocessor->quantum_timer,
148 thread, myprocessor->quantum_end);
149
150 thread_unlock(thread);
151
152 /*
153 * Check for and schedule ast if needed.
154 */
155 ast_check(myprocessor);
156
157 splx(s);
158 }