[PD-cvs] pd/src d_soundfile.c,1.4.4.4,1.4.4.5

Tim Blechmann timblech at users.sourceforge.net
Fri Nov 26 11:09:49 CET 2004


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

Modified Files:
      Tag: devel_0_38
	d_soundfile.c 
Log Message:
fixed thread synchronisation problem

Index: d_soundfile.c
===================================================================
RCS file: /cvsroot/pure-data/pd/src/d_soundfile.c,v
retrieving revision 1.4.4.4
retrieving revision 1.4.4.5
diff -C2 -d -r1.4.4.4 -r1.4.4.5
*** d_soundfile.c	24 Nov 2004 14:31:21 -0000	1.4.4.4
--- d_soundfile.c	26 Nov 2004 10:09:46 -0000	1.4.4.5
***************
*** 1007,1010 ****
--- 1007,1014 ----
      int i;
  	t_sfprocess * next;
+ 
+ #ifdef DEBUG
+ 	post("soundfiler_thread ID: %d", pthread_self());
+ #endif
  	while (1)
      {
***************
*** 1137,1140 ****
--- 1141,1145 ----
      TB: adapted for threaded use
      */
+ static t_int soundfiler_t_read_callback(t_int * w);
  
  static void soundfiler_t_read(t_soundfiler *x, t_symbol *s,
***************
*** 1143,1152 ****
      int headersize = -1, channels = 0, bytespersamp = 0, bigendian = 0,
  		resize = 0, i, j;
!     long skipframes = 0, 
! 		nframes = 0, 
  		finalsize = 0,                     /* new size */
  		itemsleft,
!     	maxsize = DEFMAXSIZE, 
! 		itemsread = 0, 
  		bytelimit  = 0x7fffffff;
      int fd = -1;
--- 1148,1157 ----
      int headersize = -1, channels = 0, bytespersamp = 0, bigendian = 0,
  		resize = 0, i, j;
!     long skipframes = 0,
! 		nframes = 0,
  		finalsize = 0,                     /* new size */
  		itemsleft,
!     	maxsize = DEFMAXSIZE,
! 		itemsread = 0,
  		bytelimit  = 0x7fffffff;
      int fd = -1;
***************
*** 1159,1163 ****
      int bufframes, nitems;
      FILE *fp;
! 
      while (argc > 0 && argv->a_type == A_SYMBOL &&
  		   *argv->a_w.w_symbol->s_name == '-')
--- 1164,1169 ----
      int bufframes, nitems;
      FILE *fp;
! 	pthread_cond_t resume_after_callback = PTHREAD_COND_INITIALIZER;
! 	pthread_mutex_t resume_after_callback_mutex = PTHREAD_MUTEX_INITIALIZER; /* dummy */
      while (argc > 0 && argv->a_type == A_SYMBOL &&
  		   *argv->a_w.w_symbol->s_name == '-')
***************
*** 1185,1191 ****
  				argv[2].a_type != A_FLOAT ||
  				((channels = argv[2].a_w.w_float) < 1) ||
! 				(channels > MAXSFCHANS) || 
  				argv[3].a_type != A_FLOAT ||
! 				((bytespersamp = argv[3].a_w.w_float) < 2) || 
  				(bytespersamp > 4) ||
  				argv[4].a_type != A_SYMBOL ||
--- 1191,1197 ----
  				argv[2].a_type != A_FLOAT ||
  				((channels = argv[2].a_w.w_float) < 1) ||
! 				(channels > MAXSFCHANS) ||
  				argv[3].a_type != A_FLOAT ||
! 				((bytespersamp = argv[3].a_w.w_float) < 2) ||
  				(bytespersamp > 4) ||
  				argv[4].a_type != A_SYMBOL ||
***************
*** 1305,1309 ****
  				for (j=0; j!=i;++j)
  					/* if the resizing fails, we'll have to free all arrays again */
! 					freealignedbytes (nvecs[i],finalsize * sizeof(t_float)); 
  				goto done;
  			}
--- 1311,1315 ----
  				for (j=0; j!=i;++j)
  					/* if the resizing fails, we'll have to free all arrays again */
! 					freealignedbytes (nvecs[i],finalsize * sizeof(t_float));
  				goto done;
  			}
***************
*** 1324,1328 ****
  				for (j=0; j!=i;++j)
  					/* if the resizing fails, we'll have to free all arrays again */
! 					freealignedbytes (nvecs[i],vecsize[i] * sizeof(t_float)); 
  				goto done;
  			}
--- 1330,1334 ----
  				for (j=0; j!=i;++j)
  					/* if the resizing fails, we'll have to free all arrays again */
! 					freealignedbytes (nvecs[i],vecsize[i] * sizeof(t_float));
  				goto done;
  			}
***************
*** 1332,1335 ****
--- 1338,1345 ----
  		}
  
+ #ifdef SFDEBUG
+ 	post("transfer soundfile");
+ #endif
+ 
  	for (itemsread = 0; itemsread < finalsize; )
  	{
***************
*** 1343,1346 ****
--- 1353,1359 ----
  	}
  
+ #ifdef SFDEBUG
+ 	post("zeroing remaining elements");
+ #endif
  	/* zero out remaining elements of vectors */
  	for (i = 0; i < argc; i++)
***************
*** 1350,1389 ****
  	}
  
