From 13ad60a139643056b0787a4dde86c7d8ed52d934 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Tue, 3 Nov 2015 06:32:55 +0100 Subject: fixed semaphore for realtime<->lua thread synchronization --- applause.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'applause.c') diff --git a/applause.c b/applause.c index 32f1a54..fb659bb 100644 --- a/applause.c +++ b/applause.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -67,8 +68,15 @@ svsem_op(int id, int value) .sem_op = value, .sem_flg = 0 }; + int rc; - return semop(id, &op, 1); + /* + * Repeat operation when it is interrupted. + */ + while ((rc = semop(id, &op, 1)) < 0 && + errno == EINTR); + + return rc; } static inline int @@ -114,7 +122,7 @@ jack_process(jack_nframes_t nframes, void *arg) /* * The semaphor value corresponds with the number of - * writable samples in buffer, i.e. the available space. + * writable bytes in buffer, i.e. the available space. * This operation should never block and is supposed to * be real-time safe :-) */ @@ -144,7 +152,7 @@ jack_shutdown(void *arg) static int init_audio(int buffer_size) { - size_t buffer_samples; + size_t buffer_bytes; const char **ports; const char *client_name = "applause"; const char *server_name = NULL; @@ -199,15 +207,15 @@ init_audio(int buffer_size) * since it represents the available bytes in the ring * buffer. */ - buffer_samples = jack_get_sample_rate(client)*buffer_size/1000; - buffer = jack_ringbuffer_create(sizeof(jack_default_audio_sample_t)* - buffer_samples); + buffer_bytes = sizeof(jack_default_audio_sample_t)* + jack_get_sample_rate(client)*buffer_size/1000; + buffer = jack_ringbuffer_create(buffer_bytes); if (!buffer) { fprintf(stderr, "cannot create ringbuffer\n"); return 1; } - buffer_sem = svsem_init(buffer_samples); + buffer_sem = svsem_init(buffer_bytes); if (buffer_sem < 0) { fprintf(stderr, "error initializing semaphore\n"); return 1; @@ -311,7 +319,7 @@ l_Stream_play(lua_State *L) * in buffer since jack_process() will only read from the * buffer. */ - svsem_op(buffer_sem, -1); + svsem_op(buffer_sem, -(int)sizeof(sample)); jack_ringbuffer_write(buffer, (const char *)&sample, sizeof(sample)); -- cgit v1.2.3