created semaphore for queue and mutex for pause

This commit is contained in:
2026-04-14 15:08:32 +02:00
parent d5957c86bd
commit d33bdb3482
3 changed files with 52 additions and 26 deletions

View File

@@ -13,6 +13,9 @@ install: all
SUBDIR=`racket -e "(display (format \"~a-~a\" (system-type 'os*) (system-type 'arch)))"`; \ SUBDIR=`racket -e "(display (format \"~a-~a\" (system-type 'os*) (system-type 'arch)))"`; \
FILES=`ls build/*.dll` 2>/dev/null; if [ "$$FILES" != "" ]; then cp $$FILES lib/$$SUBDIR; fi FILES=`ls build/*.dll` 2>/dev/null; if [ "$$FILES" != "" ]; then cp $$FILES lib/$$SUBDIR; fi
test: install
cp lib/linux-x86_64/*.so ~/.local/share/racket/racket-sound-lib/linux-x86_64
zip: install zip: install
SUBDIR=`racket -e "(display (format \"~a-~a\" (system-type 'os*) (system-type 'arch)))"`; \ SUBDIR=`racket -e "(display (format \"~a-~a\" (system-type 'os*) (system-type 'arch)))"`; \
(cd lib; zip -y -r -9 $$SUBDIR.zip $$SUBDIR) (cd lib; zip -y -r -9 $$SUBDIR.zip $$SUBDIR)

View File

@@ -16,6 +16,7 @@
#ifdef USE_PTHREADS #ifdef USE_PTHREADS
#include <pthread.h> #include <pthread.h>
#include <semaphore.h>
#define MUTEX_LOCK(m) pthread_mutex_lock(&m) #define MUTEX_LOCK(m) pthread_mutex_lock(&m)
#define MUTEX_UNLOCK(m) pthread_mutex_unlock(&m) #define MUTEX_UNLOCK(m) pthread_mutex_unlock(&m)
#endif #endif
@@ -55,21 +56,26 @@ typedef int(*ao_play_func_t)(void *, char *, uint32_t);
typedef struct { typedef struct {
Queue_t *play_queue; Queue_t *play_queue;
Queue_t *last_frame; Queue_t *last_frame;
sem_t queue_sem;
int paused;
ao_device *ao_device; ao_device *ao_device;
#ifdef USE_WINDOWS_THREADS #ifdef USE_WINDOWS_THREADS
HANDLE mutex; HANDLE mutex;
HANDLE pause_mutex;
HANDLE thread; HANDLE thread;
DWORD thread_id; DWORD thread_id;
#endif #endif
#ifdef USE_PTHREADS #ifdef USE_PTHREADS
pthread_mutex_t mutex; pthread_mutex_t mutex;
pthread_mutex_t pause_mutex;
pthread_t thread; pthread_t thread;
#endif #endif
double at_second; double at_second;
double music_duration; double music_duration;
ao_play_func_t ao_play_f; ao_play_func_t ao_play_f;
int buf_size; int buf_size;
int paused;
} AO_Handle; } AO_Handle;
//static int(*ao_play)(void *device, char *samples, uint32_t n) = NULL; //static int(*ao_play)(void *device, char *samples, uint32_t n) = NULL;
@@ -83,6 +89,8 @@ static Queue_t *front(AO_Handle *h)
static Queue_t *get(AO_Handle *h) static Queue_t *get(AO_Handle *h)
{ {
sem_wait(&h->queue_sem);
MUTEX_LOCK(h->mutex);
assert(h->play_queue != NULL); assert(h->play_queue != NULL);
Queue_t *q = h->play_queue; Queue_t *q = h->play_queue;
h->play_queue = h->play_queue->next; h->play_queue = h->play_queue->next;
@@ -92,11 +100,13 @@ static Queue_t *get(AO_Handle *h)
h->play_queue->prev = NULL; h->play_queue->prev = NULL;
} }
h->buf_size -= q->buflen; h->buf_size -= q->buflen;
MUTEX_UNLOCK(h->mutex);
return q; return q;
} }
static void add(AO_Handle *h, Queue_t *elem) static void add(AO_Handle *h, Queue_t *elem)
{ {
MUTEX_LOCK(h->mutex);
if (h->last_frame == NULL) { if (h->last_frame == NULL) {
h->play_queue = elem; h->play_queue = elem;
elem->next = NULL; elem->next = NULL;
@@ -109,6 +119,8 @@ static void add(AO_Handle *h, Queue_t *elem)
h->last_frame = elem; h->last_frame = elem;
} }
h->buf_size += elem->buflen; h->buf_size += elem->buflen;
MUTEX_UNLOCK(h->mutex);
sem_post(&h->queue_sem);
} }
static Queue_t *new_elem(int command, double at_second, double music_duration, int buf_len, void *buf) static Queue_t *new_elem(int command, double at_second, double music_duration, int buf_len, void *buf)
@@ -126,7 +138,7 @@ static Queue_t *new_elem(int command, double at_second, double music_duration, i
q->music_duration = music_duration; q->music_duration = music_duration;
q->buf = new_buf; q->buf = new_buf;
q->buflen = buf_len; q->buflen = buf_len;
q->command = command; q->command = (Command_t) command;
q->next = NULL; q->next = NULL;
q->prev = NULL; q->prev = NULL;
return q; return q;
@@ -160,27 +172,22 @@ static DWORD run(LPVOID arg)
int go_on = (1 == 1); int go_on = (1 == 1);
while(go_on) { while(go_on) {
MUTEX_LOCK(handle->mutex); MUTEX_LOCK(handle->pause_mutex);
int has_frames = (!handle->paused) && (handle->play_queue != NULL); MUTEX_UNLOCK(handle->pause_mutex);
if (has_frames) { Queue_t *q = get(handle);
Queue_t *q = get(handle);
handle->at_second = q->at_second;
handle->music_duration = q->music_duration;
MUTEX_UNLOCK(handle->mutex);
if (q->command == STOP) { handle->at_second = q->at_second;
go_on = (1 == 0); handle->music_duration = q->music_duration;
} else {
//fprintf(stderr, "playing buf at %lf\n", handle->at_second);
handle->ao_play_f(handle->ao_device, q->buf, q->buflen);
}
del_elem(q); if (q->command == STOP) {
go_on = (1 == 0);
} else { } else {
MUTEX_UNLOCK(handle->mutex); //fprintf(stderr, "playing buf at %lf\n", handle->at_second);
sleep_ms(5); // sleep for 5ms handle->ao_play_f(handle->ao_device, (char *) q->buf, q->buflen);
} }
del_elem(q);
} }
#ifdef USE_PTHREADS #ifdef USE_PTHREADS
@@ -207,23 +214,33 @@ void *ao_create_async(void *ao_device_yeah, void *ao_play_f)
handle->last_frame = NULL; handle->last_frame = NULL;
handle->at_second = -1; handle->at_second = -1;
handle->ao_play_f = ao_play_f; handle->ao_play_f = (ao_play_func_t) ao_play_f;
handle->buf_size = 0; handle->buf_size = 0;
handle->paused = (1 == 0); handle->paused = (1 == 0);
#ifdef USE_PTHREADS #ifdef USE_PTHREADS
pthread_mutex_t p = PTHREAD_MUTEX_INITIALIZER;
handle->pause_mutex = p;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
handle->mutex = m; handle->mutex = m;
pthread_create(&handle->thread, NULL, run, handle); pthread_create(&handle->thread, NULL, run, handle);
sem_init(&handle->queue_sem, 0, 0);
#endif #endif
#ifdef USE_WINDOWS_THREADS #ifdef USE_WINDOWS_THREADS
handle->mutex = CreateMutex(NULL, // default security attributes handle->mutex = CreateMutex(NULL, // default security attributes
FALSE, // initially not owned FALSE, // initially not owned
NULL); NULL);
handle->pause_mutex = CreateMutex(NULL, // default security attributes
FALSE, // initially not owned
NULL);
handle->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) run, handle, 0, &handle->thread_id); handle->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) run, handle, 0, &handle->thread_id);
// TODO Windows Semaphores!
#endif #endif
MUTEX_UNLOCK(handle->pause_mutex);
return (void *) handle; return (void *) handle;
} }
@@ -231,11 +248,9 @@ void ao_stop_async(void *ao_handle)
{ {
AO_Handle *h = (AO_Handle *) ao_handle; AO_Handle *h = (AO_Handle *) ao_handle;
MUTEX_LOCK(h->mutex);
clear(h); clear(h);
Queue_t *q = new_elem(STOP, 0.0, 0.0, 0, NULL); Queue_t *q = new_elem(STOP, 0.0, 0.0, 0, NULL);
add(h, q); add(h, q);
MUTEX_UNLOCK(h->mutex);
#ifdef USE_PTHREADS #ifdef USE_PTHREADS
void *retval; void *retval;
@@ -375,17 +390,13 @@ void ao_play_async(void *ao_handle, double at_second, double music_duration, int
break; break;
} }
MUTEX_LOCK(h->mutex);
add(h, q); add(h, q);
MUTEX_UNLOCK(h->mutex);
} }
void ao_clear_async(void *ao_handle) void ao_clear_async(void *ao_handle)
{ {
AO_Handle *h = (AO_Handle *) ao_handle; AO_Handle *h = (AO_Handle *) ao_handle;
MUTEX_LOCK(h->mutex);
clear(h); clear(h);
MUTEX_UNLOCK(h->mutex);
} }
double ao_is_at_second_async(void *ao_handle) double ao_is_at_second_async(void *ao_handle)
@@ -419,7 +430,19 @@ void ao_pause_async(void *ao_handle, int paused)
{ {
AO_Handle *h = (AO_Handle *) ao_handle; AO_Handle *h = (AO_Handle *) ao_handle;
MUTEX_LOCK(h->mutex); MUTEX_LOCK(h->mutex);
h->paused = paused;
if (h->paused) {
if (!paused) {
h->paused = paused;
MUTEX_UNLOCK(h->pause_mutex);
}
} else {
if (paused) {
h->paused = paused;
MUTEX_LOCK(h->pause_mutex);
}
}
MUTEX_UNLOCK(h->mutex); MUTEX_UNLOCK(h->mutex);
} }

Binary file not shown.