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