[PD-cvs] pd/src m_sched.c,1.5.4.35.2.21.2.3,1.5.4.35.2.21.2.4

Mathieu Bouchard matju at users.sourceforge.net
Tue Jan 9 18:14:11 CET 2007


Update of /cvsroot/pure-data/pd/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6508

Modified Files:
      Tag: desiredata
	m_sched.c 
Log Message:
freebytes,resizebytes -> free,realloc


Index: m_sched.c
===================================================================
RCS file: /cvsroot/pure-data/pd/src/m_sched.c,v
retrieving revision 1.5.4.35.2.21.2.3
retrieving revision 1.5.4.35.2.21.2.4
diff -C2 -d -r1.5.4.35.2.21.2.3 -r1.5.4.35.2.21.2.4
*** m_sched.c	20 Dec 2006 08:28:13 -0000	1.5.4.35.2.21.2.3
--- m_sched.c	9 Jan 2007 17:14:09 -0000	1.5.4.35.2.21.2.4
***************
*** 7,10 ****
--- 7,11 ----
  #include "desire.h"
  #include "m_fifo.h"
+ #include <stdlib.h>
  
  /* for timeval */
***************
*** 21,29 ****
  #include "assert.h"
  
!     /* LATER consider making this variable.  It's now the LCM of all sample
!     rates we expect to see: 32000, 44100, 48000, 88200, 96000. */
  #define TIMEUNITPERSEC (32.*441000.)
  
- 
  /* T.Grill - enable PD global thread locking - sys_lock, sys_unlock, sys_trylock functions */
  #define THREAD_LOCKING  
--- 22,29 ----
  #include "assert.h"
  
! /* LATER consider making this variable.  It's now the LCM of all sample
!    rates we expect to see: 32000, 44100, 48000, 88200, 96000. */
  #define TIMEUNITPERSEC (32.*441000.)
  
  /* T.Grill - enable PD global thread locking - sys_lock, sys_unlock, sys_trylock functions */
  #define THREAD_LOCKING  
***************
*** 38,47 ****
  int sys_keepsched = 1;          /* if 0: change scheduler mode  */
  int sys_callbackscheduler = 0;  /* if 1: change scheduler to callback based dsp */
- 
- 
  static void run_idle_callbacks(int microsec);
  t_fifo * callback_fifo = NULL;
- 
- 
  /* tb: }*/
  
--- 38,43 ----
***************
*** 55,60 ****
  #endif
  
