[PD-cvs] pd/src g_array.c,1.1.1.3.2.3,1.1.1.3.2.4

Tim Blechmann timblech at users.sourceforge.net
Sun May 23 20:53:34 CEST 2004


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

Modified Files:
      Tag: devel_0_37
	g_array.c 
Log Message:
t_garray thread locks


Index: g_array.c
===================================================================
RCS file: /cvsroot/pure-data/pd/src/g_array.c,v
retrieving revision 1.1.1.3.2.3
retrieving revision 1.1.1.3.2.4
diff -C2 -d -r1.1.1.3.2.3 -r1.1.1.3.2.4
*** g_array.c	6 May 2004 08:28:12 -0000	1.1.1.3.2.3
--- g_array.c	23 May 2004 18:53:32 -0000	1.1.1.3.2.4
***************
*** 10,13 ****
--- 10,24 ----
  #include <math.h>
  
+ /* Tim Blechmann: garray thread locks: */
+ //#define GARRAY_THREAD_LOCK
+ #ifdef GARRAY_THREAD_LOCK
+ 
+ #ifdef __linux__
+ #define __USE_GNU        /* we need that for linux threads */
+ #endif
+ 
+ #include <pthread.h>
+ #endif /* GARRAY_THREAD_LOCK */
+ 
  /* see also the "plot" object in g_scalar.c which deals with graphing
  arrays which are fields in scalars.  Someday we should unify the
***************
*** 116,120 ****
      char x_usedindsp;	    /* true if some DSP routine is using this */
      char x_saveit;   	    /* true if we should save this with parent */
! 	double x_lastupdate;    /* T.Grill - clock_getlogicaltime() of last array update */
  };
  
--- 127,134 ----
      char x_usedindsp;	    /* true if some DSP routine is using this */
      char x_saveit;   	    /* true if we should save this with parent */
!     double x_lastupdate;    /* T.Grill - clock_getlogicaltime() of last array update */
! #ifdef GARRAY_THREAD_LOCK
!     pthread_mutex_t * x_mutex; /* TB: mutex */
! #endif
  };
  
***************
*** 125,133 ****
  #define x_templatesym x_array.a_templatesym
  
  
  /* T.Grill - set update time */
  static void garray_update(t_garray *x)
  {
! 	x->x_lastupdate = clock_getlogicaltime();
  }
  
--- 139,180 ----
  #define x_templatesym x_array.a_templatesym
  
+ /* TB: array thread locks */
+ #ifdef GARRAY_THREAD_LOCK
+ void garray_lock(t_garray *x)
+ {
+     //    post("locking array");
+     pthread_mutex_lock(x->x_mutex);
+ }
+ 
+ void garray_unlock(t_garray *x)
+ {
+     //    post("unlocking array");
+     pthread_mutex_unlock(x->x_mutex);
+ }
+ 
+ int garray_trylock(t_garray *x)
+ {
+     //    post("trying to lock array");
+     return pthread_mutex_trylock(x->x_mutex);
+ }
+ #else
+ void garray_lock(t_garray *x)
+ {
+ }
+ 
+ void garray_unlock(t_garray *x)
+ {
+ }
+ 
+ int garray_trylock(t_garray *x)
+ {
+     return 0;
+ }
+ #endif /* GARRAY_THREAD_LOCK */
  
  /* T.Grill - set update time */
  static void garray_update(t_garray *x)
  {
!     x->x_lastupdate = clock_getlogicaltime();
  }
  
***************
*** 189,195 ****
      x->x_usedindsp = 0;
      x->x_saveit = (saveit != 0);
      if (x2 = pd_findbyclass(gensym("#A"), garray_class))
      	pd_unbind(x2, gensym("#A"));
- 
      pd_bind(&x->x_gobj.g_pd, gensym("#A"));
      
--- 236,254 ----
      x->x_usedindsp = 0;
      x->x_saveit = (saveit != 0);
