blob: ed73821c9d7ce66e7ccc2c08e17c216a05dfb309 [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 <stdio.h>
33#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034
Elliott Hughesc3f11402013-10-30 14:40:09 -070035extern int __pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const timespec*, clockid_t);
36extern int __pthread_cond_timedwait_relative(pthread_cond_t*, pthread_mutex_t*, const timespec*);
Elliott Hughes4cf13952013-07-19 16:42:27 -070037
Elliott Hughes470631e2012-06-06 10:32:56 -070038// 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 Project1dc9e472009-03-03 19:28:35 -080067
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 Hughes470631e2012-06-06 10:32:56 -070073/* this value is used internally to indicate a 'free' or 'zombie'
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074 * 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 Project1dc9e472009-03-03 19:28:35 -080086typedef struct thr_timer thr_timer_t;
87typedef 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 */
91typedef void (*thr_timer_func_t)( sigval_t );
92
93struct 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 Hughesc3f11402013-10-30 14:40:09 -0700109 timespec volatile expires; /* next expiration time, or 0 */
110 timespec volatile period; /* reload value, or 0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111 int volatile overruns; /* current number of overruns */
112};
113
114#define MAX_THREAD_TIMERS 32
115
116struct 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
125static void
126thr_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
142static thr_timer_t*
143thr_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
162static void
163thr_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 Hughes470631e2012-06-06 10:32:56 -0700174static void thr_timer_table_start_stop(thr_timer_table_t* t, int stop) {
175 if (t == NULL) {
176 return;
177 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178
Elliott Hughes470631e2012-06-06 10:32:56 -0700179 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 Project1dc9e472009-03-03 19:28:35 -0800188 }
Elliott Hughes470631e2012-06-06 10:32:56 -0700189 }
190 pthread_mutex_unlock(&t->lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191}
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 */
197static thr_timer_t*
198thr_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 Hughes470631e2012-06-06 10:32:56 -0700235static pthread_once_t __timer_table_once = PTHREAD_ONCE_INIT;
236static thr_timer_table_t* __timer_table;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237
Elliott Hughes470631e2012-06-06 10:32:56 -0700238static void __timer_table_init(void) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700239 __timer_table = reinterpret_cast<thr_timer_table_t*>(calloc(1, sizeof(*__timer_table)));
Elliott Hughes470631e2012-06-06 10:32:56 -0700240 if (__timer_table != NULL) {
241 thr_timer_table_init(__timer_table);
242 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243}
244
Elliott Hughes470631e2012-06-06 10:32:56 -0700245static 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 Project1dc9e472009-03-03 19:28:35 -0800248}
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 Hughesc3f11402013-10-30 14:40:09 -0700257void __timer_table_start_stop(int stop) {
Elliott Hughes470631e2012-06-06 10:32:56 -0700258 // 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 Project1dc9e472009-03-03 19:28:35 -0800260}
261
262static thr_timer_t*
263thr_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
272static __inline__ void
273thr_timer_lock( thr_timer_t* t )
274{
275 pthread_mutex_lock(&t->mutex);
276}
277
278static __inline__ void
279thr_timer_unlock( thr_timer_t* t )
280{
281 pthread_mutex_unlock(&t->mutex);
282}
283
Elliott Hughes4cf13952013-07-19 16:42:27 -0700284
Elliott Hughesc3f11402013-10-30 14:40:09 -0700285static __inline__ void timespec_add(timespec* a, const timespec* b) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700286 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 Hughesc3f11402013-10-30 14:40:09 -0700294static __inline__ void timespec_sub(timespec* a, const timespec* b) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700295 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 Hughesc3f11402013-10-30 14:40:09 -0700303static __inline__ void timespec_zero(timespec* a) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700304 a->tv_sec = a->tv_nsec = 0;
305}
306
Elliott Hughesc3f11402013-10-30 14:40:09 -0700307static __inline__ int timespec_is_zero(const timespec* a) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700308 return (a->tv_sec == 0 && a->tv_nsec == 0);
309}
310
Elliott Hughesc3f11402013-10-30 14:40:09 -0700311static __inline__ int timespec_cmp(const timespec* a, const timespec* b) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700312 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 Hughesc3f11402013-10-30 14:40:09 -0700319static __inline__ int timespec_cmp0(const timespec* a) {
Elliott Hughes4cf13952013-07-19 16:42:27 -0700320 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 Project1dc9e472009-03-03 19:28:35 -0800327/** POSIX TIMERS APIs */
328
Elliott Hughesc3f11402013-10-30 14:40:09 -0700329extern "C" int __timer_create(clockid_t, sigevent*, timer_t*);
330extern "C" int __timer_delete(timer_t);
331extern "C" int __timer_gettime(timer_t, itimerspec*);
332extern "C" int __timer_settime(timer_t, int, const itimerspec*, itimerspec*);
333extern "C" int __timer_getoverrun(timer_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334
Elliott Hughes470631e2012-06-06 10:32:56 -0700335static void* timer_thread_start(void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336
Elliott Hughesc3f11402013-10-30 14:40:09 -0700337int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) {
Elliott Hughes470631e2012-06-06 10:32:56 -0700338 // If not a SIGEV_THREAD timer, the kernel can handle it without our help.
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700339 if (__predict_true(evp == NULL || evp->sigev_notify != SIGEV_THREAD)) {
Elliott Hughes470631e2012-06-06 10:32:56 -0700340 return __timer_create(clock_id, evp, timer_id);
341 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800342
Elliott Hughes470631e2012-06-06 10:32:56 -0700343 // Check arguments.
344 if (evp->sigev_notify_function == NULL) {
345 errno = EINVAL;
346 return -1;
347 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800348
Elliott Hughes470631e2012-06-06 10:32:56 -0700349 // Check that the clock id is supported by the kernel.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700350 timespec dummy;
Elliott Hughes470631e2012-06-06 10:32:56 -0700351 if (clock_gettime(clock_id, &dummy) < 0 && errno == EINVAL) {
352 return -1;
353 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800354
Elliott Hughes470631e2012-06-06 10:32:56 -0700355 // 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 Project1dc9e472009-03-03 19:28:35 -0800363
Elliott Hughes470631e2012-06-06 10:32:56 -0700364 // 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 Project1dc9e472009-03-03 19:28:35 -0800370
Elliott Hughes470631e2012-06-06 10:32:56 -0700371 // 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 Project1dc9e472009-03-03 19:28:35 -0800375
Elliott Hughes470631e2012-06-06 10:32:56 -0700376 timer->callback = evp->sigev_notify_function;
377 timer->value = evp->sigev_value;
378 timer->clock = clock_id;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800379
Elliott Hughes470631e2012-06-06 10:32:56 -0700380 pthread_mutex_init(&timer->mutex, NULL);
381 pthread_cond_init(&timer->cond, NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800382
Elliott Hughes470631e2012-06-06 10:32:56 -0700383 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 Project1dc9e472009-03-03 19:28:35 -0800388
Elliott Hughes470631e2012-06-06 10:32:56 -0700389 // 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 Project1dc9e472009-03-03 19:28:35 -0800396
Elliott Hughes470631e2012-06-06 10:32:56 -0700397 *timer_id = timer->id;
398 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800399}
400
401
402int
403timer_delete( timer_t id )
404{
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700405 if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406 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 Hughesc3f11402013-10-30 14:40:09 -0700434static 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 Project1dc9e472009-03-03 19:28:35 -0800438
Elliott Hughesc3f11402013-10-30 14:40:09 -0700439 clock_gettime(timer->clock, &now);
440 timespec_sub(&diff, &now);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800441
Elliott Hughesc3f11402013-10-30 14:40:09 -0700442 /* in case of overrun, return 0 */
443 if (timespec_cmp0(&diff) < 0) {
444 timespec_zero(&diff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800445 }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700446 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800447
Elliott Hughesc3f11402013-10-30 14:40:09 -0700448 spec->it_value = diff;
449 spec->it_interval = const_cast<timespec&>(timer->period);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800450}
451
452
Elliott Hughesc3f11402013-10-30 14:40:09 -0700453int timer_gettime(timer_t id, itimerspec* ospec) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800454 if (ospec == NULL) {
455 errno = EINVAL;
456 return -1;
457 }
458
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700459 if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800460 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
476int
Elliott Hughesc3f11402013-10-30 14:40:09 -0700477timer_settime(timer_t id, int flags, const itimerspec* spec, itimerspec* ospec) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800478 if (spec == NULL) {
479 errno = EINVAL;
480 return -1;
481 }
482
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700483 if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800484 return __timer_settime( id, flags, spec, ospec );
485 } else {
486 thr_timer_t* timer = thr_timer_from_id(id);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700487 timespec expires, now;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800488
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 Hughesc3f11402013-10-30 14:40:09 -0700513 const_cast<timespec&>(timer->expires) = expires;
514 const_cast<timespec&>(timer->period) = spec->it_interval;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800515 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
524int
525timer_getoverrun(timer_t id)
526{
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700527 if ( __predict_true(!TIMER_ID_IS_WRAPPED(id)) ) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800528 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 Hughes470631e2012-06-06 10:32:56 -0700547static void* timer_thread_start(void* arg) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700548 thr_timer_t* timer = reinterpret_cast<thr_timer_t*>(arg);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549
Elliott Hughes470631e2012-06-06 10:32:56 -0700550 thr_timer_lock(timer);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800551
Elliott Hughes470631e2012-06-06 10:32:56 -0700552 // 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 Project1dc9e472009-03-03 19:28:35 -0800556
Elliott Hughes470631e2012-06-06 10:32:56 -0700557 // We loop until timer->done is set in timer_delete().
558 while (!timer->done) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700559 timespec expires = const_cast<timespec&>(timer->expires);
560 timespec period = const_cast<timespec&>(timer->period);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800561
Elliott Hughes470631e2012-06-06 10:32:56 -0700562 // 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 Project1dc9e472009-03-03 19:28:35 -0800567 }
568
Elliott Hughes470631e2012-06-06 10:32:56 -0700569 // Otherwise, we need to do a timed wait until either a
570 // state change of the timer expiration time.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700571 timespec now;
Elliott Hughes470631e2012-06-06 10:32:56 -0700572 clock_gettime(timer->clock, &now);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800573
Elliott Hughes470631e2012-06-06 10:32:56 -0700574 if (timespec_cmp(&expires, &now) > 0) {
575 // Cool, there was no overrun, so compute the
576 // relative timeout as 'expires - now', then wait.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700577 timespec diff = expires;
Elliott Hughes470631e2012-06-06 10:32:56 -0700578 timespec_sub(&diff, &now);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800579
Elliott Hughes470631e2012-06-06 10:32:56 -0700580 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 Hughesc3f11402013-10-30 14:40:09 -0700614 const_cast<timespec&>(timer->expires) = expires;
Elliott Hughes470631e2012-06-06 10:32:56 -0700615
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 Project1dc9e472009-03-03 19:28:35 -0800636}