2021-04-12 08:02:59 +02:00
|
|
|
/*
|
2024-05-02 12:24:23 +03:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2021-04-12 08:02:59 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
2019-11-21 18:40:59 +01:00
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
#include "freertos/semphr.h"
|
2021-03-11 20:17:21 +11:00
|
|
|
#include "esp_compiler.h"
|
2024-02-28 20:33:12 +02:00
|
|
|
#include "esp_private/log_lock.h"
|
2019-11-21 18:40:59 +01:00
|
|
|
|
|
|
|
|
// Maximum time to wait for the mutex in a logging statement.
|
2021-03-11 20:17:21 +11:00
|
|
|
//
|
|
|
|
|
// We don't expect this to happen in most cases, as contention is low. The most likely case is if a
|
|
|
|
|
// log function is called from an ISR (technically caller should use the ISR-friendly logging macros but
|
|
|
|
|
// possible they use the normal one instead and disable the log type by tag).
|
2019-11-21 18:40:59 +01:00
|
|
|
#define MAX_MUTEX_WAIT_MS 10
|
|
|
|
|
#define MAX_MUTEX_WAIT_TICKS ((MAX_MUTEX_WAIT_MS + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS)
|
|
|
|
|
|
|
|
|
|
static SemaphoreHandle_t s_log_mutex = NULL;
|
|
|
|
|
|
|
|
|
|
void esp_log_impl_lock(void)
|
|
|
|
|
{
|
2021-03-11 20:17:21 +11:00
|
|
|
if (unlikely(!s_log_mutex)) {
|
2019-11-21 18:40:59 +01:00
|
|
|
s_log_mutex = xSemaphoreCreateMutex();
|
|
|
|
|
}
|
2021-03-11 20:17:21 +11:00
|
|
|
if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-11-21 18:40:59 +01:00
|
|
|
xSemaphoreTake(s_log_mutex, portMAX_DELAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool esp_log_impl_lock_timeout(void)
|
|
|
|
|
{
|
2021-03-11 20:17:21 +11:00
|
|
|
if (unlikely(!s_log_mutex)) {
|
2019-11-21 18:40:59 +01:00
|
|
|
s_log_mutex = xSemaphoreCreateMutex();
|
|
|
|
|
}
|
2021-03-11 20:17:21 +11:00
|
|
|
if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-11-21 18:40:59 +01:00
|
|
|
return xSemaphoreTake(s_log_mutex, MAX_MUTEX_WAIT_TICKS) == pdTRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void esp_log_impl_unlock(void)
|
|
|
|
|
{
|
2021-03-11 20:17:21 +11:00
|
|
|
if (unlikely(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-11-21 18:40:59 +01:00
|
|
|
xSemaphoreGive(s_log_mutex);
|
|
|
|
|
}
|