+ 
+ #ifdef GARRAY_THREAD_LOCK    /* TB: array lock */
+     x->x_mutex=getbytes(sizeof(pthread_mutex_t));
+ #ifdef __linux__
+     *(x->x_mutex)=(pthread_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+ #else
+     pthread_mutexattr_t mutex_attr; 
+     pthread_mutexattr_init (&mutex_attr);
+     pthread_mutexattr_settype(&mutex_attr,PTHREAD_MUTEX_RECURSIVE); 
+     pthread_mutex_init(x->x_mutex, &mutex_attr); 
+ #endif
+ #endif /* GARRAY_THREAD_LOCK */
+ 
      if (x2 = pd_findbyclass(gensym("#A"), garray_class))
      	pd_unbind(x2, gensym("#A"));
      pd_bind(&x->x_gobj.g_pd, gensym("#A"));
      
***************
*** 242,245 ****
--- 301,307 ----
      t_floatarg saveit, t_floatarg deleteit)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      if (deleteit != 0)
      {
***************
*** 265,272 ****
--- 327,342 ----
  	garray_redraw(x);
      }
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
  static void garray_free(t_garray *x)
  {
+ #ifdef GARRAY_THREAD_LOCK
+     garray_lock(x);/* TB: thread lock */
+     pthread_mutex_destroy(x->x_mutex);
+     freebytes(x->x_mutex,sizeof(pthread_mutex_t));
+ #endif
      t_pd *x2;
      gfxstub_deleteforkey(x);
***************
*** 402,407 ****
      glist_redrawitem(array_motion_glist, array_motion_gobj);
  
! 	/* T.Grill - don't know if the array has really changed.... should be enhanced */
! 	garray_update((t_garray *)array_motion_gobj);
  }
  
--- 472,477 ----
      glist_redrawitem(array_motion_glist, array_motion_gobj);
  
!     /* T.Grill - don't know if the array has really changed.... should be enhanced */
!     garray_update((t_garray *)array_motion_gobj);
  }
  
***************
*** 754,762 ****
--- 824,842 ----
  void garray_usedindsp(t_garray *x)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      x->x_usedindsp = 1;
+ #ifdef GARRAY_THREAD_LOCK
+     garray_unlock(x); /* TB: array lock */
+ #endif
  }
  
  void garray_redraw(t_garray *x)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
+ 
      if (glist_isvisible(x->x_glist))
      {
***************
*** 765,770 ****
      }
  
! 	/* T.Grill - last update time */
! 	garray_update(x);
  }
  
--- 845,854 ----
      }
  
!     /* T.Grill - last update time */
!     garray_update(x);
! 
! #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
!     garray_unlock(x); 
! #endif
  }
  
***************
*** 772,776 ****
  double garray_updatetime(t_garray *x)
  {
! 	return x->x_lastupdate;
  }
  
--- 856,866 ----
  double garray_updatetime(t_garray *x)
  {
! #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
!     garray_lock(x); 
! #endif
!     return x->x_lastupdate;
! #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
!     garray_unlock(x); 
! #endif
  }
  
***************
*** 781,785 ****
--- 871,881 ----
  t_template *garray_template(t_garray *x)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      t_template *template = template_findbyname(x->x_templatesym);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      if (!template)
      	bug("garray_template");
***************
*** 789,798 ****
  int garray_npoints(t_garray *x)	/* get the length */
  {
!     return (x->x_n);
  }
  
  char *garray_vec(t_garray *x) /* get the contents */
  {
!     return ((char *)(x->x_vec));
  }
  
--- 885,908 ----
  int garray_npoints(t_garray *x)	/* get the length */
  {
! #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
!     garray_lock(x); 
! #endif
!     int ret = (x->x_n);
! #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
!     garray_unlock(x); 
! #endif
!     return ret;
  }
  
  char *garray_vec(t_garray *x) /* get the contents */
  {
! #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
!     garray_lock(x); 
! #endif
!     char * ret =((char *)(x->x_vec));
! #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
!     garray_unlock(x); 
! #endif
!     return ret;
  }
  
***************
*** 802,805 ****
--- 912,918 ----
  int garray_getfloatarray(t_garray *x, int *size, t_float **vec)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      t_template *template = garray_template(x);
      int yonset, type;
