]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/timer_call.c
941061c3d30fcbac7a1fb855b2bbd12e2a340646
[apple/xnu.git] / osfmk / kern / timer_call.c
1 /*
2 * Copyright (c) 1993-2007 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 * Timer interrupt callout module.
30 */
31
32 #include <mach/mach_types.h>
33
34 #include <kern/clock.h>
35 #include <kern/processor.h>
36 #include <kern/etimer.h>
37 #include <kern/timer_call.h>
38 #include <kern/call_entry.h>
39
40 #include <sys/kdebug.h>
41
42 #if CONFIG_DTRACE && (DEVELOPMENT || DEBUG )
43 #include <mach/sdt.h>
44 #endif
45
46 decl_simple_lock_data(static,timer_call_lock)
47
48 static void
49 timer_call_interrupt(
50 uint64_t timestamp);
51
52 #define qe(x) ((queue_entry_t)(x))
53 #define TC(x) ((timer_call_t)(x))
54
55 void
56 timer_call_initialize(void)
57 {
58 spl_t s;
59
60 simple_lock_init(&timer_call_lock, 0);
61
62 s = splclock();
63 simple_lock(&timer_call_lock);
64
65 clock_set_timer_func((clock_timer_func_t)timer_call_interrupt);
66
67 simple_unlock(&timer_call_lock);
68 splx(s);
69 }
70
71 void
72 timer_call_setup(
73 timer_call_t call,
74 timer_call_func_t func,
75 timer_call_param_t param0)
76 {
77 call_entry_setup(call, func, param0);
78 }
79
80 static __inline__
81 void
82 _delayed_call_enqueue(
83 queue_t queue,
84 timer_call_t call)
85 {
86 timer_call_t current;
87
88 current = TC(queue_first(queue));
89
90 while (TRUE) {
91 if ( queue_end(queue, qe(current)) ||
92 call->deadline < current->deadline ) {
93 current = TC(queue_prev(qe(current)));
94 break;
95 }
96
97 current = TC(queue_next(qe(current)));
98 }
99
100 insque(qe(call), qe(current));
101
102 call->state = DELAYED;
103 }
104
105 static __inline__
106 void
107 _delayed_call_dequeue(
108 timer_call_t call)
109 {
110 (void)remque(qe(call));
111
112 call->state = IDLE;
113 }
114
115 static __inline__
116 void
117 _set_delayed_call_timer(
118 timer_call_t call)
119 {
120 etimer_set_deadline(call->deadline);
121 }
122
123 boolean_t
124 timer_call_enter(
125 timer_call_t call,
126 uint64_t deadline)
127 {
128 boolean_t result = TRUE;
129 queue_t queue;
130 spl_t s;
131
132 s = splclock();
133 simple_lock(&timer_call_lock);
134
135 if (call->state == DELAYED)
136 _delayed_call_dequeue(call);
137 else
138 result = FALSE;
139
140 call->param1 = NULL;
141 call->deadline = deadline;
142
143 queue = &PROCESSOR_DATA(current_processor(), timer_call_queue);
144
145 _delayed_call_enqueue(queue, call);
146
147 if (queue_first(queue) == qe(call))
148 _set_delayed_call_timer(call);
149
150 simple_unlock(&timer_call_lock);
151 splx(s);
152
153 return (result);
154 }
155
156 boolean_t
157 timer_call_enter1(
158 timer_call_t call,
159 timer_call_param_t param1,
160 uint64_t deadline)
161 {
162 boolean_t result = TRUE;
163 queue_t queue;
164 spl_t s;
165
166 s = splclock();
167 simple_lock(&timer_call_lock);
168
169 if (call->state == DELAYED)
170 _delayed_call_dequeue(call);
171 else
172 result = FALSE;
173
174 call->param1 = param1;
175 call->deadline = deadline;
176
177 queue = &PROCESSOR_DATA(current_processor(), timer_call_queue);
178
179 _delayed_call_enqueue(queue, call);
180
181 if (queue_first(queue) == qe(call))
182 _set_delayed_call_timer(call);
183
184 simple_unlock(&timer_call_lock);
185 splx(s);
186
187 return (result);
188 }
189
190 boolean_t
191 timer_call_cancel(
192 timer_call_t call)
193 {
194 boolean_t result = TRUE;
195 spl_t s;
196
197 s = splclock();
198 simple_lock(&timer_call_lock);
199
200 if (call->state == DELAYED) {
201 queue_t queue = &PROCESSOR_DATA(current_processor(), timer_call_queue);
202
203 if (queue_first(queue) == qe(call)) {
204 _delayed_call_dequeue(call);
205
206 if (!queue_empty(queue))
207 _set_delayed_call_timer((timer_call_t)queue_first(queue));
208 }
209 else
210 _delayed_call_dequeue(call);
211 }
212 else
213 result = FALSE;
214
215 simple_unlock(&timer_call_lock);
216 splx(s);
217
218 return (result);
219 }
220
221 boolean_t
222 timer_call_is_delayed(
223 timer_call_t call,
224 uint64_t *deadline)
225 {
226 boolean_t result = FALSE;
227 spl_t s;
228
229 s = splclock();
230 simple_lock(&timer_call_lock);
231
232 if (call->state == DELAYED) {
233 if (deadline != NULL)
234 *deadline = call->deadline;
235 result = TRUE;
236 }
237
238 simple_unlock(&timer_call_lock);
239 splx(s);
240
241 return (result);
242 }
243
244 /*
245 * Called at splclock.
246 */
247
248 void
249 timer_call_shutdown(
250 processor_t processor)
251 {
252 timer_call_t call;
253 queue_t queue, myqueue;
254
255 assert(processor != current_processor());
256
257 queue = &PROCESSOR_DATA(processor, timer_call_queue);
258 myqueue = &PROCESSOR_DATA(current_processor(), timer_call_queue);
259
260 simple_lock(&timer_call_lock);
261
262 call = TC(queue_first(queue));
263
264 while (!queue_end(queue, qe(call))) {
265 _delayed_call_dequeue(call);
266
267 _delayed_call_enqueue(myqueue, call);
268
269 call = TC(queue_first(queue));
270 }
271
272 call = TC(queue_first(myqueue));
273
274 if (!queue_end(myqueue, qe(call)))
275 _set_delayed_call_timer(call);
276
277 simple_unlock(&timer_call_lock);
278 }
279
280 static void
281 timer_call_interrupt(uint64_t timestamp)
282 {
283 timer_call_t call;
284 queue_t queue;
285
286 simple_lock(&timer_call_lock);
287
288 queue = &PROCESSOR_DATA(current_processor(), timer_call_queue);
289
290 call = TC(queue_first(queue));
291
292 while (!queue_end(queue, qe(call))) {
293 if (call->deadline <= timestamp) {
294 timer_call_func_t func;
295 timer_call_param_t param0, param1;
296
297 _delayed_call_dequeue(call);
298
299 func = call->func;
300 param0 = call->param0;
301 param1 = call->param1;
302
303 simple_unlock(&timer_call_lock);
304
305 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI,
306 2)
307 | DBG_FUNC_START,
308 (unsigned int)func,
309 (unsigned int)param0,
310 (unsigned int)param1, 0, 0);
311
312 #if CONFIG_DTRACE && (DEVELOPMENT || DEBUG )
313 DTRACE_TMR3(callout__start, timer_call_func_t, func,
314 timer_call_param_t, param0,
315 timer_call_param_t, param1);
316 #endif
317
318 (*func)(param0, param1);
319
320 #if CONFIG_DTRACE && (DEVELOPMENT || DEBUG )
321 DTRACE_TMR3(callout__end, timer_call_func_t, func,
322 timer_call_param_t, param0,
323 timer_call_param_t, param1);
324 #endif
325
326 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_DECI,
327 2)
328 | DBG_FUNC_END,
329 (unsigned int)func,
330 (unsigned int)param0,
331 (unsigned int)param1, 0, 0);
332
333 simple_lock(&timer_call_lock);
334 } else
335 break;
336
337 call = TC(queue_first(queue));
338 }
339
340 if (!queue_end(queue, qe(call)))
341 _set_delayed_call_timer(call);
342
343 simple_unlock(&timer_call_lock);
344 }