Re: [AD] Timer patch

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Jan 29, 2008 4:52 PM, Peter Wang <novalazy@xxxxxxxxxx> wrote:
On 2008-01-29, Ryan Dickie <goalieca@xxxxxxxxxx> wrote:
> On Jan 29, 2008 3:34 PM, Ryan Dickie <goalieca@xxxxxxxxxx> wrote:
 >
> > I have made those style changes (and found some dead code as well). See
> > attachment
> >
> > -- Ryan Dickie
> >
>
> Trent tested it out on windows for me. We found a silly typo in the
> wtime.cpatch. I've fixed it. See attached

For future reference, you can use the interdiff program from patchutils
to generate relative diffs.

http://cyberelk.net/tim/software/patchutils/

Peter


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
--
https://lists.sourceforge.net/lists/listinfo/alleg-developers


I've attached another patch for the periodic timers. I've switched them to doubles as well. I did not touch the windows one. The unix timer utimernu.c is now built entirely on portable allegro functions. This code should work for windows as well provided we get windows using a more accurate time and sleep function.

I might have missed this as well in my first patch. No compiler warnings were given but timestamp is probably set using al_current_time(). I switched it to a double.

--- include/allegro5/events.h    (revision 9954)
+++ include/allegro5/events.h    (working copy)
@@ -142,7 +142,7 @@
 #define _AL_EVENT_HEADER(srctype)                    \
    ALLEGRO_EVENT_TYPE type;                          \
    srctype *source;                                  \
-   unsigned long timestamp;                          \
+   double timestamp;                          \
    signed int _refcount;            /* internal */   \
    union ALLEGRO_EVENT *_next;      /* internal */   \
    union ALLEGRO_EVENT *_next_free  /* internal */

-- Ryan Dickie
only in patch2:
unchanged:
--- src/unix/utimernu.c	(revision 9954)
+++ src/unix/utimernu.c	(working copy)
@@ -25,14 +25,8 @@
 #include "allegro5/internal/aintern_events.h"
 
 
-
-/* readability typedefs */
-typedef long msecs_t;
-typedef long usecs_t;
-
-
 /* forward declarations */
-static usecs_t timer_thread_handle_tick(usecs_t interval);
+static double timer_thread_handle_tick(double interval);
 static void timer_handle_tick(ALLEGRO_TIMER *this);
 
 
@@ -40,7 +34,7 @@
 {
    ALLEGRO_EVENT_SOURCE es;
    bool started;
-   usecs_t speed_usecs;
+   double speed_secs;
    long count;
    long counter;		/* counts down to zero=blastoff */
 };
@@ -91,27 +85,18 @@
    }
 #endif
 
-   struct timeval old_time;
-   struct timeval new_time;
-   struct timeval delay;
-   usecs_t interval = 0x8000;
+   double old_time = al_current_time();
+   double new_time;
+   double interval = 0x8000 / 1000000;
 