***************
*** 815,820 ****
--- 928,939 ----
      	*size = garray_npoints(x);
      	*vec =  (float *)garray_vec(x);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	return (1);
      }
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      return (0);
  }
***************
*** 823,826 ****
--- 942,948 ----
  float garray_get(t_garray *x, t_symbol *s, t_int indx)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      t_template *template = garray_template(x);
      int yonset, type;
***************
*** 835,839 ****
      if (indx < 0) indx = 0;
      else if (indx >= x->x_n) indx = x->x_n - 1;
!     return (*(float *)((x->x_vec + sizeof(t_word) * indx) + yonset));
  }
  
--- 957,966 ----
      if (indx < 0) indx = 0;
      else if (indx >= x->x_n) indx = x->x_n - 1;
!     float ret = (*(float *)((x->x_vec + sizeof(t_word) * indx) + yonset));
! 
! #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
!     garray_unlock(x); 
! #endif
!     return ret;
  }
  
***************
*** 841,853 ****
--- 968,990 ----
  void garray_setsaveit(t_garray *x, int saveit)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      if (x->x_saveit && !saveit)
      	post("warning: array %s: clearing save-in-patch flag",
  	    x->x_name->s_name);
      x->x_saveit = saveit;
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
+ 
  /*------------------- Pd messages ------------------------ */
  static void garray_const(t_garray *x, t_floatarg g)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      t_template *template = garray_template(x);
      int yonset, type, i;
***************
*** 860,863 ****
--- 997,1003 ----
      	    *(float *)(((char *)x->x_vec + sizeof(t_word) * i) + yonset) = g;
      garray_redraw(x);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
***************
*** 866,869 ****
--- 1006,1012 ----
      int nsin, t_float *vsin, int sineflag)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      t_template *template = garray_template(x);
      int yonset, type, i, j;
***************
*** 895,902 ****
--- 1038,1052 ----
      }
      garray_redraw(x);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
+ 
  }
  
  static void garray_sinesum(t_garray *x, t_symbol *s, int argc, t_atom *argv)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      t_template *template = garray_template(x);
      
***************
*** 920,927 ****
--- 1070,1083 ----
      garray_dofo(x, npoints, 0, argc, svec, 1);
      t_freebytes(svec, sizeof(t_float) * argc);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
  static void garray_cosinesum(t_garray *x, t_symbol *s, int argc, t_atom *argv)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      t_template *template = garray_template(x);
      
***************
*** 945,952 ****
--- 1101,1115 ----
      garray_dofo(x, npoints, 0, argc, svec, 0);
      t_freebytes(svec, sizeof(t_float) * argc);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
+ 
  }
  
  static void garray_normalize(t_garray *x, t_float f)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      t_template *template = garray_template(x);
      int yonset, type, npoints, i;
***************
*** 981,984 ****
--- 1144,1151 ----
      }
      garray_redraw(x);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
+ 
  }
  
***************
*** 987,990 ****
--- 1154,1161 ----
  static void garray_list(t_garray *x, t_symbol *s, int argc, t_atom *argv)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
+ 
      t_template *template = garray_template(x);
      int yonset, type, i;
***************
*** 1018,1021 ****
--- 1189,1195 ----
      }
      garray_redraw(x);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
***************
*** 1024,1028 ****
--- 1198,1208 ----
      t_floatarg x2, t_floatarg y2)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      vmess(&x->x_glist->gl_pd, gensym("bounds"), "ffff", x1, y1, x2, y2);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
***************
*** 1031,1035 ****
--- 1211,1221 ----
      t_floatarg point, t_floatarg inc, t_floatarg f)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      vmess(&x->x_glist->gl_pd, gensym("xticks"), "fff", point, inc, f);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
***************
*** 1037,1062 ****
--- 1223,1277 ----
      t_floatarg point, t_floatarg inc, t_floatarg f)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      vmess(&x->x_glist->gl_pd, gensym("yticks"), "fff", point, inc, f);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
