Ignore:
Timestamp:
Oct 6, 2016, 8:03:01 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
sampler
Children:
6cc2d9d
Parents:
2f1ab3f
Message:

src/synth/sampler.c: add thread open/close functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/synth/sampler.c

    r2f1ab3f rb959a8f  
    2828
    2929#define HAVE_THREADS 1
     30#define READER_THREAD_ON 0
    3031#if 0
    3132#undef HAVE_THREADS
     
    4748  uint_t eof;                   // end of file is now
    4849#ifdef HAVE_THREADS
    49   pthread_t read_thread;        // file reading thread
     50  // file reading thread
     51  pthread_t read_thread;
     52  uint_t threaded_read;         // use reading thread?
    5053  pthread_mutex_t read_mutex;
    5154  pthread_cond_t read_avail;
     
    6366
    6467#ifdef HAVE_THREADS
     68static void *aubio_sampler_openfn(void *p);
    6569static void *aubio_sampler_readfn(void *p);
     70static void aubio_sampler_open_opening_thread(aubio_sampler_t *o);
     71static void aubio_sampler_open_reading_thread(aubio_sampler_t *o);
     72static void aubio_sampler_close_opening_thread(aubio_sampler_t *o);
     73static void aubio_sampler_close_reading_thread(aubio_sampler_t *o);
    6674#endif
    6775
     
    8290  s->eof = 0;
    8391  s->opened = 0;
    84 
    85 #ifdef HAVE_THREADS
     92  s->available = 0;
     93
     94#ifdef HAVE_THREADS
     95  s->threaded_read = READER_THREAD_ON;
     96  aubio_sampler_open_opening_thread(s);
     97  if (s->threaded_read) {
     98    aubio_sampler_open_reading_thread(s);
     99  }
     100#endif
     101
     102  return s;
     103beach:
     104  AUBIO_FREE(s);
     105  return NULL;
     106}
     107
     108#ifdef HAVE_THREADS
     109void aubio_sampler_open_opening_thread(aubio_sampler_t *s) {
    86110  pthread_mutex_init(&s->open_mutex, 0);
    87111  s->waited = 0;
    88112  s->open_thread = 0;
    89113  s->open_thread_running = 0;
    90 
     114}
     115
     116void aubio_sampler_open_reading_thread(aubio_sampler_t *s) {
    91117  s->read_thread_finish = 0;
    92118  pthread_mutex_init(&s->read_mutex, 0);
     
    94120  pthread_cond_init (&s->read_request, 0);
    95121  pthread_create(&s->read_thread, 0, aubio_sampler_readfn, s);
    96 #endif
    97   return s;
    98 beach:
    99   AUBIO_FREE(s);
    100   return NULL;
    101 }
     122}
     123
     124void aubio_sampler_close_opening_thread(aubio_sampler_t *o) {
     125  // clean up opening thread
     126  void *threadret;
     127  pthread_mutex_destroy(&o->open_mutex);
     128  if (o->open_thread_running) {
     129    if (pthread_cancel(o->open_thread)) {
     130      AUBIO_WRN("sampler: cancelling file opening thread failed\n");
     131    }
     132  }
     133  if (o->open_thread && pthread_join(o->open_thread, &threadret)) {
     134    AUBIO_WRN("sampler: joining file opening thread failed\n");
     135  }
     136  pthread_mutex_destroy(&o->open_mutex);
     137}
     138
     139void aubio_sampler_close_reading_thread(aubio_sampler_t *o) {
     140  // clean up reading thread
     141  void *threadret;
     142  if (!o->read_thread) return;
     143  o->read_thread_finish = 1;
     144  pthread_cond_signal(&o->read_request);
     145  if (pthread_cancel(o->read_thread)) {
     146    AUBIO_WRN("sampler: cancelling file reading thread failed\n");
     147  }
     148  if (pthread_join(o->read_thread, &threadret)) {
     149    AUBIO_WRN("sampler: joining file reading thread failed\n");
     150  }
     151  pthread_mutex_destroy(&o->read_mutex);
     152  pthread_cond_destroy(&o->read_avail);
     153  pthread_cond_destroy(&o->read_request);
     154}
     155#endif
    102156
    103157uint_t aubio_sampler_load( aubio_sampler_t * o, const char_t * uri )
     
    391445{
    392446#ifdef HAVE_THREADS
    393   void *threadret;
    394 
    395   // clean up opening thread
    396   pthread_mutex_destroy(&o->open_mutex);
    397   if (o->open_thread_running) {
    398     if (pthread_cancel(o->open_thread)) {
    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");
    404   }
    405   pthread_mutex_destroy(&o->open_mutex);
    406 
     447  // close opening thread
     448  aubio_sampler_close_opening_thread(o);
    407449  // 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);
     450  aubio_sampler_close_reading_thread(o);
    419451#endif
    420452  if (o->source) {
Note: See TracChangeset for help on using the changeset viewer.