-   gettimeofday(&old_time, NULL);
-
    while (!_al_thread_should_stop(self)) {
-      /* Go to sleep for a short time.  `select' is more accurate than
-       * `usleep' (or even `nanosleep') on my Linux system.
-       */
-      delay.tv_sec = interval / 1000000L;
-      delay.tv_usec = interval % 1000000L;
-      select(0, NULL, NULL, NULL, &delay);
+      al_rest(interval);
 
       _al_mutex_lock(&timer_thread_mutex);
       {
          /* Calculate actual time elapsed.  */
-         gettimeofday(&new_time, NULL);
-         interval = ((new_time.tv_sec - old_time.tv_sec) * 1000000L
-                     + (new_time.tv_usec - old_time.tv_usec));
+         new_time = al_current_time();
+         double interval = new_time - old_time;
          old_time = new_time;
 
          /* Handle a tick.  */
@@ -128,24 +113,24 @@
  *  returns the duration that the timer thread should try to sleep
  *  next time.
  */
-static usecs_t timer_thread_handle_tick(usecs_t interval)
+static double timer_thread_handle_tick(double interval)
 {
-   usecs_t new_delay = 0x8000;
+   double new_delay = 0x8000 / 1000000;
    unsigned int i;
 
    for (i = 0; i < _al_vector_size(&active_timers); i++) {
       ALLEGRO_TIMER **slot = _al_vector_ref(&active_timers, i);
       ALLEGRO_TIMER *timer = *slot;
 
-      timer->counter -= interval;
+      timer->counter -= (long)(interval*1000000);
 
       while (timer->counter <= 0) {
-         timer->counter += timer->speed_usecs;
+         timer->counter += (long)(timer->speed_secs * 1000000);
          timer_handle_tick(timer);
       }
 
-      if ((timer->counter > 0) && (timer->counter < new_delay))
-         new_delay = timer->counter;
+      if ((timer->counter > 0) && (timer->counter < (long)(new_delay*1000000)))
+         new_delay = (double) timer->counter / 1000000;
    }
 
    return new_delay;
@@ -161,9 +146,9 @@
 /* al_install_timer: [primary thread]
  *  Create a new timer object.
  */
-ALLEGRO_TIMER* al_install_timer(msecs_t speed_msecs)
+ALLEGRO_TIMER* al_install_timer(double speed_secs)
 {
-   ASSERT(speed_msecs > 0);
+   ASSERT(speed_secs > 0);
    {
       ALLEGRO_TIMER *timer = _AL_MALLOC(sizeof *timer);
 
@@ -173,7 +158,7 @@
          _al_event_source_init(&timer->es);
          timer->started = false;
          timer->count = 0;
-         timer->speed_usecs = speed_msecs * 1000;
+         timer->speed_secs = speed_secs;
          timer->counter = 0;
 
          _al_register_destructor(timer, (void (*)(void *)) al_uninstall_timer);
@@ -220,7 +205,7 @@
          ALLEGRO_TIMER **slot;
 
          this->started = true;
-         this->counter = this->speed_usecs;
+         this->counter = (long)(this->speed_secs * 1000000);
 
          slot = _al_vector_alloc_back(&active_timers);
          *slot = this;
@@ -285,11 +270,11 @@
 /* al_timer_get_speed: [primary thread]
  *  Return this timer's speed.
  */
-msecs_t al_timer_get_speed(ALLEGRO_TIMER *this)
+double al_timer_get_speed(ALLEGRO_TIMER *this)
 {
    ASSERT(this);
 
-   return this->speed_usecs / 1000;
+   return this->speed_secs;
 }
 
 
@@ -297,19 +282,19 @@
 /* al_timer_set_speed: [primary thread]
  *  Change this timer's speed.
  */
-void al_timer_set_speed(ALLEGRO_TIMER *this, msecs_t new_speed_msecs)
+void al_timer_set_speed(ALLEGRO_TIMER *this, double new_speed_secs)
 {
    ASSERT(this);
-   ASSERT(new_speed_msecs > 0);
+   ASSERT(new_speed_secs > 0);
 
    _al_mutex_lock(&timer_thread_mutex);
    {
       if (this->started) {
-         this->counter -= this->speed_usecs;
-         this->counter += new_speed_msecs * 1000;
+         this->counter -= (long)(this->speed_secs * 1000000);
+         this->counter += (long)(new_speed_secs * 1000000);
       }
 
-      this->speed_usecs = new_speed_msecs * 1000;
+      this->speed_secs = new_speed_secs;
    }
    _al_mutex_unlock(&timer_thread_mutex);
 }
only in patch2:
unchanged:
--- include/allegro5/events.h	(revision 9954)
+++ include/allegro5/events.h	(working copy)
@@ -142,7 +142,7 @@
 #define _AL_EVENT_HEADER(srctype)                    \
    ALLEGRO_EVENT_TYPE type;                          \
    srctype *source;                                  \
-   unsigned long timestamp;                          \
+   double timestamp;                          \
    signed int _refcount;            /* internal */   \
    union ALLEGRO_EVENT *_next;      /* internal */   \
    union ALLEGRO_EVENT *_next_free  /* internal */
@@ -210,7 +210,7 @@
  *
  * >	ALLEGRO_EVENT_TYPE	    type;
  * >	ALLEGRO_EVENT_SOURCE *      any.source;
- * >	unsigned long	            any.timestamp;
+ * >	double		            any.timestamp;
  *
  * By examining the type field you can then access type-specific fields.  The
  * any.source field tells you which event source generated that particular
only in patch2:
unchanged:
--- include/allegro5/timer.h	(revision 9954)
+++ include/allegro5/timer.h	(working copy)
@@ -26,15 +26,17 @@
 
 
 /* Macros: conversion macros
- *  AL_SECS_TO_MSECS - seconds to milliseconds
- *  AL_BPS_TO_MSECS - beats per second to milliseconds
- *  AL_BPM_TO_MSECS - beats per minute to milliseconds
+ *  AL_USECS_TO_SECS - microseconds to seconds
+ *  AL_MSECS_TO_SECS - milliseconds to seconds
+ *  AL_BPS_TO_MSECS - beats per second to seconds
+ *  AL_BPM_TO_MSECS - beats per minute to seconds
  *
  *  These macros convert from various time units into milliseconds.
  */
-#define ALLEGRO_SECS_TO_MSECS(x)      ((long)(x) * 1000)
-#define ALLEGRO_BPS_TO_MSECS(x)       (1000 / (long)(x))
-#define ALLEGRO_BPM_TO_MSECS(x)       ((60 * 1000) / (long)(x))
+#define ALLEGRO_USECS_TO_SECS(x)      (x / 1000000)
+#define ALLEGRO_MSECS_TO_SECS(x)      (x / 1000)
+#define ALLEGRO_BPS_TO_SECS(x)        (1.0 / x)
+#define ALLEGRO_BPM_TO_SECS(x)        (60 / x)
 
 
 /* Type: ALLEGRO_TIMER
@@ -47,14 +49,14 @@
 
 /* Function: al_install_timer
  *  Install a new timer.  If successful, a pointer to a new timer object is
- *  returned, otherwise NULL is returned.  SPEED_MSECS is in milliseconds per
+ *  returned, otherwise NULL is returned.  SPEED_SECS is in seconds per
  *  "tick", and must be positive.  The new timer is initially stopped.
  * 
  *  The system driver must be installed before this function can be called.
  * 
  *  Usage note: typical granularity is on the order of milliseconds.
  */
-AL_FUNC(ALLEGRO_TIMER*, al_install_timer, (long speed_msecs));
+AL_FUNC(ALLEGRO_TIMER*, al_install_timer, (double speed_secs));
 
 
 /* Function: al_uninstall_timer
@@ -92,7 +94,7 @@
 /* Function: al_timer_get_speed
  *  Return the timer's speed.
  */
-AL_FUNC(long, al_timer_get_speed, (ALLEGRO_TIMER *timer));
+AL_FUNC(double, al_timer_get_speed, (ALLEGRO_TIMER *timer));
 
 
 /* Function: al_timer_set_speed
@@ -100,11 +102,11 @@
  *  incremented when it is started.  This can be done when the timer is
  *  started or stopped.  If the timer is currently running, it is made to
  *  look as though the speed change occured precisely at the last tick.
- *  SPEED_MSECS is in milliseconds per "tick", and must be positive.
+ *  SPEED_SECS is in seconds per "tick", and must be positive.
  * 
  *  Usage note: typical granularity is on the order of milliseconds.
  */
-AL_FUNC(void, al_timer_set_speed, (ALLEGRO_TIMER *timer, long speed_msecs));
+AL_FUNC(void, al_timer_set_speed, (ALLEGRO_TIMER *timer, double speed_secs));
 
 
 /* Function: al_timer_get_count
only in patch2:
unchanged:
--- examples/exnew_events.c	(revision 9954)
+++ examples/exnew_events.c	(working copy)
@@ -448,9 +448,9 @@
     * automatically increment a counter every n milliseconds.  They no longer
     * run user-defined callbacks.
     */
-   timer_a = al_install_timer(100);
-   timer_b = al_install_timer(1000);
-   timer_c = al_install_timer(5000);
+   timer_a = al_install_timer(0.100);
+   timer_b = al_install_timer(1.000);
+   timer_c = al_install_timer(5.000);
    if (!timer_a || !timer_b || !timer_c) {
       fatal_error("al_install_timer");
    }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/