diff options
author | Alex Chronopoulos <[email protected]> | 2016-11-18 07:03:27 +0200 |
---|---|---|
committer | Matthew Gregan <[email protected]> | 2016-11-18 18:03:27 +1300 |
commit | 023503faf86c7f3cc32711388c4f0ca887dd8718 (patch) | |
tree | 9356b8fce2be7c0b6c44c0154b74fd70b8275aab /src/cubeb_array_queue.h | |
parent | 7f74039f92f4e7183188f9f00a207df8f16330a5 (diff) | |
download | cubeb-023503faf86c7f3cc32711388c4f0ca887dd8718.tar.gz cubeb-023503faf86c7f3cc32711388c4f0ca887dd8718.zip |
OpenSL ES full duplex restored (#196)
* opensl: Restore full duplex for OpenSL ES from 1e26a908
* opensl: Fix build errors from gecko build system
* opensl: Add extra debug logs
* opensl: On cubeb_stop pause instead of stop
* opensl: Remove output queue and a small refactoring
* opensl: Remove extra stream destroy on error
* opensl: Change message on assert fail
* opensl: Restore logs after new log system change
* opensl: Android 6 expect EBUSY instead of EDEADLK
Diffstat (limited to 'src/cubeb_array_queue.h')
-rw-r--r-- | src/cubeb_array_queue.h | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/cubeb_array_queue.h b/src/cubeb_array_queue.h new file mode 100644 index 0000000..ef6d0fe --- /dev/null +++ b/src/cubeb_array_queue.h @@ -0,0 +1,98 @@ +/* + * Copyright © 2016 Mozilla Foundation + * + * This program is made available under an ISC-style license. See the + * accompanying file LICENSE for details. + */ + +#ifndef CUBEB_ARRAY_QUEUE_H +#define CUBEB_ARRAY_QUEUE_H + +#include <assert.h> +#include <pthread.h> +#include <unistd.h> + +#if defined(__cplusplus) +extern "C" { +#endif + +typedef struct +{ + void ** buf; + size_t num; + size_t writePos; + size_t readPos; + pthread_mutex_t mutex; +} array_queue; + +array_queue * array_queue_create(size_t num) +{ + assert(num != 0); + array_queue * new_queue = (array_queue*)calloc(1, sizeof(array_queue)); + new_queue->buf = (void **)calloc(1, sizeof(void *) * num); + new_queue->readPos = 0; + new_queue->writePos = 0; + new_queue->num = num; + + pthread_mutex_init(&new_queue->mutex, NULL); + + return new_queue; +} + +void array_queue_destroy(array_queue * aq) +{ + assert(aq); + + free(aq->buf); + pthread_mutex_destroy(&aq->mutex); + pthread_cond_destroy(&aq->empty_convar); + free(aq); +} + +int array_queue_push(array_queue * aq, void * item) +{ + assert(item); + + pthread_mutex_lock(&aq->mutex); + int ret = -1; + if(aq->buf[aq->writePos % aq->num] == NULL) + { + aq->buf[aq->writePos % aq->num] = item; + aq->writePos = (aq->writePos + 1) % aq->num; + ret = 0; + } + // else queue is full + pthread_mutex_unlock(&aq->mutex); + return ret; +} + +void* array_queue_pop(array_queue * aq) +{ + pthread_mutex_lock(&aq->mutex); + void * value = aq->buf[aq->readPos % aq->num]; + if(value) + { + aq->buf[aq->readPos % aq->num] = NULL; + aq->readPos = (aq->readPos + 1) % aq->num; + } + pthread_mutex_unlock(&aq->mutex); + return value; +} + +size_t array_queue_get_size(array_queue * aq) +{ + pthread_mutex_lock(&aq->mutex); + ssize_t r = aq->writePos - aq->readPos; + if (r < 0) { + r = aq->num + r; + assert(r >= 0); + } + pthread_mutex_unlock(&aq->mutex); + return (size_t)r; +} + +#if defined(__cplusplus) +} +#endif + +#endif //CUBE_ARRAY_QUEUE_H |