Changeset 0a756ea for src/synth/sampler.c
- Timestamp:
- Oct 5, 2016, 2:30:57 PM (8 years ago)
- Branches:
- sampler
- Children:
- cb1fad8
- Parents:
- 88042ef
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/synth/sampler.c
r88042ef r0a756ea 44 44 uint_t opened; 45 45 uint_t loop; 46 uint_t finished; 47 uint_t eof; 48 #ifdef HAVE_THREADS 46 uint_t finished; // end of file was reached 47 uint_t eof; // end of file is now 48 #ifdef HAVE_THREADS 49 pthread_t read_thread; // file reading thread 50 pthread_mutex_t read_mutex; 51 pthread_cond_t read_avail; 52 pthread_cond_t read_request; 49 53 pthread_t open_thread; // file opening thread 50 54 pthread_mutex_t open_mutex; … … 54 58 sint_t available; // number of samples currently available 55 59 uint_t started; // source warmed up 56 uint_t finish;// flag to tell reading thread to exit60 uint_t read_thread_finish; // flag to tell reading thread to exit 57 61 #endif 58 62 }; 63 64 #ifdef HAVE_THREADS 65 static void *aubio_sampler_readfn(void *p); 66 #endif 59 67 60 68 aubio_sampler_t *new_aubio_sampler(uint_t blocksize, uint_t samplerate) … … 80 88 s->open_thread = 0; 81 89 s->open_thread_running = 0; 90 91 s->read_thread_finish = 0; 92 pthread_mutex_init(&s->read_mutex, 0); 93 pthread_cond_init (&s->read_avail, 0); 94 pthread_cond_init (&s->read_request, 0); 95 pthread_create(&s->read_thread, 0, aubio_sampler_readfn, s); 82 96 #endif 83 97 return s; … … 164 178 o->available = 0; 165 179 o->next_uri = uri; 180 o->waited = 0; 166 181 if (pthread_create(&o->open_thread, 0, aubio_sampler_openfn, o) != 0) { 167 182 AUBIO_ERR("sampler: failed creating opening thread\n"); … … 184 199 #endif 185 200 } 201 202 #ifdef HAVE_THREADS 203 void *aubio_sampler_readfn(void *z) { 204 aubio_sampler_t *p = z; 205 while(1) { 206 pthread_mutex_lock(&p->read_mutex); 207 if (1) { 208 // idle 209 pthread_cond_wait(&p->read_request, &p->read_mutex); 210 if (p->read_thread_finish) { 211 goto done; 212 } 213 } 214 pthread_mutex_unlock(&p->read_mutex); 215 } 216 done: 217 //AUBIO_WRN("sampler: exiting reading thread\n"); 218 pthread_mutex_unlock(&p->read_mutex); 219 pthread_exit(NULL); 220 } 221 #endif 186 222 187 223 void … … 251 287 o->finished = 0; 252 288 if (!o->opened) return AUBIO_OK; 253 #ifdef HAVE_THREADS254 if (pthread_mutex_trylock(&o->open_mutex)) {255 AUBIO_WRN("sampler: failed locking in seek\n");256 return ret;257 }258 #endif259 289 if (o->source) { 260 290 ret = aubio_source_seek(o->source, pos); 261 291 } 262 #ifdef HAVE_THREADS263 pthread_mutex_unlock(&o->open_mutex);264 #endif265 292 return ret; 266 293 } … … 364 391 { 365 392 #ifdef HAVE_THREADS 366 AUBIO_WRN("sampler: cleaning up\n"); 393 void *threadret; 394 395 // clean up opening thread 367 396 pthread_mutex_destroy(&o->open_mutex); 368 397 if (o->open_thread_running) { 369 398 if (pthread_cancel(o->open_thread)) { 370 AUBIO_WRN("sampler: cancelling open thread failed\n"); 371 } 372 } 373 void *threadret; 374 if (pthread_join(o->open_thread, &threadret)) { 375 AUBIO_WRN("sampler: joining open thread failed\n"); 399 AUBIO_WRN("sampler: cancelling file opening thread failed\n"); 400 } 401 } 402 if (o->open_thread && pthread_join(o->open_thread, &threadret)) { 403 AUBIO_WRN("sampler: joining file opening thread failed\n"); 376 404 } 377 405 pthread_mutex_destroy(&o->open_mutex); 406 407 // close reading thread 408 o->read_thread_finish = 1; 409 pthread_cond_signal(&o->read_request); 410 if (pthread_cancel(o->read_thread)) { 411 AUBIO_WRN("sampler: cancelling file reading thread failed\n"); 412 } 413 if (pthread_join(o->read_thread, &threadret)) { 414 AUBIO_WRN("sampler: joining file reading thread failed\n"); 415 } 416 pthread_mutex_destroy(&o->read_mutex); 417 pthread_cond_destroy(&o->read_avail); 418 pthread_cond_destroy(&o->read_request); 378 419 #endif 379 420 if (o->source) {
Note: See TracChangeset
for help on using the changeset viewer.