[vlc-devel] [RFC] WinCE: implement timer API

Pierre Ynard linkfanel at yahoo.fr
Thu Jul 30 22:59:35 CEST 2009


The Timer Queue Windows API is not available on WinCE, so this is a
rewrite based on timeSetEvent() and friends. It may still need a few
cosmetics fixes, but I'm leaving on a vacation and won't be able to work
on it, so I thought I'd post it anyway.

There is one performance concern: timeSetEvent() internally calls
timeBeginPeriod() based on the resolution argument, which modifies the
frequency of the thread scheduler. I put a resolution of one 20th of the
timer interval. What do we want there, what precision do the callers
require?


diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 7b7a5b7..6b0e649 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -159,7 +159,12 @@ typedef DWORD   vlc_threadvar_t;
 typedef struct vlc_timer_t vlc_timer_t;
 struct vlc_timer_t
 {
+#ifndef UNDER_CE
     HANDLE handle;
+#else
+    unsigned int id;
+    mtime_t interval;
+#endif
     void (*func) (void *);
     void *data;
 };
diff --git a/src/misc/w32thread.c b/src/misc/w32thread.c
index b369733..369ecf6 100644
--- a/src/misc/w32thread.c
+++ b/src/misc/w32thread.c
@@ -9,6 +9,7 @@
  *          Gildas Bazin <gbazin at netcourrier.com>
  *          Clément Sténac
  *          Rémi Denis-Courmont
+ *          Pierre Ynard
  *
  * 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
@@ -35,6 +36,9 @@
 #include <stdarg.h>
 #include <assert.h>
 #include <limits.h>
+#ifdef UNDER_CE
+# include <mmsystem.h>
+#endif
 
 static vlc_threadvar_t cancel_key;
 
@@ -592,6 +596,80 @@ void vlc_control_cancel (int cmd, ...)
 
 
 /*** Timers ***/
+#ifdef UNDER_CE
+static void CALLBACK vlc_timer_do (unsigned int timer_id, unsigned int msg,
+                                   DWORD_PTR user, DWORD_PTR unused1,
+                                   DWORD_PTR unused2)
+{
+    vlc_timer_t *id = (vlc_timer_t *) user;
+    assert (timer_id == id->id);
+    (void) msg;
+    (void) unused1;
+    (void) unused2;
+
+    id->func (id->data);
+
+    if (id->interval)
+        vlc_timer_schedule (id, false, id->interval * 1000, id->interval * 1000);
+}
+
+int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
+{
+    id->func = func;
+    id->data = data;
+    id->id = 0;
+    id->interval = 0;
+    return 0;
+}
+
+void vlc_timer_destroy (vlc_timer_t *id)
+{
+    if (id->id)
+        timeKillEvent (id->id);
+}
+
+void vlc_timer_schedule (vlc_timer_t *id, bool absolute,
+                         mtime_t value, mtime_t interval)
+{
+    if (id->id)
+    {
+        timeKillEvent (id->id);
+        id->id = 0;
+        id->interval = 0;
+    }
+    if (value == 0)
+        return; /* Disarm */
+
+    if (absolute)
+        value -= mdate ();
+    value = (value + 999) / 1000;
+    interval = (interval + 999) / 1000;
+
+    TIMECAPS caps;
+    timeGetDevCaps (&caps, sizeof(caps));
+
+    mtime_t 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)
+    {
+        id->interval = interval;
+    }
+
+    id->id = timeSetEvent (delay, delay / 20, vlc_timer_do, (DWORD) id, event);
+    if (!id)
+        abort ();
+}
+
+#else /* Win32 implementation now */
+
 static void CALLBACK vlc_timer_do (void *val, BOOLEAN timeout)
 {
     vlc_timer_t *id = val;
@@ -633,6 +711,7 @@ void vlc_timer_schedule (vlc_timer_t *id, bool absolute,
                                 interval, WT_EXECUTEDEFAULT))
         abort ();
 }
+#endif
 
 unsigned vlc_timer_getoverrun (const vlc_timer_t *id)
 {


Regards,

-- 
Pierre Ynard
"Une âme dans un corps, c'est comme un dessin sur une feuille de papier."



More information about the vlc-devel mailing list