From 10f8ab08067cdb39fc146d8e2140536f9ea9c403 Mon Sep 17 00:00:00 2001 From: Moises Silva Date: Sat, 21 Nov 2009 04:45:22 +0000 Subject: [PATCH] beautify some code git-svn-id: http://svn.openzap.org/svn/openzap/branches/sangoma_boost@893 a93c3328-9c30-0410-af19-c9cd2b2d52af --- libs/freetdm/src/include/openzap.h | 4 +- .../ozmod_sangoma_boost/ozmod_sangoma_boost.c | 41 +++++++------------ libs/freetdm/src/zap_queue.c | 33 +++++++++------ libs/freetdm/src/zap_threadmutex.c | 5 +-- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/libs/freetdm/src/include/openzap.h b/libs/freetdm/src/include/openzap.h index 4547951cd3..1b50a6d31b 100644 --- a/libs/freetdm/src/include/openzap.h +++ b/libs/freetdm/src/include/openzap.h @@ -620,7 +620,7 @@ struct zap_io_interface { struct zap_queue; #define zap_queue_t struct zap_queue -typedef zap_status_t (*zap_queue_create_func_t)(zap_queue_t **queue, zap_size_t); +typedef zap_status_t (*zap_queue_create_func_t)(zap_queue_t **queue, zap_size_t capacity); typedef zap_status_t (*zap_queue_enqueue_func_t)(zap_queue_t *queue, void *obj); typedef void *(*zap_queue_dequeue_func_t)(zap_queue_t *queue); typedef zap_status_t (*zap_queue_wait_func_t)(zap_queue_t *queue, int ms); @@ -635,7 +635,7 @@ typedef struct zap_queue_handler { OZ_DECLARE_DATA extern zap_queue_handler_t g_zap_queue_handler; /*! brief create a new queue */ -#define zap_queue_create(queue, size) g_zap_queue_handler.create(queue, size) +#define zap_queue_create(queue, capacity) g_zap_queue_handler.create(queue, capacity) /*! Enqueue an object */ #define zap_queue_enqueue(queue, obj) g_zap_queue_handler.enqueue(queue, obj) diff --git a/libs/freetdm/src/ozmod/ozmod_sangoma_boost/ozmod_sangoma_boost.c b/libs/freetdm/src/ozmod/ozmod_sangoma_boost/ozmod_sangoma_boost.c index 3a2752cddb..a33b1cc351 100644 --- a/libs/freetdm/src/ozmod/ozmod_sangoma_boost/ozmod_sangoma_boost.c +++ b/libs/freetdm/src/ozmod/ozmod_sangoma_boost/ozmod_sangoma_boost.c @@ -1164,6 +1164,7 @@ static zap_status_t zap_boost_connection_open(zap_span_t *span) sangoma_boost_data->pcon = sangoma_boost_data->mcon; + /* when sigmod is present, all arguments: local_ip etc, are ignored by sangomabc_connection_open */ if (sangomabc_connection_open(&sangoma_boost_data->mcon, sangoma_boost_data->mcon.cfg.local_ip, sangoma_boost_data->mcon.cfg.local_port, @@ -1196,10 +1197,14 @@ static int zap_boost_wait_event(zap_span_t *span, int ms) zap_sangoma_boost_data_t *sangoma_boost_data = span->signal_data; if (sangoma_boost_data->sigmod) { - int result; - result = zap_queue_wait(sangoma_boost_data->boost_queue, ms); - if (result == ZAP_TIMEOUT) return 0; - if (result != ZAP_SUCCESS) return -1; + zap_status_t res; + res = zap_queue_wait(sangoma_boost_data->boost_queue, ms); + if (ZAP_TIMEOUT == res) { + return 0; + } + if (ZAP_SUCCESS != res) { + return -1; + } return 1; } mcon = &sangoma_boost_data->mcon; @@ -1273,7 +1278,7 @@ static void *zap_sangoma_boost_run(zap_thread_t *me, void *obj) } if (zap_boost_connection_open(span) != ZAP_SUCCESS) { - zap_log(ZAP_LOG_ERROR, "zap_boost_connection failed\n"); + zap_log(ZAP_LOG_ERROR, "zap_boost_connection_open failed\n"); goto end; } @@ -1299,46 +1304,28 @@ static void *zap_sangoma_boost_run(zap_thread_t *me, void *obj) SIGBOOST_EVENT_SYSTEM_RESTART, 0); zap_set_flag(mcon, MSU_FLAG_DOWN); - zap_log(ZAP_LOG_DEBUG, "OPENZAP is no longer running\n"); + zap_log(ZAP_LOG_DEBUG, "zap is no longer running\n"); break; } if ((activity = zap_boost_wait_event(span, ms)) < 0) { - zap_log(ZAP_LOG_ERROR, "Zap boost waitevent failed\n"); + zap_log(ZAP_LOG_ERROR, "zap_boost_wait_event failed\n"); goto error; } if (activity) { - while ((event = zap_boost_read_event(span))) { parse_sangoma_event(span, pcon, (sangomabc_short_event_t*)event); sangoma_boost_data->iteration++; } - } - pcon->hb_elapsed += ms; if (zap_test_flag(span, ZAP_SPAN_SUSPENDED) || zap_test_flag(mcon, MSU_FLAG_DOWN)) { pcon->hb_elapsed = 0; } - -#if 0 - if (pcon->hb_elapsed >= too_long) { - zap_log(ZAP_LOG_CRIT, "Lost Heartbeat!\n"); - zap_set_flag_locked(span, ZAP_SPAN_SUSPENDED); - zap_set_flag(mcon, MSU_FLAG_DOWN); - sangomabc_exec_commandp(pcon, - 0, - 0, - -1, - SIGBOOST_EVENT_SYSTEM_RESTART, - 0); - } -#endif - if (zap_running()) { check_state(span); } @@ -1346,10 +1333,10 @@ static void *zap_sangoma_boost_run(zap_thread_t *me, void *obj) goto end; - error: +error: zap_log(ZAP_LOG_CRIT, "Boost event processing Error!\n"); - end: +end: if (!sangoma_boost_data->sigmod) { sangomabc_connection_close(&sangoma_boost_data->mcon); sangomabc_connection_close(&sangoma_boost_data->pcon); diff --git a/libs/freetdm/src/zap_queue.c b/libs/freetdm/src/zap_queue.c index e04707537b..7f40b036df 100644 --- a/libs/freetdm/src/zap_queue.c +++ b/libs/freetdm/src/zap_queue.c @@ -38,14 +38,14 @@ typedef struct zap_queue { zap_mutex_t *mutex; zap_condition_t *condition; + zap_size_t capacity; zap_size_t size; - zap_size_t num_elements; unsigned rindex; unsigned windex; void **elements; } zap_queue_t; -static zap_status_t zap_std_queue_create(zap_queue_t **outqueue, zap_size_t size); +static zap_status_t zap_std_queue_create(zap_queue_t **outqueue, zap_size_t capacity); 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); @@ -74,10 +74,10 @@ OZ_DECLARE(zap_status_t) zap_global_set_queue_handler(zap_queue_handler_t *handl return ZAP_SUCCESS; } -static zap_status_t zap_std_queue_create(zap_queue_t **outqueue, zap_size_t size) +static zap_status_t zap_std_queue_create(zap_queue_t **outqueue, zap_size_t capacity) { zap_assert(outqueue, ZAP_FAIL, "Queue double pointer is null\n"); - zap_assert(size > 0, ZAP_FAIL, "Queue size is not bigger than 0\n"); + zap_assert(capacity > 0, ZAP_FAIL, "Queue capacity is not bigger than 0\n"); *outqueue = NULL; zap_queue_t *queue = zap_calloc(1, sizeof(*queue)); @@ -85,11 +85,11 @@ static zap_status_t zap_std_queue_create(zap_queue_t **outqueue, zap_size_t size return ZAP_FAIL; } - queue->elements = zap_calloc(1, (sizeof(void*)*size)); + queue->elements = zap_calloc(1, (sizeof(void*)*capacity)); if (!queue->elements) { goto failed; } - queue->size = size; + queue->capacity = capacity; if (zap_mutex_create(&queue->mutex) != ZAP_SUCCESS) { goto failed; @@ -124,18 +124,20 @@ static zap_status_t zap_std_queue_enqueue(zap_queue_t *queue, void *obj) zap_mutex_lock(queue->mutex); - if (queue->windex == queue->size) { + if (queue->windex == queue->capacity) { /* try to see if we can wrap around */ queue->windex = 0; } - if (queue->num_elements != 0 && queue->windex == queue->rindex) { + if (queue->size != 0 && queue->windex == queue->rindex) { 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; } + queue->elements[queue->windex++] = obj; + queue->size++; status = ZAP_SUCCESS; - queue->num_elements++; + /* wake up queue reader */ zap_condition_signal(queue->condition); @@ -154,19 +156,21 @@ static void *zap_std_queue_dequeue(zap_queue_t *queue) zap_mutex_lock(queue->mutex); - if (queue->num_elements == 0) { + if (queue->size == 0) { goto done; } obj = queue->elements[queue->rindex]; queue->elements[queue->rindex++] = NULL; - queue->num_elements--; + queue->size--; if (queue->rindex == queue->size) { queue->rindex = 0; } done: + zap_mutex_unlock(queue->mutex); + return obj; } @@ -177,13 +181,18 @@ static zap_status_t zap_std_queue_wait(zap_queue_t *queue, int ms) zap_mutex_lock(queue->mutex); - if (queue->elements[queue->rindex]) { + /* if there is elements in the queue, no need to wait */ + if (queue->size != 0) { zap_mutex_unlock(queue->mutex); return ZAP_SUCCESS; } + /* no elements on the queue, wait for someone to write an element */ ret = zap_condition_wait(queue->condition, ms); + + /* got an element or timeout, bail out */ zap_mutex_unlock(queue->mutex); + return ret; } diff --git a/libs/freetdm/src/zap_threadmutex.c b/libs/freetdm/src/zap_threadmutex.c index a6e9dc26c8..e0744158a7 100644 --- a/libs/freetdm/src/zap_threadmutex.c +++ b/libs/freetdm/src/zap_threadmutex.c @@ -292,9 +292,8 @@ OZ_DECLARE(zap_status_t) zap_condition_wait(zap_condition_t *condition, int ms) int res = 0; if (ms > 0) { struct timespec waitms; - waitms.tv_sec = time(NULL)+(ms/1000); - waitms.tv_nsec = 1000*1000*(ms%1000); - + waitms.tv_sec = time(NULL) + ( ms / 1000 ); + waitms.tv_nsec = 1000 * 1000 * ( ms % 1000 ); res = pthread_cond_timedwait(&condition->condition, condition->mutex, &waitms); } else { res = pthread_cond_wait(&condition->condition, condition->mutex);