! 	/* lock the system and reset the garrays */
! 	sys_lock();
  	for (i = 0; i < argc; i++)
  	{
! #ifdef GARRAY_THREAD_LOCK 
! 		garray_lock(garrays[i]); 
! #endif
! 		garrays[i]->x_array.a_vec = (char *) nvecs[i];
! 		garrays[i]->x_array.a_n = finalsize;
! #ifdef GARRAY_THREAD_LOCK 
! 		garray_unlock(garrays[i]); 
! #endif
! 		if (garrays[i]->x_usedindsp) canvas_update_dsp();
  	}
- 	sys_unlock();
  
! 	/* do all graphics updates */
! 	for (i = 0; i < argc; i++)
! 	{
! 		t_glist *gl;
! 		int n = finalsize;
! 		/* if this is the only array in the graph,
! 		   reset the graph's coordinates */
! 		gl = garrays[i]->x_glist;
! 		if (gl->gl_list == &garrays[i]->x_gobj && !garrays[i]->x_gobj.g_next)
! 		{
! 			vmess(&gl->gl_pd, gensym("bounds"), "ffff",
! 				  0., gl->gl_y1, (double)(n > 1 ? n-1 : 1), gl->gl_y2);
! 			/* close any dialogs that might have the wrong info now... */
! 			gfxstub_deleteforkey(gl);
! 		}
! 		else garray_redraw(garrays[i]);
! 	}	
  
   end:
  	/* free the old arrays */
  	for (i = 0; i < argc; i++)
  		freealignedbytes(vecs[i], vecsize[i] * sizeof(t_float));
--- 1363,1415 ----
  	}
  
! 	/* set idle callback to switch pointers */
! #ifdef SFDEBUG
! 	post("locked");
! #endif
  	for (i = 0; i < argc; i++)
  	{
! 		t_int w[4];
! 		
! 		post("nvec %p",(char*)nvecs[i]);
! 		post("%d",finalsize);
! 
! 		w[0] = (t_int)(garrays[i]);
! 		w[1] = (t_int)nvecs[i];
! 		w[2] = (t_int)finalsize;
! 		w[3] = (t_int)(&resume_after_callback);
! 
! 		set_callback(&soundfiler_t_read_callback, w, 4);
! 		pthread_cond_wait(&resume_after_callback, &resume_after_callback_mutex);
! 
  	}
  
! #ifdef SFDEBUG
! 	post("unlocked, doing graphics updates");
! #endif
! 
! 	/* todo: do all graphics updates */
! 
! 	/* run this in the main thread via callback */
! /* 	for (i = 0; i < argc; i++) */
! /* 	{ */
! /* 		t_glist *gl; */
! /* 		int n = finalsize; */
! /* 		/\* if this is the only array in the graph, */
! /* 		   reset the graph's coordinates *\/ */
! /* 		gl = garrays[i]->x_glist; */
! /* 		if (gl->gl_list == &garrays[i]->x_gobj && !garrays[i]->x_gobj.g_next) */
! /* 		{ */
! /* 			vmess(&gl->gl_pd, gensym("bounds"), "ffff", */
! /* 				  0., gl->gl_y1, (double)(n > 1 ? n-1 : 1), gl->gl_y2); */
! /* 			/\* close any dialogs that might have the wrong info now... *\/ */
! /* 			gfxstub_deleteforkey(gl); */
! /* 		} */
! /* 		else garray_redraw(garrays[i]); */
! /* 	} */
  
   end:
  	/* free the old arrays */
+ 
+ 	post("freeing arrays");
  	for (i = 0; i < argc; i++)
  		freealignedbytes(vecs[i], vecsize[i] * sizeof(t_float));
***************
*** 1402,1408 ****
  	mlockall(MCL_FUTURE);
  #endif
!     sys_lock();
!     outlet_float(x->x_obj.ob_outlet, (float)itemsread); 
!     sys_unlock();
  }
  
--- 1428,1475 ----
  	mlockall(MCL_FUTURE);
  #endif
! 	post("bang");
! 	
! 	/* todo: do this in the idle callback */
! /*     sys_lock(); */
! /* 	outlet_float(x->x_obj.ob_outlet, (float)itemsread);  */
! /*     sys_unlock(); */
! }
! 
! /* idle callback for threadsafe synchronisation */
! static t_int soundfiler_t_read_callback(t_int * w)
! {
! 	t_garray* garray = (t_garray*)w[0];
! 	t_int nvec = w[1];
! 	t_int finalsize = w[2];
! 	pthread_cond_t * conditional = (pthread_cond_t*) w[3];
! 
! 	post("w: %p",w);
! 	post("%p",garray);
! 	post("nvec %p",(char*)nvec);
! 	post("%d",finalsize);
! 
! #ifdef GARRAY_THREAD_LOCK
! 
! #ifdef SFDEBUG
! 	post("lock array %p", garray);
! #endif
! 
! 	garray_lock(garray);
! #endif
! 	garray->x_array.a_vec = (char *) nvec;
! 	garray->x_array.a_n = finalsize;
! 	if (garray->x_usedindsp) canvas_update_dsp();
! 
! #ifdef GARRAY_THREAD_LOCK
! 
! #ifdef SFDEBUG
! 	post("unlock array %p", garray);
! #endif
! 
! 	garray_unlock(garray);
! #endif
! 	pthread_cond_broadcast(conditional);
! 	
! 	return 0;
  }
  





More information about the Pd-cvs mailing list