From 09f40153002b4fe54bc8130cb624c200deb6b959 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 4 Mar 2019 16:59:09 +0800 Subject: [PATCH] mdns: use binary semaphore instead of mutex when searching mdns_search_once_t::lock is used to synchronize tasks (taken by one task and given by the other) so it should not be a mutex. Convert to semaphore, and rename to indicate its purpose. --- components/mdns/mdns.c | 14 ++++++-------- components/mdns/private_include/mdns_private.h | 5 +---- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 84d578b3c..40b2cc038 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -3074,7 +3074,7 @@ static void _mdns_search_free(mdns_search_once_t * search) free(search->instance); free(search->service); free(search->proto); - vSemaphoreDelete(search->lock); + vSemaphoreDelete(search->done_semaphore); free(search); } @@ -3090,8 +3090,8 @@ static mdns_search_once_t * _mdns_search_init(const char * name, const char * se } memset(search, 0, sizeof(mdns_search_once_t)); - search->lock = xSemaphoreCreateMutex(); - if (!search->lock) { + search->done_semaphore = xSemaphoreCreateBinary(); + if (!search->done_semaphore) { free(search); return NULL; } @@ -3130,8 +3130,6 @@ static mdns_search_once_t * _mdns_search_init(const char * name, const char * se search->started_at = xTaskGetTickCount() * portTICK_PERIOD_MS; search->next = NULL; - xSemaphoreTake(search->lock, 0); - return search; } @@ -3142,7 +3140,7 @@ static void _mdns_search_finish(mdns_search_once_t * search) { search->state = SEARCH_OFF; queueDetach(mdns_search_once_t, _mdns_server->search_once, search); - xSemaphoreGive(search->lock); + xSemaphoreGive(search->done_semaphore); } /** @@ -4148,7 +4146,7 @@ void mdns_free() free(h->instance); free(h->service); free(h->proto); - vSemaphoreDelete(h->lock); + vSemaphoreDelete(h->done_semaphore); if (h->result) { mdns_query_results_free(h->result); } @@ -4543,7 +4541,7 @@ esp_err_t mdns_query(const char * name, const char * service, const char * proto _mdns_search_free(search); return ESP_ERR_NO_MEM; } - xSemaphoreTake(search->lock, portMAX_DELAY); + xSemaphoreTake(search->done_semaphore, portMAX_DELAY); *results = search->result; _mdns_search_free(search); diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index 934b9427e..9efd23ae7 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -115,9 +115,6 @@ #define PCB_STATE_IS_ANNOUNCING(s) (s->state > PCB_PROBE_3 && s->state < PCB_RUNNING) #define PCB_STATE_IS_RUNNING(s) (s->state == PCB_RUNNING) -#define MDNS_SEARCH_LOCK() xSemaphoreTake(_mdns_server->search.lock, portMAX_DELAY) -#define MDNS_SEARCH_UNLOCK() xSemaphoreGive(_mdns_server->search.lock) - #ifndef HOOK_MALLOC_FAILED #define HOOK_MALLOC_FAILED ESP_LOGE(TAG, "Cannot allocate memory (line: %d, free heap: %d bytes)", __LINE__, esp_get_free_heap_size()); #endif @@ -318,7 +315,7 @@ typedef struct mdns_search_once_s { uint32_t started_at; uint32_t sent_at; uint32_t timeout; - SemaphoreHandle_t lock; + SemaphoreHandle_t done_semaphore; uint16_t type; uint8_t max_results; uint8_t num_results;