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