* commit '3069270d8641c7bb57aa308d8f90b4d2dcbf66fc': Give the timer_create SIGEV_THREAD helper threads sensible names.
This commit is contained in:
commit
158ad015c2
@ -458,6 +458,7 @@ libc_common_cflags := \
|
|||||||
-DUSE_DL_PREFIX \
|
-DUSE_DL_PREFIX \
|
||||||
-DPOSIX_MISTAKE \
|
-DPOSIX_MISTAKE \
|
||||||
-DLOG_ON_HEAP_ERROR \
|
-DLOG_ON_HEAP_ERROR \
|
||||||
|
-std=gnu99
|
||||||
|
|
||||||
# these macro definitions are required to implement the
|
# these macro definitions are required to implement the
|
||||||
# 'timezone' and 'daylight' global variables, as well as
|
# 'timezone' and 'daylight' global variables, as well as
|
||||||
|
@ -25,42 +25,43 @@
|
|||||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "pthread_internal.h"
|
#include "pthread_internal.h"
|
||||||
#include <linux/time.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <linux/time.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
/* This file implements the support required to implement SIGEV_THREAD posix
|
// Normal (i.e. non-SIGEV_THREAD) timers are created directly by the kernel
|
||||||
* timers. See the following pages for additionnal details:
|
// and are passed as is to/from the caller.
|
||||||
*
|
//
|
||||||
* www.opengroup.org/onlinepubs/000095399/functions/timer_create.html
|
// This file also implements the support required for SIGEV_THREAD ("POSIX interval")
|
||||||
* www.opengroup.org/onlinepubs/000095399/functions/timer_settime.html
|
// timers. See the following pages for additional details:
|
||||||
* www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_04.html#tag_02_04_01
|
//
|
||||||
*
|
// www.opengroup.org/onlinepubs/000095399/functions/timer_create.html
|
||||||
* The Linux kernel doesn't support these, so we need to implement them in the
|
// www.opengroup.org/onlinepubs/000095399/functions/timer_settime.html
|
||||||
* C library. We use a very basic scheme where each timer is associated to a
|
// www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_04.html#tag_02_04_01
|
||||||
* thread that will loop, waiting for timeouts or messages from the program
|
//
|
||||||
* corresponding to calls to timer_settime() and timer_delete().
|
// The Linux kernel doesn't support these, so we need to implement them in the
|
||||||
*
|
// C library. We use a very basic scheme where each timer is associated to a
|
||||||
* Note also an important thing: Posix mandates that in the case of fork(),
|
// thread that will loop, waiting for timeouts or messages from the program
|
||||||
* the timers of the child process should be disarmed, but not deleted.
|
// corresponding to calls to timer_settime() and timer_delete().
|
||||||
* this is implemented by providing a fork() wrapper (see bionic/fork.c) which
|
//
|
||||||
* stops all timers before the fork, and only re-start them in case of error
|
// Note also an important thing: Posix mandates that in the case of fork(),
|
||||||
* or in the parent process.
|
// the timers of the child process should be disarmed, but not deleted.
|
||||||
*
|
// this is implemented by providing a fork() wrapper (see bionic/fork.c) which
|
||||||
* the stop/start is implemented by the __timer_table_start_stop() function
|
// stops all timers before the fork, and only re-start them in case of error
|
||||||
* below.
|
// or in the parent process.
|
||||||
*/
|
//
|
||||||
|
// This stop/start is implemented by the __timer_table_start_stop() function
|
||||||
/* normal (i.e. non-SIGEV_THREAD) timer ids are created directly by the kernel
|
// below.
|
||||||
* and are passed as is to/from the caller.
|
//
|
||||||
*
|
// A SIGEV_THREAD timer ID will always have its TIMER_ID_WRAP_BIT
|
||||||
* on the other hand, a SIGEV_THREAD timer ID will have its TIMER_ID_WRAP_BIT
|
// set to 1. In this implementation, this is always bit 31, which is
|
||||||
* always set to 1. In this implementation, this is always bit 31, which is
|
// guaranteed to never be used by kernel-provided timer ids
|
||||||
* guaranteed to never be used by kernel-provided timer ids
|
//
|
||||||
*
|
// (See code in <kernel>/lib/idr.c, used to manage IDs, to see why.)
|
||||||
* (see code in <kernel>/lib/idr.c, used to manage IDs, to see why)
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define TIMER_ID_WRAP_BIT 0x80000000
|
#define TIMER_ID_WRAP_BIT 0x80000000
|
||||||
#define TIMER_ID_WRAP(id) ((timer_t)((id) | TIMER_ID_WRAP_BIT))
|
#define TIMER_ID_WRAP(id) ((timer_t)((id) | TIMER_ID_WRAP_BIT))
|
||||||
@ -171,18 +172,16 @@ thr_timer_table_free( thr_timer_table_t* t, thr_timer_t* timer )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void thr_timer_table_start_stop(thr_timer_table_t* t, int stop) {
|
||||||
thr_timer_table_start_stop( thr_timer_table_t* t, int stop )
|
if (t == NULL) {
|
||||||
{
|
return;
|
||||||
int nn;
|
}
|
||||||
|
|
||||||
pthread_mutex_lock(&t->lock);
|
pthread_mutex_lock(&t->lock);
|
||||||
|
for (int nn = 0; nn < MAX_THREAD_TIMERS; ++nn) {
|
||||||
for (nn = 0; nn < MAX_THREAD_TIMERS; nn++) {
|
|
||||||
thr_timer_t* timer = &t->timers[nn];
|
thr_timer_t* timer = &t->timers[nn];
|
||||||
|
|
||||||
if (TIMER_ID_IS_VALID(timer->id)) {
|
if (TIMER_ID_IS_VALID(timer->id)) {
|
||||||
/* tell the thread to start/stop */
|
// Tell the thread to start/stop.
|
||||||
pthread_mutex_lock(&timer->mutex);
|
pthread_mutex_lock(&timer->mutex);
|
||||||
timer->stopped = stop;
|
timer->stopped = stop;
|
||||||
pthread_cond_signal( &timer->cond );
|
pthread_cond_signal( &timer->cond );
|
||||||
@ -237,18 +236,14 @@ thr_timer_table_from_id( thr_timer_table_t* t,
|
|||||||
static pthread_once_t __timer_table_once = PTHREAD_ONCE_INIT;
|
static pthread_once_t __timer_table_once = PTHREAD_ONCE_INIT;
|
||||||
static thr_timer_table_t* __timer_table;
|
static thr_timer_table_t* __timer_table;
|
||||||
|
|
||||||
static void
|
static void __timer_table_init(void) {
|
||||||
__timer_table_init( void )
|
|
||||||
{
|
|
||||||
__timer_table = calloc(1, sizeof(*__timer_table));
|
__timer_table = calloc(1, sizeof(*__timer_table));
|
||||||
|
if (__timer_table != NULL) {
|
||||||
if (__timer_table != NULL)
|
|
||||||
thr_timer_table_init(__timer_table);
|
thr_timer_table_init(__timer_table);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static thr_timer_table_t*
|
static thr_timer_table_t* __timer_table_get(void) {
|
||||||
__timer_table_get(void)
|
|
||||||
{
|
|
||||||
pthread_once(&__timer_table_once, __timer_table_init);
|
pthread_once(&__timer_table_once, __timer_table_init);
|
||||||
return __timer_table;
|
return __timer_table;
|
||||||
}
|
}
|
||||||
@ -260,13 +255,9 @@ __timer_table_get(void)
|
|||||||
** requirements: the timers of fork child processes must be
|
** requirements: the timers of fork child processes must be
|
||||||
** disarmed but not deleted.
|
** disarmed but not deleted.
|
||||||
**/
|
**/
|
||||||
__LIBC_HIDDEN__ void
|
__LIBC_HIDDEN__ void __timer_table_start_stop(int stop) {
|
||||||
__timer_table_start_stop( int stop )
|
// We access __timer_table directly so we don't create it if it doesn't yet exist.
|
||||||
{
|
thr_timer_table_start_stop(__timer_table, stop);
|
||||||
if (__timer_table != NULL) {
|
|
||||||
thr_timer_table_t* table = __timer_table_get();
|
|
||||||
thr_timer_table_start_stop(table, stop);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static thr_timer_t*
|
static thr_timer_t*
|
||||||
@ -293,7 +284,6 @@ thr_timer_unlock( thr_timer_t* t )
|
|||||||
|
|
||||||
/** POSIX TIMERS APIs */
|
/** POSIX TIMERS APIs */
|
||||||
|
|
||||||
/* first, declare the syscall stubs */
|
|
||||||
extern int __timer_create(clockid_t, struct sigevent*, timer_t*);
|
extern int __timer_create(clockid_t, struct sigevent*, timer_t*);
|
||||||
extern int __timer_delete(timer_t);
|
extern int __timer_delete(timer_t);
|
||||||
extern int __timer_gettime(timer_t, struct itimerspec*);
|
extern int __timer_gettime(timer_t, struct itimerspec*);
|
||||||
@ -302,56 +292,48 @@ extern int __timer_getoverrun(timer_t);
|
|||||||
|
|
||||||
static void* timer_thread_start(void*);
|
static void* timer_thread_start(void*);
|
||||||
|
|
||||||
/* then the wrappers themselves */
|
int timer_create(clockid_t clock_id, struct sigevent* evp, timer_t* timer_id) {
|
||||||
int
|
// If not a SIGEV_THREAD timer, the kernel can handle it without our help.
|
||||||
timer_create( clockid_t clockid, struct sigevent* evp, timer_t *ptimerid)
|
if (__likely(evp == NULL || evp->sigev_notify != SIGEV_THREAD)) {
|
||||||
{
|
return __timer_create(clock_id, evp, timer_id);
|
||||||
/* if not a SIGEV_THREAD timer, direct creation by the kernel */
|
}
|
||||||
if (__likely(evp == NULL || evp->sigev_notify != SIGEV_THREAD))
|
|
||||||
return __timer_create( clockid, evp, ptimerid );
|
|
||||||
|
|
||||||
// check arguments
|
// Check arguments.
|
||||||
if (evp->sigev_notify_function == NULL) {
|
if (evp->sigev_notify_function == NULL) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
// Check that the clock id is supported by the kernel.
|
||||||
struct timespec dummy;
|
struct timespec dummy;
|
||||||
|
if (clock_gettime(clock_id, &dummy) < 0 && errno == EINVAL) {
|
||||||
/* check that the clock id is supported by the kernel */
|
|
||||||
if (clock_gettime( clockid, &dummy ) < 0 && errno == EINVAL )
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create a new timer and its thread */
|
// Create a new timer and its thread.
|
||||||
{
|
// TODO: use a single global thread for all timers.
|
||||||
thr_timer_table_t* table = __timer_table_get();
|
thr_timer_table_t* table = __timer_table_get();
|
||||||
thr_timer_t* timer = thr_timer_table_alloc(table);
|
thr_timer_t* timer = thr_timer_table_alloc(table);
|
||||||
struct sigevent evp0;
|
|
||||||
|
|
||||||
if (timer == NULL) {
|
if (timer == NULL) {
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy the thread attributes */
|
// Copy the thread attributes.
|
||||||
if (evp->sigev_notify_attributes == NULL) {
|
if (evp->sigev_notify_attributes == NULL) {
|
||||||
pthread_attr_init(&timer->attributes);
|
pthread_attr_init(&timer->attributes);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
timer->attributes = ((pthread_attr_t*) evp->sigev_notify_attributes)[0];
|
timer->attributes = ((pthread_attr_t*) evp->sigev_notify_attributes)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Posix says that the default is PTHREAD_CREATE_DETACHED and
|
// Posix says that the default is PTHREAD_CREATE_DETACHED and
|
||||||
* that PTHREAD_CREATE_JOINABLE has undefined behaviour.
|
// that PTHREAD_CREATE_JOINABLE has undefined behavior.
|
||||||
* So simply always use DETACHED :-)
|
// So simply always use DETACHED :-)
|
||||||
*/
|
|
||||||
pthread_attr_setdetachstate(&timer->attributes, PTHREAD_CREATE_DETACHED);
|
pthread_attr_setdetachstate(&timer->attributes, PTHREAD_CREATE_DETACHED);
|
||||||
|
|
||||||
timer->callback = evp->sigev_notify_function;
|
timer->callback = evp->sigev_notify_function;
|
||||||
timer->value = evp->sigev_value;
|
timer->value = evp->sigev_value;
|
||||||
timer->clock = clockid;
|
timer->clock = clock_id;
|
||||||
|
|
||||||
pthread_mutex_init(&timer->mutex, NULL);
|
pthread_mutex_init(&timer->mutex, NULL);
|
||||||
pthread_cond_init(&timer->cond, NULL);
|
pthread_cond_init(&timer->cond, NULL);
|
||||||
@ -362,17 +344,17 @@ timer_create( clockid_t clockid, struct sigevent* evp, timer_t *ptimerid)
|
|||||||
timer->period.tv_sec = timer->period.tv_nsec = 0;
|
timer->period.tv_sec = timer->period.tv_nsec = 0;
|
||||||
timer->overruns = 0;
|
timer->overruns = 0;
|
||||||
|
|
||||||
/* create the thread */
|
// Create the thread.
|
||||||
if (pthread_create( &timer->thread, &timer->attributes, timer_thread_start, timer ) < 0) {
|
int rc = pthread_create(&timer->thread, &timer->attributes, timer_thread_start, timer);
|
||||||
thr_timer_table_free( __timer_table, timer );
|
if (rc != 0) {
|
||||||
errno = ENOMEM;
|
thr_timer_table_free(table, timer);
|
||||||
|
errno = rc;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*ptimerid = timer->id;
|
*timer_id = timer->id;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -532,78 +514,68 @@ timer_getoverrun(timer_t id)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void*
|
static void* timer_thread_start(void* arg) {
|
||||||
timer_thread_start( void* _arg )
|
thr_timer_t* timer = arg;
|
||||||
{
|
|
||||||
thr_timer_t* timer = _arg;
|
|
||||||
|
|
||||||
thr_timer_lock(timer);
|
thr_timer_lock(timer);
|
||||||
|
|
||||||
/* we loop until timer->done is set in timer_delete() */
|
// Give this thread a meaningful name.
|
||||||
while (!timer->done)
|
char name[32];
|
||||||
{
|
snprintf(name, sizeof(name), "POSIX interval timer 0x%08x", timer->id);
|
||||||
|
pthread_setname_np(pthread_self(), name);
|
||||||
|
|
||||||
|
// We loop until timer->done is set in timer_delete().
|
||||||
|
while (!timer->done) {
|
||||||
struct timespec expires = timer->expires;
|
struct timespec expires = timer->expires;
|
||||||
struct timespec period = timer->period;
|
struct timespec period = timer->period;
|
||||||
struct timespec now;
|
|
||||||
|
|
||||||
/* if the timer is stopped or disarmed, wait indefinitely
|
// If the timer is stopped or disarmed, wait indefinitely
|
||||||
* for a state change from timer_settime/_delete/_start_stop
|
// for a state change from timer_settime/_delete/_start_stop.
|
||||||
*/
|
if (timer->stopped || timespec_is_zero(&expires)) {
|
||||||
if ( timer->stopped || timespec_is_zero(&expires) )
|
|
||||||
{
|
|
||||||
pthread_cond_wait(&timer->cond, &timer->mutex);
|
pthread_cond_wait(&timer->cond, &timer->mutex);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* otherwise, we need to do a timed wait until either a
|
// Otherwise, we need to do a timed wait until either a
|
||||||
* state change of the timer expiration time.
|
// state change of the timer expiration time.
|
||||||
*/
|
struct timespec now;
|
||||||
clock_gettime(timer->clock, &now);
|
clock_gettime(timer->clock, &now);
|
||||||
|
|
||||||
if (timespec_cmp( &expires, &now ) > 0)
|
if (timespec_cmp(&expires, &now) > 0) {
|
||||||
{
|
// Cool, there was no overrun, so compute the
|
||||||
/* cool, there was no overrun, so compute the
|
// relative timeout as 'expires - now', then wait.
|
||||||
* relative timeout as 'expires - now', then wait
|
|
||||||
*/
|
|
||||||
int ret;
|
|
||||||
struct timespec diff = expires;
|
struct timespec diff = expires;
|
||||||
timespec_sub(&diff, &now);
|
timespec_sub(&diff, &now);
|
||||||
|
|
||||||
ret = __pthread_cond_timedwait_relative(
|
int ret = __pthread_cond_timedwait_relative(&timer->cond, &timer->mutex, &diff);
|
||||||
&timer->cond, &timer->mutex, &diff);
|
|
||||||
|
|
||||||
/* if we didn't timeout, it means that a state change
|
// If we didn't time out, it means that a state change
|
||||||
* occured, so reloop to take care of it.
|
// occurred, so loop to take care of it.
|
||||||
*/
|
if (ret != ETIMEDOUT) {
|
||||||
if (ret != ETIMEDOUT)
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else
|
} else {
|
||||||
{
|
// Overrun was detected before we could wait!
|
||||||
/* overrun was detected before we could wait ! */
|
if (!timespec_is_zero(&period)) {
|
||||||
if (!timespec_is_zero( &period ) )
|
// For periodic timers, compute total overrun count.
|
||||||
{
|
|
||||||
/* for periodic timers, compute total overrun count */
|
|
||||||
do {
|
do {
|
||||||
timespec_add(&expires, &period);
|
timespec_add(&expires, &period);
|
||||||
if (timer->overruns < DELAYTIMER_MAX)
|
if (timer->overruns < DELAYTIMER_MAX) {
|
||||||
timer->overruns += 1;
|
timer->overruns += 1;
|
||||||
|
}
|
||||||
} while (timespec_cmp(&expires, &now) < 0);
|
} while (timespec_cmp(&expires, &now) < 0);
|
||||||
|
|
||||||
/* backtrack the last one, because we're going to
|
// Backtrack the last one, because we're going to
|
||||||
* add the same value just a bit later */
|
// add the same value just a bit later.
|
||||||
timespec_sub(&expires, &period);
|
timespec_sub(&expires, &period);
|
||||||
}
|
} else {
|
||||||
else
|
// For non-periodic timers, things are simple.
|
||||||
{
|
|
||||||
/* for non-periodic timer, things are simple */
|
|
||||||
timer->overruns = 1;
|
timer->overruns = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if we get there, a timeout was detected.
|
// If we get here, a timeout was detected.
|
||||||
* first reload/disarm the timer has needed
|
// First reload/disarm the timer as needed.
|
||||||
*/
|
|
||||||
if (!timespec_is_zero(&period)) {
|
if (!timespec_is_zero(&period)) {
|
||||||
timespec_add(&expires, &period);
|
timespec_add(&expires, &period);
|
||||||
} else {
|
} else {
|
||||||
@ -611,29 +583,24 @@ timer_thread_start( void* _arg )
|
|||||||
}
|
}
|
||||||
timer->expires = expires;
|
timer->expires = expires;
|
||||||
|
|
||||||
/* now call the timer callback function. release the
|
// Now call the timer callback function. Release the
|
||||||
* lock to allow the function to modify the timer setting
|
// lock to allow the function to modify the timer setting
|
||||||
* or call timer_getoverrun().
|
// or call timer_getoverrun().
|
||||||
*
|
// NOTE: at this point we trust the callback not to be a
|
||||||
* NOTE: at this point we trust the callback not to be a
|
// total moron and pthread_kill() the timer thread
|
||||||
* total moron and pthread_kill() the timer thread
|
|
||||||
*/
|
|
||||||
thr_timer_unlock(timer);
|
thr_timer_unlock(timer);
|
||||||
timer->callback(timer->value);
|
timer->callback(timer->value);
|
||||||
thr_timer_lock(timer);
|
thr_timer_lock(timer);
|
||||||
|
|
||||||
/* now clear the overruns counter. it only makes sense
|
// Now clear the overruns counter. it only makes sense
|
||||||
* within the callback */
|
// within the callback.
|
||||||
timer->overruns = 0;
|
timer->overruns = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
thr_timer_unlock(timer);
|
thr_timer_unlock(timer);
|
||||||
|
|
||||||
/* free the timer object now. there is no need to call
|
// Free the timer object.
|
||||||
* __timer_table_get() since we're guaranteed that __timer_table
|
thr_timer_table_free(__timer_table_get(), timer);
|
||||||
* is initialized in this thread
|
|
||||||
*/
|
|
||||||
thr_timer_table_free(__timer_table, timer);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user