The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 28 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 29 | #include "pthread_internal.h" |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 30 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | #include <errno.h> |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 32 | #include <linux/time.h> |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 36 | // Normal (i.e. non-SIGEV_THREAD) timers are created directly by the kernel |
| 37 | // and are passed as is to/from the caller. |
| 38 | // |
| 39 | // This file also implements the support required for SIGEV_THREAD ("POSIX interval") |
| 40 | // timers. See the following pages for additional details: |
| 41 | // |
| 42 | // www.opengroup.org/onlinepubs/000095399/functions/timer_create.html |
| 43 | // www.opengroup.org/onlinepubs/000095399/functions/timer_settime.html |
| 44 | // www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_04.html#tag_02_04_01 |
| 45 | // |
| 46 | // The Linux kernel doesn't support these, so we need to implement them in the |
| 47 | // C library. We use a very basic scheme where each timer is associated to a |
| 48 | // thread that will loop, waiting for timeouts or messages from the program |
| 49 | // corresponding to calls to timer_settime() and timer_delete(). |
| 50 | // |
| 51 | // Note also an important thing: Posix mandates that in the case of fork(), |
| 52 | // the timers of the child process should be disarmed, but not deleted. |
| 53 | // this is implemented by providing a fork() wrapper (see bionic/fork.c) which |
| 54 | // stops all timers before the fork, and only re-start them in case of error |
| 55 | // or in the parent process. |
| 56 | // |
| 57 | // This stop/start is implemented by the __timer_table_start_stop() function |
| 58 | // below. |
| 59 | // |
| 60 | // A SIGEV_THREAD timer ID will always have its TIMER_ID_WRAP_BIT |
| 61 | // set to 1. In this implementation, this is always bit 31, which is |
| 62 | // guaranteed to never be used by kernel-provided timer ids |
| 63 | // |
| 64 | // (See code in <kernel>/lib/idr.c, used to manage IDs, to see why.) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 65 | |
| 66 | #define TIMER_ID_WRAP_BIT 0x80000000 |
| 67 | #define TIMER_ID_WRAP(id) ((timer_t)((id) | TIMER_ID_WRAP_BIT)) |
| 68 | #define TIMER_ID_UNWRAP(id) ((timer_t)((id) & ~TIMER_ID_WRAP_BIT)) |
| 69 | #define TIMER_ID_IS_WRAPPED(id) (((id) & TIMER_ID_WRAP_BIT) != 0) |
| 70 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 71 | /* this value is used internally to indicate a 'free' or 'zombie' |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 | * thr_timer structure. Here, 'zombie' means that timer_delete() |
| 73 | * has been called, but that the corresponding thread hasn't |
| 74 | * exited yet. |
| 75 | */ |
| 76 | #define TIMER_ID_NONE ((timer_t)0xffffffff) |
| 77 | |
| 78 | /* True iff a timer id is valid */ |
| 79 | #define TIMER_ID_IS_VALID(id) ((id) != TIMER_ID_NONE) |
| 80 | |
| 81 | /* the maximum value of overrun counters */ |
| 82 | #define DELAYTIMER_MAX 0x7fffffff |
| 83 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 | typedef struct thr_timer thr_timer_t; |
| 85 | typedef struct thr_timer_table thr_timer_table_t; |
| 86 | |
| 87 | /* The Posix spec says the function receives an unsigned parameter, but |
| 88 | * it's really a 'union sigval' a.k.a. sigval_t */ |
| 89 | typedef void (*thr_timer_func_t)( sigval_t ); |
| 90 | |
| 91 | struct thr_timer { |
| 92 | thr_timer_t* next; /* next in free list */ |
| 93 | timer_t id; /* TIMER_ID_NONE iff free or dying */ |
| 94 | clockid_t clock; |
| 95 | pthread_t thread; |
| 96 | pthread_attr_t attributes; |
| 97 | thr_timer_func_t callback; |
| 98 | sigval_t value; |
| 99 | |
| 100 | /* the following are used to communicate between |
| 101 | * the timer thread and the timer_XXX() functions |
| 102 | */ |
| 103 | pthread_mutex_t mutex; /* lock */ |
| 104 | pthread_cond_t cond; /* signal a state change to thread */ |
| 105 | int volatile done; /* set by timer_delete */ |
| 106 | int volatile stopped; /* set by _start_stop() */ |
| 107 | struct timespec volatile expires; /* next expiration time, or 0 */ |
| 108 | struct timespec volatile period; /* reload value, or 0 */ |
| 109 | int volatile overruns; /* current number of overruns */ |
| 110 | }; |
| 111 | |
| 112 | #define MAX_THREAD_TIMERS 32 |
| 113 | |
| 114 | struct thr_timer_table { |
| 115 | pthread_mutex_t lock; |
| 116 | thr_timer_t* free_timer; |
| 117 | thr_timer_t timers[ MAX_THREAD_TIMERS ]; |
| 118 | }; |
| 119 | |
| 120 | /** GLOBAL TABLE OF THREAD TIMERS |
| 121 | **/ |
| 122 | |
| 123 | static void |
| 124 | thr_timer_table_init( thr_timer_table_t* t ) |
| 125 | { |
| 126 | int nn; |
| 127 | |
| 128 | memset(t, 0, sizeof *t); |
| 129 | pthread_mutex_init( &t->lock, NULL ); |
| 130 | |
| 131 | for (nn = 0; nn < MAX_THREAD_TIMERS; nn++) |
| 132 | t->timers[nn].id = TIMER_ID_NONE; |
| 133 | |
| 134 | t->free_timer = &t->timers[0]; |
| 135 | for (nn = 1; nn < MAX_THREAD_TIMERS; nn++) |
| 136 | t->timers[nn-1].next = &t->timers[nn]; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | static thr_timer_t* |
| 141 | thr_timer_table_alloc( thr_timer_table_t* t ) |
| 142 | { |
| 143 | thr_timer_t* timer; |
| 144 | |
| 145 | if (t == NULL) |
| 146 | return NULL; |
| 147 | |
| 148 | pthread_mutex_lock(&t->lock); |
| 149 | timer = t->free_timer; |
| 150 | if (timer != NULL) { |
| 151 | t->free_timer = timer->next; |
| 152 | timer->next = NULL; |
| 153 | timer->id = TIMER_ID_WRAP((timer - t->timers)); |
| 154 | } |
| 155 | pthread_mutex_unlock(&t->lock); |
| 156 | return timer; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | static void |
| 161 | thr_timer_table_free( thr_timer_table_t* t, thr_timer_t* timer ) |
| 162 | { |
| 163 | pthread_mutex_lock( &t->lock ); |
| 164 | timer->id = TIMER_ID_NONE; |
| 165 | timer->thread = 0; |
| 166 | timer->next = t->free_timer; |
| 167 | t->free_timer = timer; |
| 168 | pthread_mutex_unlock( &t->lock ); |
| 169 | } |
| 170 | |
| 171 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 172 | static void thr_timer_table_start_stop(thr_timer_table_t* t, int stop) { |
| 173 | if (t == NULL) { |
| 174 | return; |
| 175 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 177 | pthread_mutex_lock(&t->lock); |
| 178 | for (int nn = 0; nn < MAX_THREAD_TIMERS; ++nn) { |
| 179 | thr_timer_t* timer = &t->timers[nn]; |
| 180 | if (TIMER_ID_IS_VALID(timer->id)) { |
| 181 | // Tell the thread to start/stop. |
| 182 | pthread_mutex_lock(&timer->mutex); |
| 183 | timer->stopped = stop; |
| 184 | pthread_cond_signal( &timer->cond ); |
| 185 | pthread_mutex_unlock(&timer->mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 186 | } |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 187 | } |
| 188 | pthread_mutex_unlock(&t->lock); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | /* convert a timer_id into the corresponding thr_timer_t* pointer |
| 193 | * returns NULL if the id is not wrapped or is invalid/free |
| 194 | */ |
| 195 | static thr_timer_t* |
| 196 | thr_timer_table_from_id( thr_timer_table_t* t, |
| 197 | timer_t id, |
| 198 | int remove ) |
| 199 | { |
| 200 | unsigned index; |
| 201 | thr_timer_t* timer; |
| 202 | |
| 203 | if (t == NULL || !TIMER_ID_IS_WRAPPED(id)) |
| 204 | return NULL; |
| 205 | |
| 206 | index = (unsigned) TIMER_ID_UNWRAP(id); |
| 207 | if (index >= MAX_THREAD_TIMERS) |
| 208 | return NULL; |
| 209 | |
| 210 | pthread_mutex_lock(&t->lock); |
| 211 | |
| 212 | timer = &t->timers[index]; |
| 213 | |
| 214 | if (!TIMER_ID_IS_VALID(timer->id)) { |
| 215 | timer = NULL; |
| 216 | } else { |
| 217 | /* if we're removing this timer, clear the id |
| 218 | * right now to prevent another thread to |
| 219 | * use the same id after the unlock */ |
| 220 | if (remove) |
| 221 | timer->id = TIMER_ID_NONE; |
| 222 | } |
| 223 | pthread_mutex_unlock(&t->lock); |
| 224 | |
| 225 | return timer; |
| 226 | } |
| 227 | |
| 228 | /* the static timer table - we only create it if the process |
| 229 | * really wants to use SIGEV_THREAD timers, which should be |
| 230 | * pretty infrequent |
| 231 | */ |
| 232 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 233 | static pthread_once_t __timer_table_once = PTHREAD_ONCE_INIT; |
| 234 | static thr_timer_table_t* __timer_table; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 235 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 236 | static void __timer_table_init(void) { |
| 237 | __timer_table = calloc(1, sizeof(*__timer_table)); |
| 238 | if (__timer_table != NULL) { |
| 239 | thr_timer_table_init(__timer_table); |
| 240 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 243 | static thr_timer_table_t* __timer_table_get(void) { |
| 244 | pthread_once(&__timer_table_once, __timer_table_init); |
| 245 | return __timer_table; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** POSIX THREAD TIMERS CLEANUP ON FORK |
| 249 | ** |
| 250 | ** this should be called from the 'fork()' wrapper to stop/start |
| 251 | ** all active thread timers. this is used to implement a Posix |
| 252 | ** requirements: the timers of fork child processes must be |
| 253 | ** disarmed but not deleted. |
| 254 | **/ |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 255 | __LIBC_HIDDEN__ void __timer_table_start_stop(int stop) { |
| 256 | // We access __timer_table directly so we don't create it if it doesn't yet exist. |
| 257 | thr_timer_table_start_stop(__timer_table, stop); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static thr_timer_t* |
| 261 | thr_timer_from_id( timer_t id ) |
| 262 | { |
| 263 | thr_timer_table_t* table = __timer_table_get(); |
| 264 | thr_timer_t* timer = thr_timer_table_from_id( table, id, 0 ); |
| 265 | |
| 266 | return timer; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | static __inline__ void |
| 271 | thr_timer_lock( thr_timer_t* t ) |
| 272 | { |
| 273 | pthread_mutex_lock(&t->mutex); |
| 274 | } |
| 275 | |
| 276 | static __inline__ void |
| 277 | thr_timer_unlock( thr_timer_t* t ) |
| 278 | { |
| 279 | pthread_mutex_unlock(&t->mutex); |
| 280 | } |
| 281 | |
| 282 | /** POSIX TIMERS APIs */ |
| 283 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 284 | extern int __timer_create(clockid_t, struct sigevent*, timer_t*); |
| 285 | extern int __timer_delete(timer_t); |
| 286 | extern int __timer_gettime(timer_t, struct itimerspec*); |
| 287 | extern int __timer_settime(timer_t, int, const struct itimerspec*, struct itimerspec*); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 288 | extern int __timer_getoverrun(timer_t); |
| 289 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 290 | static void* timer_thread_start(void*); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 291 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 292 | int timer_create(clockid_t clock_id, struct sigevent* evp, timer_t* timer_id) { |
| 293 | // If not a SIGEV_THREAD timer, the kernel can handle it without our help. |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 294 | if (__predict_true(evp == NULL || evp->sigev_notify != SIGEV_THREAD)) { |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 295 | return __timer_create(clock_id, evp, timer_id); |
| 296 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 297 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 298 | // Check arguments. |
| 299 | if (evp->sigev_notify_function == NULL) { |
| 300 | errno = EINVAL; |
| 301 | return -1; |
| 302 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 303 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 304 | // Check that the clock id is supported by the kernel. |
| 305 | struct timespec dummy; |
| 306 | if (clock_gettime(clock_id, &dummy) < 0 && errno == EINVAL) { |
| 307 | return -1; |
| 308 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 309 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 310 | // Create a new timer and its thread. |
| 311 | // TODO: use a single global thread for all timers. |
| 312 | thr_timer_table_t* table = __timer_table_get(); |
| 313 | thr_timer_t* timer = thr_timer_table_alloc(table); |
| 314 | if (timer == NULL) { |
| 315 | errno = ENOMEM; |
| 316 | return -1; |
| 317 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 318 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 319 | // Copy the thread attributes. |
| 320 | if (evp->sigev_notify_attributes == NULL) { |
| 321 | pthread_attr_init(&timer->attributes); |
| 322 | } else { |
| 323 | timer->attributes = ((pthread_attr_t*) evp->sigev_notify_attributes)[0]; |
| 324 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 325 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 326 | // Posix says that the default is PTHREAD_CREATE_DETACHED and |
| 327 | // that PTHREAD_CREATE_JOINABLE has undefined behavior. |
| 328 | // So simply always use DETACHED :-) |
| 329 | pthread_attr_setdetachstate(&timer->attributes, PTHREAD_CREATE_DETACHED); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 330 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 331 | timer->callback = evp->sigev_notify_function; |
| 332 | timer->value = evp->sigev_value; |
| 333 | timer->clock = clock_id; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 334 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 335 | pthread_mutex_init(&timer->mutex, NULL); |
| 336 | pthread_cond_init(&timer->cond, NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 337 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 338 | timer->done = 0; |
| 339 | timer->stopped = 0; |
| 340 | timer->expires.tv_sec = timer->expires.tv_nsec = 0; |
| 341 | timer->period.tv_sec = timer->period.tv_nsec = 0; |
| 342 | timer->overruns = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 343 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 344 | // Create the thread. |
| 345 | int rc = pthread_create(&timer->thread, &timer->attributes, timer_thread_start, timer); |
| 346 | if (rc != 0) { |
| 347 | thr_timer_table_free(table, timer); |
| 348 | errno = rc; |
| 349 | return -1; |
| 350 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 351 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 352 | *timer_id = timer->id; |
| 353 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | |
| 357 | int |
| 358 | timer_delete( timer_t id ) |
| 359 | { |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 360 | if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 361 | return __timer_delete( id ); |
| 362 | else |
| 363 | { |
| 364 | thr_timer_table_t* table = __timer_table_get(); |
| 365 | thr_timer_t* timer = thr_timer_table_from_id(table, id, 1); |
| 366 | |
| 367 | if (timer == NULL) { |
| 368 | errno = EINVAL; |
| 369 | return -1; |
| 370 | } |
| 371 | |
| 372 | /* tell the timer's thread to stop */ |
| 373 | thr_timer_lock(timer); |
| 374 | timer->done = 1; |
| 375 | pthread_cond_signal( &timer->cond ); |
| 376 | thr_timer_unlock(timer); |
| 377 | |
| 378 | /* NOTE: the thread will call __timer_table_free() to free the |
| 379 | * timer object. the '1' parameter to thr_timer_table_from_id |
| 380 | * above ensured that the object and its timer_id cannot be |
| 381 | * reused before that. |
| 382 | */ |
| 383 | return 0; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* return the relative time until the next expiration, or 0 if |
| 388 | * the timer is disarmed */ |
| 389 | static void |
| 390 | timer_gettime_internal( thr_timer_t* timer, |
| 391 | struct itimerspec* spec) |
| 392 | { |
| 393 | struct timespec diff; |
| 394 | |
| 395 | diff = timer->expires; |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 396 | if (!timespec_is_zero(&diff)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 397 | { |
| 398 | struct timespec now; |
| 399 | |
| 400 | clock_gettime( timer->clock, &now ); |
| 401 | timespec_sub(&diff, &now); |
| 402 | |
| 403 | /* in case of overrun, return 0 */ |
| 404 | if (timespec_cmp0(&diff) < 0) { |
| 405 | timespec_zero(&diff); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | spec->it_value = diff; |
| 410 | spec->it_interval = timer->period; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | int |
| 415 | timer_gettime( timer_t id, struct itimerspec* ospec ) |
| 416 | { |
| 417 | if (ospec == NULL) { |
| 418 | errno = EINVAL; |
| 419 | return -1; |
| 420 | } |
| 421 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 422 | if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 423 | return __timer_gettime( id, ospec ); |
| 424 | } else { |
| 425 | thr_timer_t* timer = thr_timer_from_id(id); |
| 426 | |
| 427 | if (timer == NULL) { |
| 428 | errno = EINVAL; |
| 429 | return -1; |
| 430 | } |
| 431 | thr_timer_lock(timer); |
| 432 | timer_gettime_internal( timer, ospec ); |
| 433 | thr_timer_unlock(timer); |
| 434 | } |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | int |
| 440 | timer_settime( timer_t id, |
| 441 | int flags, |
| 442 | const struct itimerspec* spec, |
| 443 | struct itimerspec* ospec ) |
| 444 | { |
| 445 | if (spec == NULL) { |
| 446 | errno = EINVAL; |
| 447 | return -1; |
| 448 | } |
| 449 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 450 | if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 451 | return __timer_settime( id, flags, spec, ospec ); |
| 452 | } else { |
| 453 | thr_timer_t* timer = thr_timer_from_id(id); |
| 454 | struct timespec expires, now; |
| 455 | |
| 456 | if (timer == NULL) { |
| 457 | errno = EINVAL; |
| 458 | return -1; |
| 459 | } |
| 460 | thr_timer_lock(timer); |
| 461 | |
| 462 | /* return current timer value if ospec isn't NULL */ |
| 463 | if (ospec != NULL) { |
| 464 | timer_gettime_internal(timer, ospec ); |
| 465 | } |
| 466 | |
| 467 | /* compute next expiration time. note that if the |
| 468 | * new it_interval is 0, we should disarm the timer |
| 469 | */ |
| 470 | expires = spec->it_value; |
| 471 | if (!timespec_is_zero(&expires)) { |
| 472 | clock_gettime( timer->clock, &now ); |
| 473 | if (!(flags & TIMER_ABSTIME)) { |
| 474 | timespec_add(&expires, &now); |
| 475 | } else { |
| 476 | if (timespec_cmp(&expires, &now) < 0) |
| 477 | expires = now; |
| 478 | } |
| 479 | } |
| 480 | timer->expires = expires; |
| 481 | timer->period = spec->it_interval; |
| 482 | thr_timer_unlock( timer ); |
| 483 | |
| 484 | /* signal the change to the thread */ |
| 485 | pthread_cond_signal( &timer->cond ); |
| 486 | } |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | int |
| 492 | timer_getoverrun(timer_t id) |
| 493 | { |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 494 | if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 495 | return __timer_getoverrun( id ); |
| 496 | } else { |
| 497 | thr_timer_t* timer = thr_timer_from_id(id); |
| 498 | int result; |
| 499 | |
| 500 | if (timer == NULL) { |
| 501 | errno = EINVAL; |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | thr_timer_lock(timer); |
| 506 | result = timer->overruns; |
| 507 | thr_timer_unlock(timer); |
| 508 | |
| 509 | return result; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 514 | static void* timer_thread_start(void* arg) { |
| 515 | thr_timer_t* timer = arg; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 516 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 517 | thr_timer_lock(timer); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 518 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 519 | // Give this thread a meaningful name. |
| 520 | char name[32]; |
| 521 | snprintf(name, sizeof(name), "POSIX interval timer 0x%08x", timer->id); |
| 522 | pthread_setname_np(pthread_self(), name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 523 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 524 | // We loop until timer->done is set in timer_delete(). |
| 525 | while (!timer->done) { |
| 526 | struct timespec expires = timer->expires; |
| 527 | struct timespec period = timer->period; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 528 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 529 | // If the timer is stopped or disarmed, wait indefinitely |
| 530 | // for a state change from timer_settime/_delete/_start_stop. |
| 531 | if (timer->stopped || timespec_is_zero(&expires)) { |
| 532 | pthread_cond_wait(&timer->cond, &timer->mutex); |
| 533 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 536 | // Otherwise, we need to do a timed wait until either a |
| 537 | // state change of the timer expiration time. |
| 538 | struct timespec now; |
| 539 | clock_gettime(timer->clock, &now); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 540 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 541 | if (timespec_cmp(&expires, &now) > 0) { |
| 542 | // Cool, there was no overrun, so compute the |
| 543 | // relative timeout as 'expires - now', then wait. |
| 544 | struct timespec diff = expires; |
| 545 | timespec_sub(&diff, &now); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 546 | |
Elliott Hughes | 470631e | 2012-06-06 10:32:56 -0700 | [diff] [blame] | 547 | int ret = __pthread_cond_timedwait_relative(&timer->cond, &timer->mutex, &diff); |
| 548 | |
| 549 | // If we didn't time out, it means that a state change |
| 550 | // occurred, so loop to take care of it. |
| 551 | if (ret != ETIMEDOUT) { |
| 552 | continue; |
| 553 | } |
| 554 | } else { |
| 555 | // Overrun was detected before we could wait! |
| 556 | if (!timespec_is_zero(&period)) { |
| 557 | // For periodic timers, compute total overrun count. |
| 558 | do { |
| 559 | timespec_add(&expires, &period); |
| 560 | if (timer->overruns < DELAYTIMER_MAX) { |
| 561 | timer->overruns += 1; |
| 562 | } |
| 563 | } while (timespec_cmp(&expires, &now) < 0); |
| 564 | |
| 565 | // Backtrack the last one, because we're going to |
| 566 | // add the same value just a bit later. |
| 567 | timespec_sub(&expires, &period); |
| 568 | } else { |
| 569 | // For non-periodic timers, things are simple. |
| 570 | timer->overruns = 1; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // If we get here, a timeout was detected. |
| 575 | // First reload/disarm the timer as needed. |
| 576 | if (!timespec_is_zero(&period)) { |
| 577 | timespec_add(&expires, &period); |
| 578 | } else { |
| 579 | timespec_zero(&expires); |
| 580 | } |
| 581 | timer->expires = expires; |
| 582 | |
| 583 | // Now call the timer callback function. Release the |
| 584 | // lock to allow the function to modify the timer setting |
| 585 | // or call timer_getoverrun(). |
| 586 | // NOTE: at this point we trust the callback not to be a |
| 587 | // total moron and pthread_kill() the timer thread |
| 588 | thr_timer_unlock(timer); |
| 589 | timer->callback(timer->value); |
| 590 | thr_timer_lock(timer); |
| 591 | |
| 592 | // Now clear the overruns counter. it only makes sense |
| 593 | // within the callback. |
| 594 | timer->overruns = 0; |
| 595 | } |
| 596 | |
| 597 | thr_timer_unlock(timer); |
| 598 | |
| 599 | // Free the timer object. |
| 600 | thr_timer_table_free(__timer_table_get(), timer); |
| 601 | |
| 602 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 603 | } |