Commit 675700d1 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

POSIX: use pthread-based timers

This is inefficient. It could be optimized a lot with
epoll/timerfd on Linux and kqueue/kevent on BSD. Unfortunately, the
POSIX timer API is impossible to use in a thread-safe manner, other
than with signals.
parent 4d014d82
...@@ -109,21 +109,7 @@ typedef pthread_mutex_t vlc_mutex_t; ...@@ -109,21 +109,7 @@ typedef pthread_mutex_t vlc_mutex_t;
typedef pthread_cond_t vlc_cond_t; typedef pthread_cond_t vlc_cond_t;
typedef pthread_rwlock_t vlc_rwlock_t; typedef pthread_rwlock_t vlc_rwlock_t;
typedef pthread_key_t vlc_threadvar_t; typedef pthread_key_t vlc_threadvar_t;
typedef struct vlc_timer_t vlc_timer_t; typedef struct vlc_timer *vlc_timer_t;
#ifndef __APPLE__
/* There is no POSIX timer on Mac OS X. Move that to configure eventually. */
#define HAVE_POSIX_TIMER 1
#endif
struct vlc_timer_t
{
#ifdef HAVE_POSIX_TIMER
timer_t handle;
#endif
void (*func) (void *);
void *data;
};
#elif defined( WIN32 ) #elif defined( WIN32 )
typedef HANDLE vlc_thread_t; typedef HANDLE vlc_thread_t;
......
...@@ -679,25 +679,70 @@ void vlc_control_cancel (int cmd, ...) ...@@ -679,25 +679,70 @@ void vlc_control_cancel (int cmd, ...)
assert (0); assert (0);
} }
#ifndef HAVE_POSIX_TIMER
/* We have no fallback currently. We'll just crash on timer API usage. */ struct vlc_timer
static void timer_not_supported(void) {
vlc_thread_t thread;
vlc_mutex_t lock;
vlc_cond_t wait;
void (*func) (void *);
void *data;
mtime_t value, interval;
unsigned users;
unsigned overruns;
};
static void *vlc_timer_do (void *data)
{ {
fprintf(stderr, "*** Error: Timer API is not supported on this platform.\n"); struct vlc_timer *timer = data;
abort();
timer->func (timer->data);
vlc_mutex_lock (&timer->lock);
assert (timer->users > 0);
if (--timer->users == 0)
vlc_cond_signal (&timer->wait);
vlc_mutex_unlock (&timer->lock);
return NULL;
} }
#endif
static void vlc_timer_do (union sigval val) static void *vlc_timer_thread (void *data)
{ {
vlc_timer_t *id = val.sival_ptr; struct vlc_timer *timer = data;
id->func (id->data); mtime_t value, interval;
vlc_mutex_lock (&timer->lock);
value = timer->value;
interval = timer->interval;
vlc_mutex_unlock (&timer->lock);
for (;;)
{
vlc_thread_t th;
mwait (value);
vlc_mutex_lock (&timer->lock);
if (vlc_clone (&th, vlc_timer_do, timer, VLC_THREAD_PRIORITY_INPUT))
timer->overruns++;
else
{
vlc_detach (th);
timer->users++;
}
vlc_mutex_unlock (&timer->lock);
if (interval == 0)
return NULL;
value += interval;
}
} }
/** /**
* Initializes an asynchronous timer. * Initializes an asynchronous timer.
* @warning Asynchronous timers are processed from an unspecified thread, and * @warning Asynchronous timers are processed from an unspecified thread.
* a timer is only serialized against itself. * Also, multiple occurences of an interval timer can run concurrently.
* *
* @param id pointer to timer to be initialized * @param id pointer to timer to be initialized
* @param func function that the timer will call * @param func function that the timer will call
...@@ -706,29 +751,21 @@ static void vlc_timer_do (union sigval val) ...@@ -706,29 +751,21 @@ static void vlc_timer_do (union sigval val)
*/ */
int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data) int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
{ {
#ifdef HAVE_POSIX_TIMER struct vlc_timer *timer = malloc (sizeof (*timer));
struct sigevent ev;
if (timer == NULL)
memset (&ev, 0, sizeof (ev)); return ENOMEM;
ev.sigev_notify = SIGEV_THREAD; vlc_mutex_init (&timer->lock);
ev.sigev_value.sival_ptr = id; vlc_cond_init (&timer->wait);
ev.sigev_notify_function = vlc_timer_do; assert (func);
ev.sigev_notify_attributes = NULL; timer->func = func;
id->func = func; timer->data = data;
id->data = data; timer->value = 0;
timer->interval = 0;
#if (_POSIX_CLOCK_SELECTION >= 0) timer->users = 0;
if (timer_create (CLOCK_MONOTONIC, &ev, &id->handle)) timer->overruns = 0;
#else *id = timer;
if (timer_create (CLOCK_REALTIME, &ev, &id->handle))
#endif
return errno;
return 0;
#else
timer_not_supported();
return 0; return 0;
#endif
} }
/** /**
...@@ -742,12 +779,17 @@ int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data) ...@@ -742,12 +779,17 @@ int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
*/ */
void vlc_timer_destroy (vlc_timer_t *id) void vlc_timer_destroy (vlc_timer_t *id)
{ {
#ifdef HAVE_POSIX_TIMER struct vlc_timer *timer = *id;
int val = timer_delete (id->handle);
VLC_THREAD_ASSERT ("deleting timer"); vlc_timer_schedule (id, false, 0, 0);
#else vlc_mutex_lock (&timer->lock);
timer_not_supported(); while (timer->users != 0)
#endif vlc_cond_wait (&timer->wait, &timer->lock);
vlc_mutex_unlock (&timer->lock);
vlc_cond_destroy (&timer->wait);
vlc_mutex_destroy (&timer->lock);
free (timer);
} }
/** /**
...@@ -770,29 +812,27 @@ void vlc_timer_destroy (vlc_timer_t *id) ...@@ -770,29 +812,27 @@ void vlc_timer_destroy (vlc_timer_t *id)
void vlc_timer_schedule (vlc_timer_t *id, bool absolute, void vlc_timer_schedule (vlc_timer_t *id, bool absolute,
mtime_t value, mtime_t interval) mtime_t value, mtime_t interval)
{ {
#ifdef HAVE_POSIX_TIMER struct vlc_timer *timer = *id;
lldiv_t vad = lldiv (value, CLOCK_FREQ);
lldiv_t itd = lldiv (interval, CLOCK_FREQ); vlc_mutex_lock (&timer->lock);
struct itimerspec it = { if (timer->value)
.it_interval = { {
.tv_sec = itd.quot, vlc_cancel (timer->thread);
.tv_nsec = (1000000000 / CLOCK_FREQ) * itd.rem, vlc_join (timer->thread, NULL);
}, timer->value = 0;
.it_value = { }
.tv_sec = vad.quot, if ((value != 0)
.tv_nsec = (1000000000 / CLOCK_FREQ) * vad.rem, && (vlc_clone (&timer->thread, vlc_timer_thread, timer,
}, VLC_THREAD_PRIORITY_INPUT) == 0))
}; {
int flags = absolute ? TIMER_ABSTIME : 0; timer->value = (absolute ? 0 : mdate ()) + value;
timer->interval = interval;
int val = timer_settime (id->handle, flags, &it, NULL); }
VLC_THREAD_ASSERT ("scheduling timer"); vlc_mutex_unlock (&timer->lock);
#else
timer_not_supported();
#endif
} }
/** /**
* Fetch and reset the overrun counter for a timer.
* @param id initialized timer pointer * @param id initialized timer pointer
* @return the timer overrun counter, i.e. the number of times that the timer * @return the timer overrun counter, i.e. the number of times that the timer
* should have run but did not since the last actual run. If all is well, this * should have run but did not since the last actual run. If all is well, this
...@@ -800,18 +840,12 @@ void vlc_timer_schedule (vlc_timer_t *id, bool absolute, ...@@ -800,18 +840,12 @@ void vlc_timer_schedule (vlc_timer_t *id, bool absolute,
*/ */
unsigned vlc_timer_getoverrun (const vlc_timer_t *id) unsigned vlc_timer_getoverrun (const vlc_timer_t *id)
{ {
#ifdef HAVE_POSIX_TIMER struct vlc_timer *timer = *id;
int val = timer_getoverrun (id->handle); unsigned ret;
#ifndef NDEBUG
if (val == -1) vlc_mutex_lock (&timer->lock);
{ ret = timer->overruns;
val = errno; timer->overruns = 0;
VLC_THREAD_ASSERT ("fetching timer overrun counter"); vlc_mutex_unlock (&timer->lock);
} return ret;
#endif
return val;
#else
timer_not_supported();
return 0;
#endif
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment