mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-31 03:07:21 +02:00
pthread: Fix pthread_once to use atomic compare and set
Earlier recursive mutex was being used for this but since SCOMPARE1 is already being saved/restored during context switch, atomic compare and set can be used for this. Signed-off-by: Mahavir Jain <mahavir@espressif.com>
This commit is contained in:
@ -62,7 +62,6 @@ typedef struct {
|
|||||||
} esp_pthread_mutex_t;
|
} esp_pthread_mutex_t;
|
||||||
|
|
||||||
|
|
||||||
static SemaphoreHandle_t s_once_mux = NULL;
|
|
||||||
static SemaphoreHandle_t s_threads_mux = NULL;
|
static SemaphoreHandle_t s_threads_mux = NULL;
|
||||||
static portMUX_TYPE s_mutex_init_lock = portMUX_INITIALIZER_UNLOCKED;
|
static portMUX_TYPE s_mutex_init_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||||
|
|
||||||
@ -74,13 +73,8 @@ static int IRAM_ATTR pthread_mutex_lock_internal(esp_pthread_mutex_t *mux, TickT
|
|||||||
esp_err_t esp_pthread_init(void)
|
esp_err_t esp_pthread_init(void)
|
||||||
{
|
{
|
||||||
vListInitialise((List_t *)&s_threads_list);
|
vListInitialise((List_t *)&s_threads_list);
|
||||||
s_once_mux = xSemaphoreCreateRecursiveMutex();
|
|
||||||
if (s_once_mux == NULL) {
|
|
||||||
return ESP_ERR_NO_MEM;
|
|
||||||
}
|
|
||||||
s_threads_mux = xSemaphoreCreateMutex();
|
s_threads_mux = xSemaphoreCreateMutex();
|
||||||
if (s_threads_mux == NULL) {
|
if (s_threads_mux == NULL) {
|
||||||
vSemaphoreDelete(s_once_mux);
|
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@ -346,30 +340,18 @@ int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
|
|||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
|
// Check if once_control belongs to internal DRAM for uxPortCompare to succeed
|
||||||
uint8_t do_execute = 0;
|
if (!esp_ptr_internal(once_control)) {
|
||||||
// do not take mutex if OS is not running yet
|
ESP_LOGE(TAG, "%s: once_control should belong to internal DRAM region!", __FUNCTION__);
|
||||||
if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED ||
|
return EINVAL;
|
||||||
// init_routine can call pthread_once for another objects, so use recursive mutex
|
|
||||||
// FIXME: behaviour is undefined if init_routine calls pthread_once for the same object in the current context
|
|
||||||
!cur_task || xSemaphoreTakeRecursive(s_once_mux, portMAX_DELAY) == pdTRUE)
|
|
||||||
{
|
|
||||||
if (!once_control->init_executed) {
|
|
||||||
do_execute = 1;
|
|
||||||
once_control->init_executed = 1;
|
|
||||||
}
|
|
||||||
if (cur_task) {
|
|
||||||
xSemaphoreGiveRecursive(s_once_mux);
|
|
||||||
}
|
|
||||||
if (do_execute) {
|
|
||||||
ESP_LOGV(TAG, "%s: call init_routine %p", __FUNCTION__, once_control);
|
|
||||||
init_routine();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
uint32_t res = 1;
|
||||||
ESP_LOGE(TAG, "%s: Failed to lock!", __FUNCTION__);
|
uxPortCompareSet((uint32_t *) &once_control->init_executed, 0, &res);
|
||||||
return EBUSY;
|
// Check if compare and set was successful
|
||||||
|
if (res == 0) {
|
||||||
|
ESP_LOGV(TAG, "%s: call init_routine %p", __FUNCTION__, once_control);
|
||||||
|
init_routine();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Reference in New Issue
Block a user