blob: 76619f312be7cecd48a14181a5144405bbc9729e [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Hughes470631e2012-06-06 10:32:56 -070012 * the documentation and/or other materials provided with the
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080013 * 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 Hughes470631e2012-06-06 10:32:56 -070022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080023 * 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 Hughes470631e2012-06-06 10:32:56 -070028
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029#include "pthread_internal.h"
Elliott Hughes470631e2012-06-06 10:32:56 -070030
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031#include <errno.h>
Elliott Hughes470631e2012-06-06 10:32:56 -070032#include <linux/time.h>
33#include <stdio.h>
34#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
Elliott Hughesc3f11402013-10-30 14:40:09 -070036extern int __pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const timespec*, clockid_t);
37extern int __pthread_cond_timedwait_relative(pthread_cond_t*, pthread_mutex_t*, const timespec*);
Elliott Hughes4cf13952013-07-19 16:42:27 -070038
Elliott Hughes470631e2012-06-06 10:32:56 -070039// 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 Project1dc9e472009-03-03 19:28:35 -080068
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 Hughes470631e2012-06-06 10:32:56 -070074/* this value is used internally to indicate a 'free' or 'zombie'
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075 * 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 Project1dc9e472009-03-03 19:28:35 -080087typedef struct thr_timer thr_timer_t;
88typedef 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 */
92typedef void (*thr_timer_func_t)( sigval_t );
93
94struct 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 Hughesc3f11402013-10-30 14:40:09 -0700110 timespec volatile expires; /* next expiration time, or 0 */
111 timespec volatile period; /* reload value, or 0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112 int volatile overruns; /* current number of overruns */
113};
114
115#define MAX_THREAD_TIMERS 32
116
117struct 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
126static void
127thr_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
143static thr_timer_t*
144thr_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
163static void
164thr_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 Hughes470631e2012-06-06 10:32:56 -0700175static void thr_timer_table_start_stop(thr_timer_table_t* t, int stop) {
176 if (t == NULL) {
177 return;
178 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179
Elliott Hughes470631e2012-06-06 10:32:56 -0700180 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 Project1dc9e472009-03-03 19:28:35 -0800189 }
Elliott Hughes470631e2012-06-06 10:32:56 -0700190 }
191 pthread_mutex_unlock(&t->lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192}
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 */
198static thr_timer_t*
199thr_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 Hughes470631e2012-06-06 10:32:56 -0700236static pthread_once_t __timer_table_once = PTHREAD_ONCE_INIT;
237static thr_timer_table_t* __timer_table;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238
Elliott Hughes470631e2012-06-06 10:32:56 -0700239static void __timer_table_init(void) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700240 __timer_table = reinterpret_cast<thr_timer_table_t*>(calloc(1, sizeof(*__timer_table)));
Elliott Hughes470631e2012-06-06 10:32:56 -0700241 if (__timer_table != NULL) {
242 thr_timer_table_init(__timer_table);
243 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244}
245
Elliott Hughes470631e2012-06-06 10:32:56 -0700246static 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 Project1dc9e472009-03-03 19:28:35 -0800249}
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 Hughesc3f11402013-10-30 14:40:09 -0700258void __timer_table_start_stop(int stop) {
Elliott Hughes470631e2012-06-06 10:32:56 -0700259 // 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 Project1dc9e472009-03-03 19:28:35 -0800261}
262
263static thr_timer_t*
264thr_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
273static __inline__ void
274thr_timer_lock( thr_timer_t* t )
275{
276 pthread_mutex_lock(&t->mutex);
277}
278
279static __inline__ void
280thr_timer_unlock( thr_timer_t* t )
281{
282 pthread_mutex_unlock(&t->mutex);
283}
284
Elliott Hughes4cf13952013-07-19 16:42:27 -0700285
Elliott Hughesc3f11402013-10-30 14:40:09 -0700286static __inline__ void timespec_add(timespec* a, const timespec* b) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700287 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 Hughesc3f11402013-10-30 14:40:09 -0700295static __inline__ void timespec_sub(timespec* a, const timespec* b) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700296 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 Hughesc3f11402013-10-30 14:40:09 -0700304static __inline__ void timespec_zero(timespec* a) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700305 a->tv_sec = a->tv_nsec = 0;
306}
307
Elliott Hughesc3f11402013-10-30 14:40:09 -0700308static __inline__ int timespec_is_zero(const timespec* a) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700309 return (a->tv_sec == 0 && a->tv_nsec == 0);
310}
311
Elliott Hughesc3f11402013-10-30 14:40:09 -0700312static __inline__ int timespec_cmp(const timespec* a, const timespec* b) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700313 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 Hughesc3f11402013-10-30 14:40:09 -0700320static __inline__ int timespec_cmp0(const timespec* a) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700321 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 Project1dc9e472009-03-03 19:28:35 -0800328/** POSIX TIMERS APIs */
329
Elliott Hughesc3f11402013-10-30 14:40:09 -0700330extern "C" int __timer_create(clockid_t, sigevent*, timer_t*);
331extern "C" int __timer_delete(timer_t);
332extern "C" int __timer_gettime(timer_t, itimerspec*);
333extern "C" int __timer_settime(timer_t, int, const itimerspec*, itimerspec*);
334extern "C" int __timer_getoverrun(timer_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800335
Elliott Hughes470631e2012-06-06 10:32:56 -0700336static void* timer_thread_start(void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800337
Elliott Hughesc3f11402013-10-30 14:40:09 -0700338int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) {
Elliott Hughes470631e2012-06-06 10:32:56 -0700339 // If not a SIGEV_THREAD timer, the kernel can handle it without our help.
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700340 if (__predict_true(evp == NULL || evp->sigev_notify != SIGEV_THREAD)) {
Elliott Hughes470631e2012-06-06 10:32:56 -0700341 return __timer_create(clock_id, evp, timer_id);
342 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343
Elliott Hughes470631e2012-06-06 10:32:56 -0700344 // Check arguments.
345 if (evp->sigev_notify_function == NULL) {
346 errno = EINVAL;
347 return -1;
348 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800349
Elliott Hughes470631e2012-06-06 10:32:56 -0700350 // Check that the clock id is supported by the kernel.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700351 timespec dummy;
Elliott Hughes470631e2012-06-06 10:32:56 -0700352 if (clock_gettime(clock_id, &dummy) < 0 && errno == EINVAL) {
353 return -1;
354 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355
Elliott Hughes470631e2012-06-06 10:32:56 -0700356 // 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 Project1dc9e472009-03-03 19:28:35 -0800364
Elliott Hughes470631e2012-06-06 10:32:56 -0700365 // 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 Project1dc9e472009-03-03 19:28:35 -0800371
Elliott Hughes470631e2012-06-06 10:32:56 -0700372 // 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 Project1dc9e472009-03-03 19:28:35 -0800376
Elliott Hughes470631e2012-06-06 10:32:56 -0700377 timer->callback = evp->sigev_notify_function;
378 timer->value = evp->sigev_value;
379 timer->clock = clock_id;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800380
Elliott Hughes470631e2012-06-06 10:32:56 -0700381 pthread_mutex_init(&timer->mutex, NULL);
382 pthread_cond_init(&timer->cond, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800383
Elliott Hughes470631e2012-06-06 10:32:56 -0700384 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 Project1dc9e472009-03-03 19:28:35 -0800389
Elliott Hughes470631e2012-06-06 10:32:56 -0700390 // 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 Project1dc9e472009-03-03 19:28:35 -0800397
Elliott Hughes470631e2012-06-06 10:32:56 -0700398 *timer_id = timer->id;
399 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800400}
401
402
403int
404timer_delete( timer_t id )
405{
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700406 if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800407 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 Hughesc3f11402013-10-30 14:40:09 -0700435static 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 Project1dc9e472009-03-03 19:28:35 -0800439
Elliott Hughesc3f11402013-10-30 14:40:09 -0700440 clock_gettime(timer->clock, &now);
441 timespec_sub(&diff, &now);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800442
Elliott Hughesc3f11402013-10-30 14:40:09 -0700443 /* in case of overrun, return 0 */
444 if (timespec_cmp0(&diff) < 0) {
445 timespec_zero(&diff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800446 }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700447 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800448
Elliott Hughesc3f11402013-10-30 14:40:09 -0700449 spec->it_value = diff;
450 spec->it_interval = const_cast<timespec&>(timer->period);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451}
452
453
Elliott Hughesc3f11402013-10-30 14:40:09 -0700454int timer_gettime(timer_t id, itimerspec* ospec) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800455 if (ospec == NULL) {
456 errno = EINVAL;
457 return -1;
458 }
459
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700460 if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800461 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
477int
Elliott Hughesc3f11402013-10-30 14:40:09 -0700478timer_settime(timer_t id, int flags, const itimerspec* spec, itimerspec* ospec) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800479 if (spec == NULL) {
480 errno = EINVAL;
481 return -1;
482 }
483
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700484 if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800485 return __timer_settime( id, flags, spec, ospec );
486 } else {
487 thr_timer_t* timer = thr_timer_from_id(id);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700488 timespec expires, now;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800489
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 Hughesc3f11402013-10-30 14:40:09 -0700514 const_cast<timespec&>(timer->expires) = expires;
515 const_cast<timespec&>(timer->period) = spec->it_interval;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800516 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
525int
526timer_getoverrun(timer_t id)
527{
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700528 if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800529 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 Hughes470631e2012-06-06 10:32:56 -0700548static void* timer_thread_start(void* arg) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700549 thr_timer_t* timer = reinterpret_cast<thr_timer_t*>(arg);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800550
Elliott Hughes470631e2012-06-06 10:32:56 -0700551 thr_timer_lock(timer);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800552
Elliott Hughes470631e2012-06-06 10:32:56 -0700553 // 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 Project1dc9e472009-03-03 19:28:35 -0800557
Elliott Hughes470631e2012-06-06 10:32:56 -0700558 // We loop until timer->done is set in timer_delete().
559 while (!timer->done) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700560 timespec expires = const_cast<timespec&>(timer->expires);
561 timespec period = const_cast<timespec&>(timer->period);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800562
Elliott Hughes470631e2012-06-06 10:32:56 -0700563 // 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 Project1dc9e472009-03-03 19:28:35 -0800568 }
569
Elliott Hughes470631e2012-06-06 10:32:56 -0700570 // Otherwise, we need to do a timed wait until either a
571 // state change of the timer expiration time.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700572 timespec now;
Elliott Hughes470631e2012-06-06 10:32:56 -0700573 clock_gettime(timer->clock, &now);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800574
Elliott Hughes470631e2012-06-06 10:32:56 -0700575 if (timespec_cmp(&expires, &now) > 0) {
576 // Cool, there was no overrun, so compute the
577 // relative timeout as 'expires - now', then wait.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700578 timespec diff = expires;
Elliott Hughes470631e2012-06-06 10:32:56 -0700579 timespec_sub(&diff, &now);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800580
Elliott Hughes470631e2012-06-06 10:32:56 -0700581 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 Hughesc3f11402013-10-30 14:40:09 -0700615 const_cast<timespec&>(timer->expires) = expires;
Elliott Hughes470631e2012-06-06 10:32:56 -0700616
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 Project1dc9e472009-03-03 19:28:35 -0800637}