Skip to content

Commit

Permalink
mdns: use binary semaphore instead of mutex when searching
Browse files Browse the repository at this point in the history
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.


* Original commit: espressif/esp-idf@eef0b50
  • Loading branch information
igrr authored and suren-gabrielyan-espressif committed May 27, 2022
1 parent b6efc68 commit 34f6d8d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
14 changes: 6 additions & 8 deletions components/mdns/mdns.c
Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions components/mdns/private_include/mdns_private.h
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 34f6d8d

Please sign in to comment.