src/synth/sampler.c: add thread open/close functions
authorPaul Brossier <piem@piem.org>
Thu, 6 Oct 2016 18:03:01 +0000 (20:03 +0200)
committerPaul Brossier <piem@piem.org>
Thu, 6 Oct 2016 18:03:01 +0000 (20:03 +0200)
src/synth/sampler.c

index 5c9c0ad..83584bf 100644 (file)
@@ -27,6 +27,7 @@
 #include "synth/sampler.h"
 
 #define HAVE_THREADS 1
+#define READER_THREAD_ON 0
 #if 0
 #undef HAVE_THREADS
 #endif
@@ -46,7 +47,9 @@ struct _aubio_sampler_t {
   uint_t finished;              // end of file was reached
   uint_t eof;                   // end of file is now
 #ifdef HAVE_THREADS
-  pthread_t read_thread;        // file reading thread
+  // file reading thread
+  pthread_t read_thread;
+  uint_t threaded_read;         // use reading thread?
   pthread_mutex_t read_mutex;
   pthread_cond_t read_avail;
   pthread_cond_t read_request;
@@ -62,7 +65,12 @@ struct _aubio_sampler_t {
 };
 
 #ifdef HAVE_THREADS
+static void *aubio_sampler_openfn(void *p);
 static void *aubio_sampler_readfn(void *p);
+static void aubio_sampler_open_opening_thread(aubio_sampler_t *o);
+static void aubio_sampler_open_reading_thread(aubio_sampler_t *o);
+static void aubio_sampler_close_opening_thread(aubio_sampler_t *o);
+static void aubio_sampler_close_reading_thread(aubio_sampler_t *o);
 #endif
 
 aubio_sampler_t *new_aubio_sampler(uint_t blocksize, uint_t samplerate)
@@ -81,25 +89,71 @@ aubio_sampler_t *new_aubio_sampler(uint_t blocksize, uint_t samplerate)
   s->finished = 1;
   s->eof = 0;
   s->opened = 0;
+  s->available = 0;
 
 #ifdef HAVE_THREADS
+  s->threaded_read = READER_THREAD_ON;
+  aubio_sampler_open_opening_thread(s);
+  if (s->threaded_read) {
+    aubio_sampler_open_reading_thread(s);
+  }
+#endif
+
+  return s;
+beach:
+  AUBIO_FREE(s);
+  return NULL;
+}
+
+#ifdef HAVE_THREADS
+void aubio_sampler_open_opening_thread(aubio_sampler_t *s) {
   pthread_mutex_init(&s->open_mutex, 0);
   s->waited = 0;
   s->open_thread = 0;
   s->open_thread_running = 0;
+}
 
+void aubio_sampler_open_reading_thread(aubio_sampler_t *s) {
   s->read_thread_finish = 0;
   pthread_mutex_init(&s->read_mutex, 0);
   pthread_cond_init (&s->read_avail, 0);
   pthread_cond_init (&s->read_request, 0);
   pthread_create(&s->read_thread, 0, aubio_sampler_readfn, s);
-#endif
-  return s;
-beach:
-  AUBIO_FREE(s);
-  return NULL;
 }
 
+void aubio_sampler_close_opening_thread(aubio_sampler_t *o) {
+  // clean up opening thread
+  void *threadret;
+  pthread_mutex_destroy(&o->open_mutex);
+  if (o->open_thread_running) {
+    if (pthread_cancel(o->open_thread)) {
+      AUBIO_WRN("sampler: cancelling file opening thread failed\n");
+    }
+  }
+  if (o->open_thread && pthread_join(o->open_thread, &threadret)) {
+    AUBIO_WRN("sampler: joining file opening thread failed\n");
+  }
+  pthread_mutex_destroy(&o->open_mutex);
+}
+
+void aubio_sampler_close_reading_thread(aubio_sampler_t *o) {
+  // clean up reading thread
+  void *threadret;
+  if (!o->read_thread) return;
+  o->read_thread_finish = 1;
+  pthread_cond_signal(&o->read_request);
+  if (pthread_cancel(o->read_thread)) {
+    AUBIO_WRN("sampler: cancelling file reading thread failed\n");
+  }
+  if (pthread_join(o->read_thread, &threadret)) {
+    AUBIO_WRN("sampler: joining file reading thread failed\n");
+  }
+  pthread_mutex_destroy(&o->read_mutex);
+  pthread_cond_destroy(&o->read_avail);
+  pthread_cond_destroy(&o->read_request);
+}
+#endif
+
 uint_t aubio_sampler_load( aubio_sampler_t * o, const char_t * uri )
 {
   uint_t ret = AUBIO_FAIL;
@@ -390,32 +444,10 @@ uint_t aubio_sampler_trigger ( aubio_sampler_t * o )
 void del_aubio_sampler( aubio_sampler_t * o )
 {
 #ifdef HAVE_THREADS
-  void *threadret;
-
-  // clean up opening thread
-  pthread_mutex_destroy(&o->open_mutex);
-  if (o->open_thread_running) {
-    if (pthread_cancel(o->open_thread)) {
-      AUBIO_WRN("sampler: cancelling file opening thread failed\n");
-    }
-  }
-  if (o->open_thread && pthread_join(o->open_thread, &threadret)) {
-    AUBIO_WRN("sampler: joining file opening thread failed\n");
-  }
-  pthread_mutex_destroy(&o->open_mutex);
-
+  // close opening thread
+  aubio_sampler_close_opening_thread(o);
   // close reading thread
-  o->read_thread_finish = 1;
-  pthread_cond_signal(&o->read_request);
-  if (pthread_cancel(o->read_thread)) {
-    AUBIO_WRN("sampler: cancelling file reading thread failed\n");
-  }
-  if (pthread_join(o->read_thread, &threadret)) {
-    AUBIO_WRN("sampler: joining file reading thread failed\n");
-  }
-  pthread_mutex_destroy(&o->read_mutex);
-  pthread_cond_destroy(&o->read_avail);
-  pthread_cond_destroy(&o->read_request);
+  aubio_sampler_close_reading_thread(o);
 #endif
   if (o->source) {
     del_aubio_source(o->source);