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 | /* * Copyright (c) 2004 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@ */ /* * devtimer.c * - timer source based on <kern/thread_call.h> */ /* * Modification History: * * June 22, 2004 Dieter Siegmund (dieter@apple.com) * - created */ #include <sys/param.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <kern/thread_call.h> #include <net/devtimer.h> #include <libkern/OSAtomic.h> #ifdef DEVTIMER_DEBUG #define _devtimer_printf printf #else DEVTIMER_DEBUG static __inline__ void _devtimer_printf(__unused const char * fmt, ...) { } #endif DEVTIMER_DEBUG struct devtimer_s { void * dt_callout; devtimer_timeout_func dt_timeout_func; devtimer_process_func dt_process_func; void * dt_arg0; void * dt_arg1; void * dt_arg2; int dt_generation; UInt32 dt_retain_count; }; #define M_DEVTIMER M_DEVBUF static __inline__ void timeval_add(struct timeval tv1, struct timeval tv2, struct timeval * result) { result->tv_sec = tv1.tv_sec + tv2.tv_sec; result->tv_usec = tv1.tv_usec + tv2.tv_usec; if (result->tv_usec > DEVTIMER_USECS_PER_SEC) { result->tv_usec -= DEVTIMER_USECS_PER_SEC; result->tv_sec++; } return; } static __inline__ uint64_t timeval_to_absolutetime(struct timeval tv) { uint64_t secs; uint64_t usecs; clock_interval_to_absolutetime_interval(tv.tv_sec, NSEC_PER_SEC, &secs); clock_interval_to_absolutetime_interval(tv.tv_usec, NSEC_PER_USEC, &usecs); return (secs + usecs); } __private_extern__ int devtimer_valid(devtimer_ref timer) { return (timer->dt_callout != NULL); } __private_extern__ void devtimer_retain(devtimer_ref timer) { OSIncrementAtomic(&timer->dt_retain_count); return; } __private_extern__ void devtimer_invalidate(devtimer_ref timer) { devtimer_cancel(timer); timer->dt_arg0 = NULL; if (timer->dt_callout != NULL) { thread_call_free(timer->dt_callout); timer->dt_callout = NULL; } return; } __private_extern__ void devtimer_release(devtimer_ref timer) { UInt32 old_retain_count; old_retain_count = OSDecrementAtomic(&timer->dt_retain_count); switch (old_retain_count) { case 0: panic("devtimer_release: retain count is 0\n"); break; case 1: devtimer_invalidate(timer); FREE(timer, M_DEVTIMER); _devtimer_printf("devtimer: timer released\n"); break; default: break; } return; } static void devtimer_process(void * param0, void * param1) { int generation = (int)param1; devtimer_process_func process_func; devtimer_timeout_func timeout_func; devtimer_ref timer = (devtimer_ref)param0; process_func = timer->dt_process_func; if (process_func != NULL) { (*process_func)(timer, devtimer_process_func_event_lock); } timeout_func = timer->dt_timeout_func; if (timeout_func != NULL) { timer->dt_timeout_func = NULL; if (timer->dt_generation == generation) { (*timeout_func)(timer->dt_arg0, timer->dt_arg1, timer->dt_arg2); } } devtimer_release(timer); if (process_func != NULL) { (*process_func)(timer, devtimer_process_func_event_unlock); } return; } __private_extern__ void * devtimer_arg0(devtimer_ref timer) { return (timer->dt_arg0); } __private_extern__ devtimer_ref devtimer_create(devtimer_process_func process_func, void * arg0) { devtimer_ref timer; timer = _MALLOC(sizeof(*timer), M_DEVTIMER, M_WAITOK); if (timer == NULL) { return (timer); } bzero(timer, sizeof(*timer)); devtimer_retain(timer); timer->dt_callout = thread_call_allocate(devtimer_process, timer); if (timer->dt_callout == NULL) { _devtimer_printf("devtimer: thread_call_allocate failed\n"); devtimer_release(timer); timer = NULL; } timer->dt_process_func = process_func; timer->dt_arg0 = arg0; return (timer); } __private_extern__ void devtimer_set_absolute(devtimer_ref timer, struct timeval abs_time, devtimer_timeout_func timeout_func, void * arg1, void * arg2) { if (timer->dt_callout == NULL) { printf("devtimer_set_absolute: uninitialized/freed timer\n"); return; } devtimer_cancel(timer); if (timeout_func == NULL) { return; } timer->dt_timeout_func = timeout_func; timer->dt_arg1 = arg1; timer->dt_arg2 = arg2; _devtimer_printf("devtimer: wakeup time is (%d.%d)\n", abs_time.tv_sec, abs_time.tv_usec); timer->dt_generation++; devtimer_retain(timer); thread_call_enter1_delayed(timer->dt_callout, (thread_call_param_t)timer->dt_generation, timeval_to_absolutetime(abs_time)); return; } __private_extern__ void devtimer_set_relative(devtimer_ref timer, struct timeval rel_time, devtimer_timeout_func timeout_func, void * arg1, void * arg2) { struct timeval abs_time; struct timeval current_time; current_time = devtimer_current_time(); timeval_add(current_time, rel_time, &abs_time); devtimer_set_absolute(timer, abs_time, timeout_func, arg1, arg2); return; } __private_extern__ void devtimer_cancel(devtimer_ref timer) { if (timer->dt_timeout_func != NULL) { timer->dt_timeout_func = NULL; if (timer->dt_callout != NULL) { _devtimer_printf("devtimer: cancelling timer source\n"); if (thread_call_cancel(timer->dt_callout)) { devtimer_release(timer); } else { _devtimer_printf("devtimer: delayed release\n"); } } } return; } __private_extern__ int devtimer_enabled(devtimer_ref timer) { return (timer->dt_timeout_func != NULL); } __private_extern__ int32_t devtimer_current_secs(void) { struct timeval tv; tv = devtimer_current_time(); return (tv.tv_sec); } __private_extern__ struct timeval devtimer_current_time(void) { struct timeval tv; uint32_t sec; uint32_t usec; clock_get_system_microtime(&sec, &usec); tv.tv_sec = sec; tv.tv_usec = usec; return (tv); } |