2009-11-13 21:28:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, Sangoma Technologies
|
|
|
|
* Moises Silva <moy@sangoma.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of the original author; nor the names of any contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
|
|
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openzap.h"
|
|
|
|
|
2009-11-14 02:04:38 +00:00
|
|
|
#undef zap_queue_t
|
|
|
|
typedef struct zap_queue {
|
|
|
|
zap_mutex_t *mutex;
|
|
|
|
zap_condition_t *condition;
|
2009-11-21 04:45:22 +00:00
|
|
|
zap_size_t capacity;
|
2009-11-14 02:04:38 +00:00
|
|
|
zap_size_t size;
|
|
|
|
unsigned rindex;
|
|
|
|
unsigned windex;
|
|
|
|
void **elements;
|
|
|
|
} zap_queue_t;
|
|
|
|
|
2009-11-21 04:45:22 +00:00
|
|
|
static zap_status_t zap_std_queue_create(zap_queue_t **outqueue, zap_size_t capacity);
|
2009-11-16 15:21:08 +00:00
|
|
|
static zap_status_t zap_std_queue_enqueue(zap_queue_t *queue, void *obj);
|
|
|
|
static void *zap_std_queue_dequeue(zap_queue_t *queue);
|
|
|
|
static zap_status_t zap_std_queue_wait(zap_queue_t *queue, int ms);
|
|
|
|
static zap_status_t zap_std_queue_destroy(zap_queue_t **inqueue);
|
|
|
|
|
|
|
|
OZ_DECLARE_DATA zap_queue_handler_t g_zap_queue_handler =
|
|
|
|
{
|
|
|
|
.create = zap_std_queue_create,
|
|
|
|
.enqueue = zap_std_queue_enqueue,
|
|
|
|
.dequeue = zap_std_queue_dequeue,
|
|
|
|
.wait = zap_std_queue_wait,
|
|
|
|
.destroy = zap_std_queue_destroy
|
|
|
|
};
|
|
|
|
|
|
|
|
OZ_DECLARE(zap_status_t) zap_global_set_queue_handler(zap_queue_handler_t *handler)
|
|
|
|
{
|
|
|
|
if (!handler ||
|
|
|
|
!handler->create ||
|
|
|
|
!handler->enqueue ||
|
|
|
|
!handler->dequeue ||
|
|
|
|
!handler->wait ||
|
|
|
|
!handler->destroy) {
|
|
|
|
return ZAP_FAIL;
|
|
|
|
}
|
|
|
|
memcpy(&g_zap_queue_handler, handler, sizeof(*handler));
|
|
|
|
return ZAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-11-21 04:45:22 +00:00
|
|
|
static zap_status_t zap_std_queue_create(zap_queue_t **outqueue, zap_size_t capacity)
|
2009-11-13 21:28:09 +00:00
|
|
|
{
|
2009-11-14 02:04:38 +00:00
|
|
|
zap_assert(outqueue, ZAP_FAIL, "Queue double pointer is null\n");
|
2009-11-21 04:45:22 +00:00
|
|
|
zap_assert(capacity > 0, ZAP_FAIL, "Queue capacity is not bigger than 0\n");
|
2009-11-14 02:04:38 +00:00
|
|
|
|
|
|
|
*outqueue = NULL;
|
2009-11-15 03:35:01 +00:00
|
|
|
zap_queue_t *queue = zap_calloc(1, sizeof(*queue));
|
2009-11-14 02:04:38 +00:00
|
|
|
if (!queue) {
|
|
|
|
return ZAP_FAIL;
|
|
|
|
}
|
|
|
|
|
2009-11-21 04:45:22 +00:00
|
|
|
queue->elements = zap_calloc(1, (sizeof(void*)*capacity));
|
2009-11-14 02:04:38 +00:00
|
|
|
if (!queue->elements) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2009-11-21 04:45:22 +00:00
|
|
|
queue->capacity = capacity;
|
2009-11-14 02:04:38 +00:00
|
|
|
|
|
|
|
if (zap_mutex_create(&queue->mutex) != ZAP_SUCCESS) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zap_condition_create(&queue->condition, queue->mutex) != ZAP_SUCCESS) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
*outqueue = queue;
|
|
|
|
return ZAP_SUCCESS;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
if (queue) {
|
|
|
|
if (queue->condition) {
|
|
|
|
zap_condition_destroy(&queue->condition);
|
|
|
|
}
|
|
|
|
if (queue->mutex) {
|
|
|
|
zap_mutex_destroy(&queue->mutex);
|
|
|
|
}
|
|
|
|
zap_safe_free(queue->elements);
|
|
|
|
zap_safe_free(queue);
|
|
|
|
}
|
|
|
|
return ZAP_FAIL;
|
2009-11-13 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 15:21:08 +00:00
|
|
|
static zap_status_t zap_std_queue_enqueue(zap_queue_t *queue, void *obj)
|
2009-11-13 21:28:09 +00:00
|
|
|
{
|
2009-11-14 02:04:38 +00:00
|
|
|
zap_status_t status = ZAP_FAIL;
|
|
|
|
|
|
|
|
zap_assert(queue != NULL, ZAP_FAIL, "Queue is null!");
|
|
|
|
|
|
|
|
zap_mutex_lock(queue->mutex);
|
|
|
|
|
2009-11-21 04:45:22 +00:00
|
|
|
if (queue->windex == queue->capacity) {
|
2009-11-14 02:04:38 +00:00
|
|
|
/* try to see if we can wrap around */
|
|
|
|
queue->windex = 0;
|
|
|
|
}
|
|
|
|
|
2009-11-21 04:45:22 +00:00
|
|
|
if (queue->size != 0 && queue->windex == queue->rindex) {
|
2009-11-14 02:04:38 +00:00
|
|
|
zap_log(ZAP_LOG_ERROR, "Failed to enqueue obj %p in queue %p, no more room! windex == rindex == %d!\n", obj, queue, queue->windex);
|
|
|
|
goto done;
|
|
|
|
}
|
2009-11-21 04:45:22 +00:00
|
|
|
|
2009-11-14 02:04:38 +00:00
|
|
|
queue->elements[queue->windex++] = obj;
|
2009-11-21 04:45:22 +00:00
|
|
|
queue->size++;
|
2009-11-14 02:04:38 +00:00
|
|
|
status = ZAP_SUCCESS;
|
2009-11-21 04:45:22 +00:00
|
|
|
|
2009-11-14 02:04:38 +00:00
|
|
|
/* wake up queue reader */
|
|
|
|
zap_condition_signal(queue->condition);
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
zap_mutex_unlock(queue->mutex);
|
|
|
|
|
|
|
|
return status;
|
2009-11-13 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 15:21:08 +00:00
|
|
|
static void *zap_std_queue_dequeue(zap_queue_t *queue)
|
2009-11-13 21:28:09 +00:00
|
|
|
{
|
2009-11-14 02:04:38 +00:00
|
|
|
void *obj = NULL;
|
|
|
|
|
|
|
|
zap_assert(queue != NULL, NULL, "Queue is null!");
|
|
|
|
|
|
|
|
zap_mutex_lock(queue->mutex);
|
|
|
|
|
2009-11-21 04:45:22 +00:00
|
|
|
if (queue->size == 0) {
|
2009-11-14 02:04:38 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2009-11-20 22:57:24 +00:00
|
|
|
|
2009-11-14 02:04:38 +00:00
|
|
|
obj = queue->elements[queue->rindex];
|
2009-11-20 22:57:24 +00:00
|
|
|
queue->elements[queue->rindex++] = NULL;
|
2009-11-21 04:45:22 +00:00
|
|
|
queue->size--;
|
2009-11-14 02:04:38 +00:00
|
|
|
if (queue->rindex == queue->size) {
|
|
|
|
queue->rindex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2009-11-21 04:45:22 +00:00
|
|
|
|
2009-11-14 02:04:38 +00:00
|
|
|
zap_mutex_unlock(queue->mutex);
|
2009-11-21 04:45:22 +00:00
|
|
|
|
2009-11-14 02:04:38 +00:00
|
|
|
return obj;
|
2009-11-13 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 15:21:08 +00:00
|
|
|
static zap_status_t zap_std_queue_wait(zap_queue_t *queue, int ms)
|
2009-11-13 21:28:09 +00:00
|
|
|
{
|
2009-11-20 22:57:24 +00:00
|
|
|
zap_status_t ret;
|
2009-11-14 02:04:38 +00:00
|
|
|
zap_assert(queue != NULL, ZAP_FAIL, "Queue is null!");
|
|
|
|
|
|
|
|
zap_mutex_lock(queue->mutex);
|
|
|
|
|
2009-11-21 04:45:22 +00:00
|
|
|
/* if there is elements in the queue, no need to wait */
|
|
|
|
if (queue->size != 0) {
|
2009-11-14 02:04:38 +00:00
|
|
|
zap_mutex_unlock(queue->mutex);
|
|
|
|
return ZAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-11-21 04:45:22 +00:00
|
|
|
/* no elements on the queue, wait for someone to write an element */
|
2009-11-20 22:57:24 +00:00
|
|
|
ret = zap_condition_wait(queue->condition, ms);
|
2009-11-21 04:45:22 +00:00
|
|
|
|
|
|
|
/* got an element or timeout, bail out */
|
2009-11-14 02:04:38 +00:00
|
|
|
zap_mutex_unlock(queue->mutex);
|
2009-11-21 04:45:22 +00:00
|
|
|
|
2009-11-20 22:57:24 +00:00
|
|
|
return ret;
|
2009-11-13 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 15:21:08 +00:00
|
|
|
static zap_status_t zap_std_queue_destroy(zap_queue_t **inqueue)
|
2009-11-13 21:28:09 +00:00
|
|
|
{
|
2009-11-14 02:04:38 +00:00
|
|
|
zap_queue_t *queue = NULL;
|
|
|
|
zap_assert(inqueue != NULL, ZAP_FAIL, "Queue is null!");
|
|
|
|
zap_assert(*inqueue != NULL, ZAP_FAIL, "Queue is null!");
|
2009-11-13 21:28:09 +00:00
|
|
|
|
2009-11-14 02:04:38 +00:00
|
|
|
queue = *inqueue;
|
|
|
|
zap_condition_destroy(&queue->condition);
|
|
|
|
zap_mutex_destroy(&queue->mutex);
|
|
|
|
zap_safe_free(queue->elements);
|
|
|
|
zap_safe_free(queue);
|
|
|
|
*inqueue = NULL;
|
|
|
|
return ZAP_SUCCESS;
|
2009-11-13 21:28:09 +00:00
|
|
|
}
|
2009-11-14 02:04:38 +00:00
|
|
|
|
2009-11-13 21:28:09 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* indent-tabs-mode:t
|
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
|
|
|
*/
|