From 471013f7e09635339f8165b3bfd75f1ee6384b61 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 e524a3130..cf9d0c23a 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -3098,7 +3098,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); } @@ -3114,8 +3114,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; } @@ -3154,8 +3154,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; } @@ -3166,7 +3164,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); } /** @@ -4192,7 +4190,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); } @@ -4582,7 +4580,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 da5162348..269a5695d 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -112,9 +112,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 @@ -316,7 +313,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;