From: wangmengyang Date: Mon, 10 Oct 2016 12:37:26 +0000 (+0800) Subject: recover semaphore in fixed_queue to control the queue capacity X-Git-Tag: v1.0~2^2~73 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0b30c22162b8b9dc8a25f05d058cd5230007f219;p=esp-idf recover semaphore in fixed_queue to control the queue capacity --- diff --git a/components/bt/bluedroid/osi/fixed_queue.c b/components/bt/bluedroid/osi/fixed_queue.c index 4d63365b80..fa5a752a7d 100755 --- a/components/bt/bluedroid/osi/fixed_queue.c +++ b/components/bt/bluedroid/osi/fixed_queue.c @@ -21,13 +21,14 @@ #include "fixed_queue.h" #include "list.h" #include "osi.h" +#include "osi_arch.h" #include "bt_trace.h" typedef struct fixed_queue_t { list_t *list; - //semaphore_t *enqueue_sem; - //semaphore_t *dequeue_sem; + osi_sem_t enqueue_sem; + osi_sem_t dequeue_sem; pthread_mutex_t lock; size_t capacity; @@ -53,15 +54,15 @@ fixed_queue_t *fixed_queue_new(size_t capacity) { if (!ret->list) goto error; -/* - ret->enqueue_sem = semaphore_new(capacity); + + osi_sem_new(&ret->enqueue_sem, capacity, capacity); if (!ret->enqueue_sem) goto error; - ret->dequeue_sem = semaphore_new(0); + osi_sem_new(&ret->dequeue_sem, capacity, 0); if (!ret->dequeue_sem) goto error; -*/ + return ret; error:; @@ -81,8 +82,8 @@ void fixed_queue_free(fixed_queue_t *queue, fixed_queue_free_cb free_cb) { free_cb(list_node(node)); list_free(queue->list); -// semaphore_free(queue->enqueue_sem); -// semaphore_free(queue->dequeue_sem); + osi_sem_free(&queue->enqueue_sem); + osi_sem_free(&queue->dequeue_sem); pthread_mutex_destroy(&queue->lock); osi_free(queue); } @@ -108,28 +109,28 @@ void fixed_queue_enqueue(fixed_queue_t *queue, void *data) { assert(queue != NULL); assert(data != NULL); -// semaphore_wait(queue->enqueue_sem); - + osi_sem_wait(&queue->enqueue_sem, 0); + pthread_mutex_lock(&queue->lock); list_append(queue->list, data); pthread_mutex_unlock(&queue->lock); -// semaphore_post(queue->dequeue_sem); + osi_sem_signal(&queue->dequeue_sem); } void *fixed_queue_dequeue(fixed_queue_t *queue) { void *ret = NULL; assert(queue != NULL); -// semaphore_wait(queue->dequeue_sem); + osi_sem_wait(&queue->dequeue_sem, 0); pthread_mutex_lock(&queue->lock); ret = list_front(queue->list); list_remove(queue->list, ret); pthread_mutex_unlock(&queue->lock); -// semaphore_post(queue->enqueue_sem); + osi_sem_signal(&queue->enqueue_sem); return ret; } diff --git a/components/bt/bluedroid/osi/include/osi_arch.h b/components/bt/bluedroid/osi/include/osi_arch.h index 85e60a6978..491d48296c 100755 --- a/components/bt/bluedroid/osi/include/osi_arch.h +++ b/components/bt/bluedroid/osi/include/osi_arch.h @@ -26,7 +26,7 @@ void osi_mutex_unlock(osi_mutex_t *pxMutex); void osi_mutex_free(osi_mutex_t *pxMutex); -int osi_sem_new(osi_sem_t *sem, uint8_t count); +int osi_sem_new(osi_sem_t *sem, uint32_t max_count, uint32_t init_count); void osi_sem_signal(osi_sem_t *sem); diff --git a/components/bt/bluedroid/osi/osi_arch.c b/components/bt/bluedroid/osi/osi_arch.c index 1eda38f99f..0e8cd9639c 100755 --- a/components/bt/bluedroid/osi/osi_arch.c +++ b/components/bt/bluedroid/osi/osi_arch.c @@ -84,22 +84,18 @@ osi_mutex_free(osi_mutex_t *pxMutex) } /*-----------------------------------------------------------------------------------*/ -// Creates and returns a new semaphore. The "count" argument specifies -// the initial state of the semaphore. TBD finish and test +// Creates and returns a new semaphore. The "init_count" argument specifies +// the initial state of the semaphore, "max_count" specifies the maximum value +// that can be reached. int -osi_sem_new(osi_sem_t *sem, uint8_t count) +osi_sem_new(osi_sem_t *sem, uint32_t max_count, uint32_t init_count) { int xReturn = -1; - vSemaphoreCreateBinary(*sem); - - if ((*sem) != NULL) { - if (count == 0) { // Means it can't be taken - xSemaphoreTake(*sem, 1); + if (sem) { + *sem = xSemaphoreCreateCounting(max_count, init_count); + if ((*sem) != NULL) { + xReturn = 0; } - - xReturn = 0; - } else { - ; // TBD need assert } return xReturn;