Commit 9ffe4106 authored by Pierre Ynard's avatar Pierre Ynard

WinCE: implement timer API

The Timer Queue Windows API is not available on WinCE, so this is a
rewrite based on timeSetEvent() and friends. It's not safe yet with
regard to timer deletion.
parent d4f1ab80
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
* Gildas Bazin <gbazin@netcourrier.com> * Gildas Bazin <gbazin@netcourrier.com>
* Clément Sténac * Clément Sténac
* Rémi Denis-Courmont * Rémi Denis-Courmont
* Pierre Ynard
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
...@@ -35,6 +36,9 @@ ...@@ -35,6 +36,9 @@
#include <stdarg.h> #include <stdarg.h>
#include <assert.h> #include <assert.h>
#include <limits.h> #include <limits.h>
#ifdef UNDER_CE
# include <mmsystem.h>
#endif
static vlc_threadvar_t cancel_key; static vlc_threadvar_t cancel_key;
...@@ -638,11 +642,17 @@ void vlc_control_cancel (int cmd, ...) ...@@ -638,11 +642,17 @@ void vlc_control_cancel (int cmd, ...)
/*** Timers ***/ /*** Timers ***/
struct vlc_timer struct vlc_timer
{ {
#ifndef UNDER_CE
HANDLE handle; HANDLE handle;
#else
unsigned id;
unsigned interval;
#endif
void (*func) (void *); void (*func) (void *);
void *data; void *data;
}; };
#ifndef UNDER_CE
static void CALLBACK vlc_timer_do (void *val, BOOLEAN timeout) static void CALLBACK vlc_timer_do (void *val, BOOLEAN timeout)
{ {
struct vlc_timer *timer = val; struct vlc_timer *timer = val;
...@@ -650,6 +660,26 @@ static void CALLBACK vlc_timer_do (void *val, BOOLEAN timeout) ...@@ -650,6 +660,26 @@ static void CALLBACK vlc_timer_do (void *val, BOOLEAN timeout)
assert (timeout); assert (timeout);
timer->func (timer->data); timer->func (timer->data);
} }
#else
static void CALLBACK vlc_timer_do (unsigned timer_id, unsigned msg,
DWORD_PTR user, DWORD_PTR unused1,
DWORD_PTR unused2)
{
struct vlc_timer *timer = (struct vlc_timer *) user;
assert (timer_id == timer->id);
(void) msg;
(void) unused1;
(void) unused2;
timer->func (timer->data);
if (timer->interval)
{
mtime_t interval = timer->interval * 1000;
vlc_timer_schedule (timer, false, interval, interval);
}
}
#endif
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)
{ {
...@@ -659,26 +689,46 @@ int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data) ...@@ -659,26 +689,46 @@ int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
return ENOMEM; return ENOMEM;
timer->func = func; timer->func = func;
timer->data = data; timer->data = data;
#ifndef UNDER_CE
timer->handle = INVALID_HANDLE_VALUE; timer->handle = INVALID_HANDLE_VALUE;
#else
timer->id = 0;
timer->interval = 0;
#endif
*id = timer; *id = timer;
return 0; return 0;
} }
void vlc_timer_destroy (vlc_timer_t timer) void vlc_timer_destroy (vlc_timer_t timer)
{ {
#ifndef UNDER_CE
if (timer->handle != INVALID_HANDLE_VALUE) if (timer->handle != INVALID_HANDLE_VALUE)
DeleteTimerQueueTimer (NULL, timer->handle, INVALID_HANDLE_VALUE); DeleteTimerQueueTimer (NULL, timer->handle, INVALID_HANDLE_VALUE);
#else
if (timer->id)
timeKillEvent (timer->id);
/* FIXME: timers that have not yet completed will trigger use-after-free */
#endif
free (timer); free (timer);
} }
void vlc_timer_schedule (vlc_timer_t timer, bool absolute, void vlc_timer_schedule (vlc_timer_t timer, bool absolute,
mtime_t value, mtime_t interval) mtime_t value, mtime_t interval)
{ {
#ifndef UNDER_CE
if (timer->handle != INVALID_HANDLE_VALUE) if (timer->handle != INVALID_HANDLE_VALUE)
{ {
DeleteTimerQueueTimer (NULL, timer->handle, NULL); DeleteTimerQueueTimer (NULL, timer->handle, NULL);
timer->handle = INVALID_HANDLE_VALUE; timer->handle = INVALID_HANDLE_VALUE;
} }
#else
if (timer->id)
{
timeKillEvent (timer->id);
timer->id = 0;
timer->interval = 0;
}
#endif
if (value == 0) if (value == 0)
return; /* Disarm */ return; /* Disarm */
...@@ -686,8 +736,29 @@ void vlc_timer_schedule (vlc_timer_t timer, bool absolute, ...@@ -686,8 +736,29 @@ void vlc_timer_schedule (vlc_timer_t timer, bool absolute,
value -= mdate (); value -= mdate ();
value = (value + 999) / 1000; value = (value + 999) / 1000;
interval = (interval + 999) / 1000; interval = (interval + 999) / 1000;
#ifndef UNDER_CE
if (!CreateTimerQueueTimer (&timer->handle, NULL, vlc_timer_do, timer, if (!CreateTimerQueueTimer (&timer->handle, NULL, vlc_timer_do, timer,
value, interval, WT_EXECUTEDEFAULT)) value, interval, WT_EXECUTEDEFAULT))
#else
TIMECAPS caps;
timeGetDevCaps (&caps, sizeof(caps));
unsigned delay = value;
delay = __MAX(delay, caps.wPeriodMin);
delay = __MIN(delay, caps.wPeriodMax);
unsigned event = TIME_ONESHOT;
if (interval == delay)
event = TIME_PERIODIC;
else if (interval)
timer->interval = interval;
timer->id = timeSetEvent (delay, delay / 20, vlc_timer_do, (DWORD) timer,
event);
if (!timer->id)
#endif
abort (); abort ();
} }
......
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