Ignore:
Timestamp:
Oct 5, 2016, 2:30:57 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
sampler
Children:
cb1fad8
Parents:
88042ef
Message:

src/synth/sampler.{c,h}: prepare reading thread, reset waited in _queue, only join open thread if still open, improve documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/synth/sampler.c

    r88042ef r0a756ea  
    4444  uint_t opened;
    4545  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;
    4953  pthread_t open_thread;        // file opening thread
    5054  pthread_mutex_t open_mutex;
     
    5458  sint_t available;             // number of samples currently available
    5559  uint_t started;               // source warmed up
    56   uint_t finish;                // flag to tell reading thread to exit
     60  uint_t read_thread_finish;    // flag to tell reading thread to exit
    5761#endif
    5862};
     63
     64#ifdef HAVE_THREADS
     65static void *aubio_sampler_readfn(void *p);
     66#endif
    5967
    6068aubio_sampler_t *new_aubio_sampler(uint_t blocksize, uint_t samplerate)
     
    8088  s->open_thread = 0;
    8189  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);
    8296#endif
    8397  return s;
     
    164178  o->available = 0;
    165179  o->next_uri = uri;
     180  o->waited = 0;
    166181  if (pthread_create(&o->open_thread, 0, aubio_sampler_openfn, o) != 0) {
    167182    AUBIO_ERR("sampler: failed creating opening thread\n");
     
    184199#endif
    185200}
     201
     202#ifdef HAVE_THREADS
     203void *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  }
     216done:
     217  //AUBIO_WRN("sampler: exiting reading thread\n");
     218  pthread_mutex_unlock(&p->read_mutex);
     219  pthread_exit(NULL);
     220}
     221#endif
    186222
    187223void
     
    251287  o->finished = 0;
    252288  if (!o->opened) return AUBIO_OK;
    253 #ifdef HAVE_THREADS
    254   if (pthread_mutex_trylock(&o->open_mutex)) {
    255     AUBIO_WRN("sampler: failed locking in seek\n");
    256     return ret;
    257   }
    258 #endif
    259289  if (o->source) {
    260290    ret = aubio_source_seek(o->source, pos);
    261291  }
    262 #ifdef HAVE_THREADS
    263   pthread_mutex_unlock(&o->open_mutex);
    264 #endif
    265292  return ret;
    266293}
     
    364391{
    365392#ifdef HAVE_THREADS
    366   AUBIO_WRN("sampler: cleaning up\n");
     393  void *threadret;
     394
     395  // clean up opening thread
    367396  pthread_mutex_destroy(&o->open_mutex);
    368397  if (o->open_thread_running) {
    369398    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");
    376404  }
    377405  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);
    378419#endif
    379420  if (o->source) {
Note: See TracChangeset for help on using the changeset viewer.