Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | /* * Copyright (c) 1993-1995, 1999-2000 Apple Computer, Inc. * All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* * Timer interrupt callout module. * * HISTORY * * 20 December 2000 (debo) * Created. */ #include <mach/mach_types.h> #include <kern/clock.h> #include <kern/processor.h> #include <kern/timer_call.h> #include <kern/call_entry.h> decl_simple_lock_data(static,timer_call_lock) static queue_head_t delayed_call_queues[NCPUS]; static struct { int delayed_num, delayed_hiwat; } timer_calls; static boolean_t timer_call_initialized = FALSE; static void timer_call_interrupt( uint64_t timestamp); #define qe(x) ((queue_entry_t)(x)) #define TC(x) ((timer_call_t)(x)) void timer_call_initialize(void) { spl_t s; int i; if (timer_call_initialized) panic("timer_call_initialize"); simple_lock_init(&timer_call_lock, ETAP_MISC_TIMER); s = splclock(); simple_lock(&timer_call_lock); for (i = 0; i < NCPUS; i++) queue_init(&delayed_call_queues[i]); clock_set_timer_func((clock_timer_func_t)timer_call_interrupt); timer_call_initialized = TRUE; simple_unlock(&timer_call_lock); splx(s); } void timer_call_setup( timer_call_t call, timer_call_func_t func, timer_call_param_t param0) { call_entry_setup(call, func, param0); } static __inline__ void _delayed_call_enqueue( queue_t queue, timer_call_t call) { timer_call_t current; current = TC(queue_first(queue)); while (TRUE) { if ( queue_end(queue, qe(current)) || call->deadline < current->deadline ) { current = TC(queue_prev(qe(current))); break; } current = TC(queue_next(qe(current))); } insque(qe(call), qe(current)); if (++timer_calls.delayed_num > timer_calls.delayed_hiwat) timer_calls.delayed_hiwat = timer_calls.delayed_num; call->state = DELAYED; } static __inline__ void _delayed_call_dequeue( timer_call_t call) { (void)remque(qe(call)); timer_calls.delayed_num--; call->state = IDLE; } static __inline__ void _set_delayed_call_timer( timer_call_t call) { clock_set_timer_deadline(call->deadline); } boolean_t timer_call_enter( timer_call_t call, uint64_t deadline) { boolean_t result = TRUE; queue_t delayed; spl_t s; s = splclock(); simple_lock(&timer_call_lock); if (call->state == DELAYED) _delayed_call_dequeue(call); else result = FALSE; call->param1 = 0; call->deadline = deadline; delayed = &delayed_call_queues[cpu_number()]; _delayed_call_enqueue(delayed, call); if (queue_first(delayed) == qe(call)) _set_delayed_call_timer(call); simple_unlock(&timer_call_lock); splx(s); return (result); } boolean_t timer_call_enter1( timer_call_t call, timer_call_param_t param1, uint64_t deadline) { boolean_t result = TRUE; queue_t delayed; spl_t s; s = splclock(); simple_lock(&timer_call_lock); if (call->state == DELAYED) _delayed_call_dequeue(call); else result = FALSE; call->param1 = param1; call->deadline = deadline; delayed = &delayed_call_queues[cpu_number()]; _delayed_call_enqueue(delayed, call); if (queue_first(delayed) == qe(call)) _set_delayed_call_timer(call); simple_unlock(&timer_call_lock); splx(s); return (result); } boolean_t timer_call_cancel( timer_call_t call) { boolean_t result = TRUE; spl_t s; s = splclock(); simple_lock(&timer_call_lock); if (call->state == DELAYED) _delayed_call_dequeue(call); else result = FALSE; simple_unlock(&timer_call_lock); splx(s); return (result); } boolean_t timer_call_is_delayed( timer_call_t call, uint64_t *deadline) { boolean_t result = FALSE; spl_t s; s = splclock(); simple_lock(&timer_call_lock); if (call->state == DELAYED) { if (deadline != NULL) *deadline = call->deadline; result = TRUE; } simple_unlock(&timer_call_lock); splx(s); return (result); } /* * Called at splclock. */ void timer_call_shutdown( processor_t processor) { timer_call_t call; queue_t delayed, delayed1; assert(processor != current_processor()); delayed = &delayed_call_queues[processor->slot_num]; delayed1 = &delayed_call_queues[cpu_number()]; simple_lock(&timer_call_lock); call = TC(queue_first(delayed)); while (!queue_end(delayed, qe(call))) { _delayed_call_dequeue(call); _delayed_call_enqueue(delayed1, call); call = TC(queue_first(delayed)); } call = TC(queue_first(delayed1)); if (!queue_end(delayed1, qe(call))) _set_delayed_call_timer(call); simple_unlock(&timer_call_lock); } static void timer_call_interrupt( uint64_t timestamp) { timer_call_t call; queue_t delayed = &delayed_call_queues[cpu_number()]; simple_lock(&timer_call_lock); call = TC(queue_first(delayed)); while (!queue_end(delayed, qe(call))) { if (call->deadline <= timestamp) { timer_call_func_t func; timer_call_param_t param0, param1; _delayed_call_dequeue(call); func = call->func; param0 = call->param0; param1 = call->param1; simple_unlock(&timer_call_lock); (*func)(param0, param1); simple_lock(&timer_call_lock); } else break; call = TC(queue_first(delayed)); } if (!queue_end(delayed, qe(call))) _set_delayed_call_timer(call); simple_unlock(&timer_call_lock); } |