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