+ 
  static void garray_xlabel(t_garray *x, t_symbol *s, int argc, t_atom *argv)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      typedmess(&x->x_glist->gl_pd, s, argc, argv);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
  static void garray_ylabel(t_garray *x, t_symbol *s, int argc, t_atom *argv)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      typedmess(&x->x_glist->gl_pd, s, argc, argv);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
+ 
      /* change the name of a garray. */
  static void garray_rename(t_garray *x, t_symbol *s)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      pd_unbind(&x->x_gobj.g_pd, x->x_realname);
      pd_bind(&x->x_gobj.g_pd, x->x_realname = x->x_name = s);
      garray_redraw(x);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
  static void garray_read(t_garray *x, t_symbol *filename)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      int nelem = x->x_n, filedesc;
      FILE *fd;
***************
*** 1069,1072 ****
--- 1284,1290 ----
      {
      	error("%s: needs floating-point 'y' field", x->x_templatesym->s_name);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	return;
      }
***************
*** 1077,1080 ****
--- 1295,1301 ----
      {
      	error("%s: can't open", filename->s_name);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	return;
      }
***************
*** 1093,1096 ****
--- 1314,1320 ----
      fclose(fd);
      garray_redraw(x);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
***************
*** 1109,1112 ****
--- 1333,1340 ----
      t_symbol *endian, t_floatarg fskip)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
+ 
      int skip = fskip, filedesc;
      int i, nelem;
***************
*** 1133,1136 ****
--- 1361,1367 ----
      {
      	error("%s: not a float array", x->x_templatesym->s_name);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	return;
      }
***************
*** 1141,1144 ****
--- 1372,1378 ----
      {
      	error("%s: can't open", filename->s_name);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	return;
      }
***************
*** 1150,1153 ****
--- 1384,1390 ----
      	    error("%s: can't seek to byte %d", buf, skip);
      	    fclose(fd);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	    return;
      	}
***************
*** 1168,1175 ****
--- 1405,1419 ----
      fclose(fd);
      garray_redraw(x);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
  static void garray_write(t_garray *x, t_symbol *filename)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
+ 
      FILE *fd;
      char buf[MAXPDSTRING];
***************
*** 1181,1184 ****
--- 1425,1431 ----
      {
      	error("%s: needs floating-point 'y' field", x->x_templatesym->s_name);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	return;
      }
***************
*** 1189,1192 ****
--- 1436,1442 ----
      {
      	error("%s: can't create", buf);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	return;
      }
***************
*** 1200,1203 ****
--- 1450,1456 ----
      	}
      }
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      fclose(fd);
  }
***************
*** 1222,1225 ****
--- 1475,1482 ----
  static void garray_write16(t_garray *x, t_symbol *filename, t_symbol *format)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
+ 
      t_template *template = garray_template(x);
      int yonset, type, i;
***************
*** 1247,1250 ****
--- 1504,1510 ----
      {
      	error("%s: needs floating-point 'y' field", x->x_templatesym->s_name);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	return;
      }
***************
*** 1255,1258 ****
--- 1515,1521 ----
      {
      	error("%s: can't create", buf2);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      	return;
      }
***************
*** 1297,1300 ****
--- 1560,1566 ----
      }
  closeit:
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
      fclose(fd);
  }
***************
*** 1302,1305 ****
--- 1568,1574 ----
  void garray_resize(t_garray *x, t_floatarg f)
  {
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      int was = x->x_n, elemsize;
      t_glist *gl;
***************
*** 1314,1317 ****
--- 1583,1589 ----
      {
      	pd_error(x, "array resize failed: out of memory");
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  	return;
      }
***************
*** 1335,1344 ****
--- 1607,1625 ----
      else garray_redraw(x);
      if (x->x_usedindsp) canvas_update_dsp();
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  
  static void garray_print(t_garray *x)
  {  
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_lock(x); 
+ #endif
      post("garray %s: template %s, length %d",
      	x->x_name->s_name, x->x_templatesym->s_name, x->x_n);
+ #ifdef GARRAY_THREAD_LOCK /* TB: array lock */
+     garray_unlock(x); 
+ #endif
  }
  





More information about the Pd-cvs mailing list