! struct _clock
! {
      double c_settime;
      void *c_owner;
--- 51,55 ----
  #endif
  
! struct _clock {
      double c_settime;
      void *c_owner;
***************
*** 64,85 ****
  
  t_clock *clock_setlist;
! t_clock *clock_new(void *owner, t_method fn)
! {
!     t_clock *x = (t_clock *)getbytes(sizeof *x);
      x->c_settime = -1;
      x->c_owner = owner;
      x->c_fn = (t_clockmethod)fn;
      x->c_next = 0;
!     return (x);
  }
  
! 
! void clock_unset(t_clock *x)
! {
!     if (x->c_settime >= 0)
!     {
          if (x == clock_setlist) clock_setlist = x->c_next;
!         else
!         {
              t_clock *x2 = clock_setlist;
              while (x2->c_next != x) x2 = x2->c_next;
--- 59,75 ----
  
  t_clock *clock_setlist;
! t_clock *clock_new(void *owner, t_method fn) {
!     t_clock *x = (t_clock *)malloc(sizeof(*x));
      x->c_settime = -1;
      x->c_owner = owner;
      x->c_fn = (t_clockmethod)fn;
      x->c_next = 0;
!     return x;
  }
  
! void clock_unset(t_clock *x) {
!     if (x->c_settime >= 0) {
          if (x == clock_setlist) clock_setlist = x->c_next;
!         else {
              t_clock *x2 = clock_setlist;
              while (x2->c_next != x) x2 = x2->c_next;
***************
*** 91,107 ****
  
  /* set the clock to call back at an absolute system time */
! void clock_set(t_clock *x, double setticks)
! {
      if (setticks < sys_time) setticks = sys_time;
      clock_unset(x);
      x->c_settime = setticks;
!     if (clock_setlist && clock_setlist->c_settime <= setticks)
!     {
          t_clock *cbefore, *cafter;
          for (cbefore = clock_setlist, cafter = clock_setlist->c_next;
              cbefore; cbefore = cafter, cafter = cbefore->c_next)
          {
!             if (!cafter || cafter->c_settime > setticks)
!             {
                  cbefore->c_next = x;
                  x->c_next = cafter;
--- 81,94 ----
  
  /* set the clock to call back at an absolute system time */
! void clock_set(t_clock *x, double setticks) {
      if (setticks < sys_time) setticks = sys_time;
      clock_unset(x);
      x->c_settime = setticks;
!     if (clock_setlist && clock_setlist->c_settime <= setticks) {
          t_clock *cbefore, *cafter;
          for (cbefore = clock_setlist, cafter = clock_setlist->c_next;
              cbefore; cbefore = cafter, cafter = cbefore->c_next)
          {
!             if (!cafter || cafter->c_settime > setticks) {
                  cbefore->c_next = x;
                  x->c_next = cafter;
***************
*** 109,150 ****
              }
          }
!     }
!     else x->c_next = clock_setlist, clock_setlist = x;
  }
  
!     /* set the clock to call back after a delay in msec */
! void clock_delay(t_clock *x, double delaytime)
! {
      clock_set(x, sys_time + sys_time_per_msec * delaytime);
  }
  
! 
!     /* get current logical time.  We don't specify what units this is in;
!     use clock_gettimesince() to measure intervals from time of this call. 
!     This was previously, incorrectly named "clock_getsystime"; the old
!     name is aliased to the new one in m_pd.h. */
! double clock_getlogicaltime( void)
! {
!     return (sys_time);
  }
-     /* OBSOLETE NAME */
- double clock_getsystime( void) { return (sys_time); }
  
!     /* elapsed time in milliseconds since the given system time */
! double clock_gettimesince(double prevsystime)
! {
!     return ((sys_time - prevsystime)/sys_time_per_msec);
  }
  
!     /* what value the system clock will have after a delay */
! double clock_getsystimeafter(double delaytime)
! {
!     return (sys_time + sys_time_per_msec * delaytime);
  }
  
! void clock_free(t_clock *x)
! {
      clock_unset(x);
!     freebytes(x, sizeof *x);
  }
  
--- 96,131 ----
              }
          }
!     } else x->c_next = clock_setlist, clock_setlist = x;
  }
  
! /* set the clock to call back after a delay in msec */
! void clock_delay(t_clock *x, double delaytime) {
      clock_set(x, sys_time + sys_time_per_msec * delaytime);
  }
  
! /* get current logical time.  We don't specify what units this is in;
!    use clock_gettimesince() to measure intervals from time of this call. 
!    This was previously, incorrectly named "clock_getsystime"; the old
!    name is aliased to the new one in m_pd.h. */
! double clock_getlogicaltime( void) {
!     return sys_time;
  }
  
! /* OBSOLETE NAME */
! double clock_getsystime(void) {return sys_time;}
! 
! /* elapsed time in milliseconds since the given system time */
! double clock_gettimesince(double prevsystime) {
!     return (sys_time - prevsystime)/sys_time_per_msec;
  }
  
! /* what value the system clock will have after a delay */
! double clock_getsystimeafter(double delaytime) {
!     return sys_time + sys_time_per_msec * delaytime;
  }
  
! void clock_free(t_clock *x) {
      clock_unset(x);
!     free(x);
  }
  
***************
*** 159,193 ****
  static int sched_diddsp, sched_didpoll, sched_didnothing;
  
! void sys_clearhist( void)
! {
!     unsigned int i, j;
!     for (i = 0; i < NHIST; i++)
!         for (j = 0; j < NBIN; j++) sys_histogram[i][j] = 0;
      sys_histtime = sys_getrealtime();
      sched_diddsp = sched_didpoll = sched_didnothing = 0;
  }
  
! void sys_printhist( void)
! {
!     unsigned int i, j;
!     for (i = 0; i < NHIST; i++)
!     {
          int doit = 0;
!         for (j = 0; j < NBIN; j++) if (sys_histogram[i][j]) doit = 1;
!         if (doit)
!         {
              post("%2d %8d %8d %8d %8d %8d %8d %8d %8d", i,
!                 sys_histogram[i][0],
!                 sys_histogram[i][1],
!                 sys_histogram[i][2],
!                 sys_histogram[i][3],
!                 sys_histogram[i][4],
!                 sys_histogram[i][5],
!                 sys_histogram[i][6],
!                 sys_histogram[i][7]);
          }
      }
!     post("dsp %d, pollgui %d, nothing %d",
!         sched_diddsp, sched_didpoll, sched_didnothing);
  }
  
--- 140,162 ----
  static int sched_diddsp, sched_didpoll, sched_didnothing;
  
! void sys_clearhist(void) {
!     unsigned i,j;
!     for (i=0; i<NHIST; i++) for (j=0; j<NBIN; j++) sys_histogram[i][j] = 0;
      sys_histtime = sys_getrealtime();
      sched_diddsp = sched_didpoll = sched_didnothing = 0;
  }
  
! void sys_printhist(void) {
!     unsigned i,j;
!     for (i=0; i<NHIST; i++) {
          int doit = 0;
!         for (j=0; j<NBIN; j++) if (sys_histogram[i][j]) doit = 1;
!         if (doit) {
              post("%2d %8d %8d %8d %8d %8d %8d %8d %8d", i,
!                 sys_histogram[i][0], sys_histogram[i][1], sys_histogram[i][2], sys_histogram[i][3],
!                 sys_histogram[i][4], sys_histogram[i][5], sys_histogram[i][6], sys_histogram[i][7]);
          }
      }
!     post("dsp %d, pollgui %d, nothing %d", sched_diddsp, sched_didpoll, sched_didnothing);
  }
  
***************
*** 669,767 ****
  /* linked list of callbacks 
   * callback will be freed after returning 0 */
! typedef struct _sched_callback
! {
!     struct _sched_callback* next; /* next callback in ringbuffer / in fifo */
!     t_int (*function) (t_int* argv);
!     t_int* argv;
      t_int argc;
  } t_sched_callback;
  
! 
! void sys_callback(t_int (*callback) (t_int* argv), t_int* argv, t_int argc)
! {
!     t_sched_callback* noo = (t_sched_callback*) getbytes
!         (sizeof(t_sched_callback));
! 
      noo->function = callback;
!     if (argv && argc)
!     {
          noo->argv = (t_int*) copybytes (argv, argc * sizeof (t_int));
          noo->argc = argc;
!     }
!     else
!     {
          noo->argc = 0;
!         noo->argv = NULL;
      }
!     noo->next = NULL;
! 
!     if (callback_fifo == NULL)
!         callback_fifo = fifo_init();
!     
      fifo_put(callback_fifo, noo);
  }
  
! 
! void sys_init_idle_callbacks(void)
! {
      callback_fifo = fifo_init(); /* tb: initialize fifo for idle callbacks */
! }   
  
  static t_sched_callback *ringbuffer_head = NULL;
  
! void run_all_idle_callbacks(void)
! {
      t_sched_callback * new_callback;
      /* append idle callback to ringbuffer */
!     while (new_callback = (t_sched_callback*) fifo_get(callback_fifo))
!     {
!         t_sched_callback * next;
!         
!         /* set the next field to NULL ... it might be set in the fifo */
!         new_callback->next = NULL;
!         if (ringbuffer_head == NULL)
!         {
              ringbuffer_head = new_callback;
!         }
!         else
!         {
              next = ringbuffer_head;
!             while (next->next != 0)
!                 next = next->next;
              next->next = new_callback;
          }
      }
! 
!     if (ringbuffer_head != NULL)
!     {
!         t_sched_callback * idle_callback = ringbuffer_head;
!         t_sched_callback * last = NULL;
!         t_sched_callback * next;
! 
!         do
!         {
              int status;
              status = (idle_callback->function)(idle_callback->argv);
!             
!             switch (status)
!             {
                  /* callbacks returning 0 will be deleted */
              case 0:
                  next = idle_callback->next;
!                 if (idle_callback->argv)
!                     freebytes (idle_callback->argv, idle_callback->argc*sizeof (t_int));
!                 freebytes ((void*)idle_callback, sizeof(t_sched_callback));
!                 
!                 if (last == NULL)
!                     ringbuffer_head = next;
!                 else
!                     last->next = next;
!                     
                  idle_callback = next;
-                 
                  /* callbacks returning 1 will be run again */
              case 1:
                  break;
- 
                  /* callbacks returning 2 will be run during the next idle callback */
              case 2:
--- 638,702 ----
  /* linked list of callbacks 
   * callback will be freed after returning 0 */
! typedef struct _sched_callback {
!     struct _sched_callback *next; /* next callback in ringbuffer / in fifo */
!     t_int (*function) (t_int *argv);
!     t_int *argv;
      t_int argc;
  } t_sched_callback;
  
! void sys_callback(t_int (*callback) (t_int* argv), t_int* argv, t_int argc) {
!     t_sched_callback* noo = (t_sched_callback *)malloc(sizeof(t_sched_callback));
      noo->function = callback;
!     if (argv && argc) {
          noo->argv = (t_int*) copybytes (argv, argc * sizeof (t_int));
          noo->argc = argc;
!     } else {
          noo->argc = 0;
!         noo->argv = 0;
      }
!     noo->next = 0;
!     if (!callback_fifo) callback_fifo = fifo_init();
      fifo_put(callback_fifo, noo);
  }
  
! void sys_init_idle_callbacks(void) {
      callback_fifo = fifo_init(); /* tb: initialize fifo for idle callbacks */
! }
  
  static t_sched_callback *ringbuffer_head = NULL;
  
! void run_all_idle_callbacks(void) {
      t_sched_callback * new_callback;
      /* append idle callback to ringbuffer */
!     while (new_callback = (t_sched_callback*) fifo_get(callback_fifo)) {
!         t_sched_callback *next;
!         /* set the next field to 0 ... it might be set in the fifo */
!         new_callback->next = 0;
!         if (!ringbuffer_head) {
              ringbuffer_head = new_callback;
!         } else {
              next = ringbuffer_head;
!             while (next->next) next = next->next;
              next->next = new_callback;
          }
      }
!     if (ringbuffer_head) {
!         t_sched_callback *idle_callback = ringbuffer_head;
!         t_sched_callback *last = 0;
!         t_sched_callback *next;
!         do {
              int status;
              status = (idle_callback->function)(idle_callback->argv);
!             switch (status) {
                  /* callbacks returning 0 will be deleted */
              case 0:
                  next = idle_callback->next;
!                 if (idle_callback->argv) free(idle_callback->argv);
!                 free((void*)idle_callback);
!                 if (!last) ringbuffer_head = next; else last->next = next;
                  idle_callback = next;
                  /* callbacks returning 1 will be run again */
              case 1:
                  break;
                  /* callbacks returning 2 will be run during the next idle callback */
              case 2:
***************
*** 769,799 ****
                  idle_callback = idle_callback->next;
              }
! 
!         }
!         while ((idle_callback != NULL));
      }
  }
  
! static void run_idle_callbacks(int microsec)
! {
!     t_sched_callback * new_callback;
      double stop;
- 
      stop = sys_getrealtime()*1.e6 + (double)microsec;
- 
      /* append idle callback to ringbuffer */
!     while (new_callback = (t_sched_callback*) fifo_get(callback_fifo))
!     {
!         t_sched_callback * next;
!         
          /* set the next field to NULL ... it might be set in the fifo */
!         new_callback->next = NULL;
!         if (ringbuffer_head == NULL)
!         {
              ringbuffer_head = new_callback;
!         }
!         else
!         {
!             next = ringbuffer_head;
              while (next->next != 0)
                  next = next->next;
--- 704,723 ----
                  idle_callback = idle_callback->next;
              }
!         } while (idle_callback);
      }
  }
  
! static void run_idle_callbacks(int microsec) {
!     t_sched_callback *new_callback;
      double stop;
      stop = sys_getrealtime()*1.e6 + (double)microsec;
      /* append idle callback to ringbuffer */
!     while (new_callback = (t_sched_callback*) fifo_get(callback_fifo)) {
          /* set the next field to NULL ... it might be set in the fifo */
!         new_callback->next = 0;
!         if (!ringbuffer_head) {
              ringbuffer_head = new_callback;
!         } else {
! 	    t_sched_callback *next = ringbuffer_head;
              while (next->next != 0)
                  next = next->next;
***************
*** 801,839 ****
          }
      }
! 
!     if (ringbuffer_head != NULL)
!     {
          double remain = stop - sys_getrealtime() * 1.e6;
!         t_sched_callback * idle_callback = ringbuffer_head;
!         t_sched_callback * last = NULL;
!         t_sched_callback * next;
! 
!         do
!         {
              int status;
  //            sys_lock();
              status = (idle_callback->function)(idle_callback->argv);
  //            sys_unlock();
!             
!             switch (status)
!             {
                  /* callbacks returning 0 will be deleted */
              case 0:
                  next = idle_callback->next;
!                 if (idle_callback->argc)
!                     freebytes (idle_callback->argv, idle_callback->argc*sizeof (t_int));
!                 freebytes ((void*)idle_callback, sizeof(t_sched_callback));
!                 
!                 if (last == NULL)
!                     ringbuffer_head = next;
!                 else
!                     last->next = next;
!                     
                  idle_callback = next;
-                 
                  /* callbacks returning 1 will be run again */
              case 1:
                  break;
- 
                  /* callbacks returning 2 will be run during the next idle callback */
              case 2:
--- 725,749 ----
          }
      }
!     if (ringbuffer_head) {
          double remain = stop - sys_getrealtime() * 1.e6;
!         t_sched_callback *idle_callback = ringbuffer_head;
!         t_sched_callback *last = 0;
!         t_sched_callback *next;
!         do {
              int status;
  //            sys_lock();
              status = (idle_callback->function)(idle_callback->argv);
  //            sys_unlock();
!             switch (status) {
                  /* callbacks returning 0 will be deleted */
              case 0:
                  next = idle_callback->next;
!                 if (idle_callback->argc) free(idle_callback->argv);
!                 free((void*)idle_callback);
!                 if (!last) ringbuffer_head = next; else last->next = next;
                  idle_callback = next;
                  /* callbacks returning 1 will be run again */
              case 1:
                  break;
                  /* callbacks returning 2 will be run during the next idle callback */
              case 2:
***************
*** 841,866 ****
                  idle_callback = idle_callback->next;
              }
- 
              remain = stop-sys_getrealtime()*1.e6;
          }
          while ((idle_callback != NULL) && (remain > 0));
- 
          /* sleep for the rest of the time */
          if(remain > 0) {
- 			sys_unlock();
-             sys_microsleep(remain);
- 			sys_lock();
- 		}
-     }
-     else {
  		sys_unlock();
!         sys_microsleep(microsec);
  		sys_lock();
  	}
  }
  /* } tb */
  
! void sys_setscheduler(int scheduler)
! {
      sys_keepsched = 0;
      sys_callbackscheduler = scheduler;
--- 751,772 ----
                  idle_callback = idle_callback->next;
              }
              remain = stop-sys_getrealtime()*1.e6;
          }
          while ((idle_callback != NULL) && (remain > 0));
          /* sleep for the rest of the time */
          if(remain > 0) {
  		sys_unlock();
! 		sys_microsleep(remain);
  		sys_lock();
  	}
+     } else {
+ 	sys_unlock();
+         sys_microsleep(microsec);
+ 	sys_lock();
+     }
  }
  /* } tb */
  
! void sys_setscheduler(int scheduler) {
      sys_keepsched = 0;
      sys_callbackscheduler = scheduler;
***************
*** 868,906 ****
  }
  
! int sys_getscheduler(void)
! {
      return sys_callbackscheduler;
  }
  
! static t_int sys_xrun_notification_callback(t_int * dummy)
! {
!     t_symbol * pd = gensym("pd");
!     t_symbol * xrun = gensym("xrun");
! 
      typedmess(pd->s_thing, xrun, 0, 0);
- 
      return 0;
  }
  
! 
! void sys_xrun_notification(void)
! {
      sys_callback(sys_xrun_notification_callback, 0, 0);
  }
  
! 
! static t_int sys_lock_timeout_notification_callback(t_int * dummy)
! {
!     t_symbol * pd = gensym("pd");
!     t_symbol * timeout = gensym("sys_lock_timeout");
!     
      typedmess(pd->s_thing, timeout, 0, 0);
- 
      return 0;
  }
  
! 
! void sys_lock_timeout_notification(void)
! {
      sys_callback(sys_lock_timeout_notification_callback, 0, 0);
  }
--- 774,800 ----
  }
  
! int sys_getscheduler(void) {
      return sys_callbackscheduler;
  }
  
! static t_int sys_xrun_notification_callback(t_int *dummy) {
!     t_symbol *pd = gensym("pd");
!     t_symbol *xrun = gensym("xrun");
      typedmess(pd->s_thing, xrun, 0, 0);
      return 0;
  }
  
! void sys_xrun_notification(void) {
      sys_callback(sys_xrun_notification_callback, 0, 0);
  }
  
! static t_int sys_lock_timeout_notification_callback(t_int *dummy) {
!     t_symbol *pd = gensym("pd");
!     t_symbol *timeout = gensym("sys_lock_timeout");
      typedmess(pd->s_thing, timeout, 0, 0);
      return 0;
  }
  
! void sys_lock_timeout_notification(void) {
      sys_callback(sys_lock_timeout_notification_callback, 0, 0);
  }





More information about the Pd-cvs mailing list