[PD-cvs] pd/src d_soundfile.c,1.1.1.2.2.26,1.1.1.2.2.27

Tim Blechmann timblech at users.sourceforge.net
Fri Oct 22 22:40:07 CEST 2004


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

Modified Files:
      Tag: devel_0_37
	d_soundfile.c 
Log Message:
cleanup of threaded soundfiler

Index: d_soundfile.c
===================================================================
RCS file: /cvsroot/pure-data/pd/src/d_soundfile.c,v
retrieving revision 1.1.1.2.2.26
retrieving revision 1.1.1.2.2.27
diff -C2 -d -r1.1.1.2.2.26 -r1.1.1.2.2.27
*** d_soundfile.c	20 Oct 2004 21:28:16 -0000	1.1.1.2.2.26
--- d_soundfile.c	22 Oct 2004 20:40:04 -0000	1.1.1.2.2.27
***************
*** 945,955 ****
  /* we fill the queue */
  void soundfiler_queue_add(void (* process) (t_soundfiler *,t_symbol *,
! 					   int,t_atom *), void * x, 
! 			 int argc, t_atom * argv)
  {
-     t_sfprocess * last_entry;
-     /* preparing argument vector */
-     t_atom * largv =  copybytes (argv, argc * sizeof(t_atom));
-     int i;
      
  #ifdef SFDEBUG
--- 945,951 ----
  /* we fill the queue */
  void soundfiler_queue_add(void (* process) (t_soundfiler *,t_symbol *,
! 											int,t_atom *), void * x, 
! 						  int argc, t_atom * argv)
  {
      
  #ifdef SFDEBUG
***************
*** 957,1023 ****
  #endif
  
-     // make sure that the argument vector doesn't get lost 
-     for (i = 0; i != argc; ++i)
-     {
- 	if ( (largv+i)->a_type == A_SYMBOL)
- 	{
- 	    /* we'll free these after they are processed */
- 	    t_symbol * sym1 = getbytes(sizeof(t_symbol));
- 	    sym1->s_name = copybytes((largv+i)->a_w.w_symbol->s_name,
- 				      strlen((largv+i)->a_w.w_symbol->s_name)
- 				      * (sizeof(char)+1) );
- 	    sym1->s_thing = (largv+i)->a_w.w_symbol->s_thing;
- 	    sym1->s_next = (largv+i)->a_w.w_symbol->s_next;
- 	    (largv+i)->a_w.w_symbol=sym1;
- 	}
-     }
-     
      /* preparing entry */
! 	last_entry = (t_sfprocess*)getbytes(sizeof(t_sfprocess));
  
  	pthread_mutex_init(&(last_entry->mutex), NULL);
      pthread_mutex_lock(&(last_entry->mutex));
!     last_entry->process=process;
!     last_entry->x=x;
!     last_entry->argc=argc;
!     last_entry->argv=largv;
!     last_entry->next=NULL; 
      pthread_mutex_unlock(&(last_entry->mutex));
  
!     /* inform the queue and the last element */ 
      pthread_mutex_lock(&(soundfiler_queue->mutex));
  
      if (soundfiler_queue->begin==NULL)
      {
! 	soundfiler_queue->begin=last_entry;
! 	soundfiler_queue->end=last_entry;
      }
      else 
      {
! 	pthread_mutex_lock(&(soundfiler_queue->end->mutex));
! 	soundfiler_queue->end->next=last_entry;
! 	pthread_mutex_unlock(&(soundfiler_queue->end->mutex));
! 	soundfiler_queue->end=last_entry;
      }
- #ifdef SFDEBUG
-     post("\t\tnew begin: %x",soundfiler_queue->begin);
-     post("\t\tnew end: %x",soundfiler_queue->end);
- #endif
  
      if ( soundfiler_queue->begin == soundfiler_queue->end )
      {
  #ifdef DEBUG
! 	post("signaling");
  #endif
! 	pthread_mutex_unlock(&(soundfiler_queue->mutex));
! 	/* and signal the helper thread */
! 	pthread_cond_signal(&(soundfiler_queue->cond));
      }
      else
      {
  #ifdef DEBUG
! 	post("not signaling");
  #endif
! 	pthread_mutex_unlock(&(soundfiler_queue->mutex));
      }
      return;
--- 953,999 ----
  #endif
  
      /* preparing entry */
!     t_sfprocess * last_entry = (t_sfprocess*)getbytes(sizeof(t_sfprocess));
  
  	pthread_mutex_init(&(last_entry->mutex), NULL);
      pthread_mutex_lock(&(last_entry->mutex));
!     last_entry->process = process;
!     last_entry->x = x;
!     last_entry->argc = argc;
!     last_entry->argv = copybytes (argv, argc * sizeof(t_atom));
!     last_entry->next = NULL; 
      pthread_mutex_unlock(&(last_entry->mutex));
  
!     /* add new entry to queue */ 
      pthread_mutex_lock(&(soundfiler_queue->mutex));
  
      if (soundfiler_queue->begin==NULL)
      {
! 		soundfiler_queue->begin=last_entry;
! 		soundfiler_queue->end=last_entry;
      }
      else 
      {
! 		pthread_mutex_lock(&(soundfiler_queue->end->mutex));
! 		soundfiler_queue->end->next=last_entry;
! 		pthread_mutex_unlock(&(soundfiler_queue->end->mutex));
! 		soundfiler_queue->end=last_entry;
      }
  
      if ( soundfiler_queue->begin == soundfiler_queue->end )
      {
  #ifdef DEBUG
! 		post("signaling");
  #endif
! 		pthread_mutex_unlock(&(soundfiler_queue->mutex));
! 		/* and signal the helper thread */
! 		pthread_cond_signal(&(soundfiler_queue->cond));
      }
      else
      {
  #ifdef DEBUG
! 		post("not signaling");
  #endif
! 		pthread_mutex_unlock(&(soundfiler_queue->mutex));
      }
      return;
***************
*** 1028,1165 ****
  {    
  	t_sfprocess * me;
!     t_int i;
  	t_sfprocess * next;
  	while (1)
      {
  #ifdef DEBUG
! 	post("Soundfiler sleeping");
  #endif
! 	pthread_cond_wait(&soundfiler_queue->cond, &soundfiler_queue->mutex);
! 	pthread_mutex_unlock(&(soundfiler_queue->mutex)); 
  #ifdef DEBUG
! 	post("Soundfiler awake");
! #endif
! 	/* work on the queue */
! 	
! 	pthread_mutex_lock(&(soundfiler_queue->mutex));
! 	while (soundfiler_queue->begin!=NULL)
! 	{
! 	    /* locking process */
! 	    pthread_mutex_lock(&(soundfiler_queue->begin->mutex));
! 	    
! #ifdef SFDEBUG
! 	    post("working on process: %x",soundfiler_queue->begin);
  #endif
  
! 	    /* create a copy that we can work on */
! 	    me = copybytes(soundfiler_queue->begin, 
! 					 sizeof(t_sfprocess));
! 	    
! 	    me->argv = copybytes (soundfiler_queue->begin->argv, 
! 				  soundfiler_queue->begin->argc * sizeof(t_atom));
! #ifdef SFDEBUG
! 	    post("\t\told begin: %x", soundfiler_queue->begin);
! 	    post("\t\told next: %x", soundfiler_queue->begin->next);
! 	    post("\t\told end: %x", soundfiler_queue->end);
! #endif
  	    
! 
! 	    for (i = 0; i != me->argc; ++i)
! 	    {
! 		if ( (soundfiler_queue->begin->argv+i)->a_type == A_SYMBOL)
! 		    {
! 		    t_symbol * sym1 = getbytes(sizeof(t_symbol));
! 		    sym1->s_name = copybytes((soundfiler_queue->begin->argv+i)
! 					     ->a_w.w_symbol-> s_name,
! 					     strlen((soundfiler_queue->begin
! 						     ->argv+i)->a_w.w_symbol
! 						    ->s_name)
! 					     * (sizeof(char)+1) );
! 		    sym1->s_thing = (soundfiler_queue->begin->argv+i)
! 			->a_w.w_symbol->s_thing;
! 		    sym1->s_next = (soundfiler_queue->begin->argv+i)
! 			->a_w.w_symbol->s_next;
! 		    (me->argv+i)->a_w.w_symbol=sym1;
! 		}
! 	    }
  		
! 	    pthread_mutex_unlock(&(soundfiler_queue->begin->mutex));
! 
! 	    
! 	    pthread_mutex_unlock(&(soundfiler_queue->mutex)); 
  #ifdef DEBUG
! 	    post("soundfiler: mutex unlocked, running process");
  #endif
  
! 	    /* running the specific function */
! 
! 	    me->process(me->x, NULL, me->argc, me->argv);
  #ifdef DEBUG
! 	    post("soundfiler: process done, locking mutex");
! #endif
! 	    
! 	    pthread_mutex_lock(&(soundfiler_queue->mutex));
! 	    pthread_mutex_lock(&(soundfiler_queue->begin->mutex));
! 
! #ifdef SFDEBUG
! 	    post("\tfreeing allocated memory");
  #endif
  	    
! 	    /* freeing the memory we allocated */
! 	    for (i = 0; i != soundfiler_queue->begin->argc; ++i)
! 	    {
! 		if ((soundfiler_queue->begin->argv+i)->a_type == A_SYMBOL)
! 		{
! 		    /* the string */
! 		    freebytes( (soundfiler_queue->begin->argv+i)->
! 			       a_w.w_symbol->s_name, 
! 			       strlen((soundfiler_queue->begin->argv+i)->
! 				      a_w.w_symbol->s_name)
! 			       * (sizeof(char)+1) ); 
! 		    /* the symbol */
! 		    freebytes( (soundfiler_queue->begin->argv+i)->a_w.w_symbol, 
! 			       sizeof(t_symbol) ); 
! 		}
! 	    }
! 	    
! 	    /* the argument vector */
! 	    freebytes(soundfiler_queue->begin->argv,sizeof(t_atom) 
! 		      * soundfiler_queue->begin->argc); 
! 
! 	    /* the process struct */
! 	    next=soundfiler_queue->begin->next;
! 	    freebytes(soundfiler_queue->begin,sizeof(t_sfprocess));
! 	    soundfiler_queue->begin=next;
  
! 	    /* we'll have to free the memory again */
! 	    for (i = 0; i != me->argc; ++i)
! 	    {
! 		if ( (me->argv+i)->a_type == A_SYMBOL)
! 		{
! 		    /* the string */
! 		    freebytes( (me->argv+i)->a_w.w_symbol->s_name, 
! 			       strlen((me->argv+i)->a_w.w_symbol->s_name)
! 			       * (sizeof(char)+1) ); 
! 		    /* the symbol */
! 		    freebytes( (me->argv+i)->a_w.w_symbol, sizeof(t_symbol) );
! 		}
! 	    }
! 	    /* the argument vector */
! 	    freebytes(me->argv,sizeof(t_atom) * me->argc);
! 	    /* the process struct */
! 	    freebytes(me,sizeof(t_sfprocess));
! #ifdef SFDEBUG
! 	    post("\t\tnew begin: %x", soundfiler_queue->begin);
! 	    post("\t\tnew end: %x", soundfiler_queue->end);
! #endif
  
! 	};
! 	soundfiler_queue->end=NULL;
! #ifdef SFDEBUG
! 	post("\tbegin: %x", soundfiler_queue->begin);
! 	post("\tend: %x", soundfiler_queue->end);
! #endif
      }
-     
  }
  
--- 1004,1053 ----
  {    
  	t_sfprocess * me;
!     int i;
  	t_sfprocess * next;
  	while (1)
      {
  #ifdef DEBUG
! 		post("Soundfiler sleeping");
  #endif
! 		pthread_cond_wait(&soundfiler_queue->cond, &soundfiler_queue->mutex);
  #ifdef DEBUG
! 		post("Soundfiler awake");
  #endif
  
! 		/* work on the queue */
! 		pthread_mutex_lock(&(soundfiler_queue->mutex));
! 		while (soundfiler_queue->begin!=NULL)
! 		{
! 			/* locking process */
! 			pthread_mutex_lock(&(soundfiler_queue->begin->mutex));
  	    
! 			me = soundfiler_queue->begin;
  		
! 			pthread_mutex_unlock(&(me->mutex));
! 			pthread_mutex_unlock(&(soundfiler_queue->mutex)); 
  #ifdef DEBUG
! 			post("soundfiler: mutex unlocked, running process");
  #endif
  
! 			/* running the specific function */
! 			me->process(me->x, NULL, me->argc, me->argv);
  #ifdef DEBUG
! 			post("soundfiler: process done, locking mutex");
  #endif
  	    
! 			pthread_mutex_lock(&(soundfiler_queue->mutex));
! 			pthread_mutex_lock(&(me->mutex));
  
! 			/* freeing the argument vector */
! 			freebytes(me->argv, sizeof(t_atom) * me->argc); 
  
! 			/* the  process struct */
! 			next=me->next;
! 			soundfiler_queue->begin=next;
! 			freebytes(me, sizeof(t_sfprocess));
! 		};
! 		soundfiler_queue->end=NULL;
      }
  }
  
***************
*** 1170,1174 ****
      struct sched_param sf_param;
  
! 	t_int status;
  
  	//initialize queue
--- 1058,1062 ----
      struct sched_param sf_param;
  
! 	int status;
  
  	//initialize queue
***************
*** 1190,1199 ****
      pthread_attr_setinheritsched(&sf_attr,PTHREAD_EXPLICIT_SCHED);
  
! #ifdef UNIX
!     if (getuid() == 0)
!     {
! 	sf_param.sched_priority=sched_get_priority_min(SCHED_RR);
! 	pthread_attr_setschedpolicy(&sf_attr,SCHED_RR);
!     }
  #endif /* UNIX */
  
--- 1078,1086 ----
      pthread_attr_setinheritsched(&sf_attr,PTHREAD_EXPLICIT_SCHED);
  
! #ifdef UNIX /* use realtime scheduling */
! 	if (pthread_attr_setschedpolicy(&sf_attr,SCHED_RR) != ENOTSUP)
! 		sf_param.sched_priority=sched_get_priority_min(SCHED_RR);
! 	else
! 		sf_param.sched_priority=sched_get_priority_min(SCHED_OTHER);
  #endif /* UNIX */
  
***************
*** 1202,1219 ****
      //start thread
      status = pthread_create(&sf_thread_id, &sf_attr, 
! 				(void *) soundfiler_thread,NULL);
      if ( status !=0)
! 	error("Couldn't create soundfiler thread: %d",status);
      else
! 	post("global soundfiler thread launched, priority: %d", 
! 	     sf_param.sched_priority);
! 
  }
  
  static void soundfiler_t_write(t_soundfiler *x, t_symbol *s,
! 			       int argc, t_atom *argv);
  
  static void soundfiler_t_write_addq(t_soundfiler *x, t_symbol *s,
!     int argc, t_atom *argv)
  {
      soundfiler_queue_add(soundfiler_t_write,(void *)x,argc, argv);
--- 1089,1105 ----
      //start thread
      status = pthread_create(&sf_thread_id, &sf_attr, 
! 							(void *) soundfiler_thread,NULL);
      if ( status !=0)
! 		error("Couldn't create soundfiler thread: %d",status);
      else
! 		post("global soundfiler thread launched, priority: %d", 
! 			 sf_param.sched_priority);
  }
  
  static void soundfiler_t_write(t_soundfiler *x, t_symbol *s,
! 							   int argc, t_atom *argv);
  
  static void soundfiler_t_write_addq(t_soundfiler *x, t_symbol *s,
! 									int argc, t_atom *argv)
  {
      soundfiler_queue_add(soundfiler_t_write,(void *)x,argc, argv);
***************
*** 1221,1228 ****
  
  static void soundfiler_t_read(t_soundfiler *x, t_symbol *s,
! 			      int argc, t_atom *argv);
  
  static void soundfiler_t_read_addq(t_soundfiler *x, t_symbol *s,
!     int argc, t_atom *argv)
  {
      soundfiler_queue_add(soundfiler_t_read,(void *)x,argc, argv);
--- 1107,1114 ----
  
  static void soundfiler_t_read(t_soundfiler *x, t_symbol *s,
! 							  int argc, t_atom *argv);
  
  static void soundfiler_t_read_addq(t_soundfiler *x, t_symbol *s,
! 								   int argc, t_atom *argv)
  {
      soundfiler_queue_add(soundfiler_t_read,(void *)x,argc, argv);





More information about the Pd-cvs mailing list