diff options
Diffstat (limited to 'src/messages.c')
| -rw-r--r-- | src/messages.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/messages.c b/src/messages.c new file mode 100644 index 0000000..80998a7 --- /dev/null +++ b/src/messages.c @@ -0,0 +1,47 @@ +#include "messages.h" + +int mqueue_get(message_queue *q, synth_message *msg) { + pthread_mutex_lock(&(q)->lock); + if ((q)->tail == (q)->head) { + pthread_mutex_unlock(&(q)->lock); + return 1; + } + + *(msg) = (q)->buffer[(q)->tail]; + (q)->tail = ((q)->tail + 1) % MESSAGE_QUEUE_SIZE; + + pthread_mutex_unlock(&(q)->lock); + return 0; +} + +int mqueue__push_no_lock(message_queue *q, synth_message msg) { + size_t next = ((q)->head + 1) % MESSAGE_QUEUE_SIZE; + + if ((q)->tail == next) { + return 1; + } + + (q)->buffer[(q)->head] = msg; + (q)->head = next; + return 0; +} + +int mqueue_push(message_queue *q, synth_message msg) { + pthread_mutex_lock(&(q)->lock); + int ret = mqueue__push_no_lock(q, msg); + pthread_mutex_unlock(&(q)->lock); + return ret; +} + +int mqueue_push_many(message_queue *q, synth_message *msg, size_t count) { + pthread_mutex_lock(&(q)->lock); + int ret = 0; + for (size_t i = 0; i < count; i++) { + ret = mqueue__push_no_lock(q, msg[i]); + if (ret != 0) { + break; + } + } + pthread_mutex_unlock(&(q)->lock); + return ret; +} |
