aboutsummaryrefslogtreecommitdiffhomepage
path: root/applause.c
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2015-11-03 06:32:55 +0100
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2015-11-03 06:32:55 +0100
commit13ad60a139643056b0787a4dde86c7d8ed52d934 (patch)
treea6b87241988635c992f42f05d214c359f2224c48 /applause.c
parentb74c54a02923516f4f687484484c79b0501f98b3 (diff)
downloadapplause2-13ad60a139643056b0787a4dde86c7d8ed52d934.tar.gz
fixed semaphore for realtime<->lua thread synchronization
Diffstat (limited to 'applause.c')
-rw-r--r--applause.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/applause.c b/applause.c
index 32f1a54..fb659bb 100644
--- a/applause.c
+++ b/applause.c
@@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
+#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
@@ -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));