Add TasAutoMutex

This commit is contained in:
Theo Arends 2021-01-01 17:04:36 +01:00
parent ee146b2e29
commit 628f17de8c
3 changed files with 38 additions and 43 deletions

View File

@ -111,7 +111,7 @@ String GetResetReason(void)
} }
} }
#ifdef ESP32
/*********************************************************************************************\ /*********************************************************************************************\
* ESP32 AutoMutex * ESP32 AutoMutex
\*********************************************************************************************/ \*********************************************************************************************/
@ -128,9 +128,7 @@ String GetResetReason(void)
// - the same thread can take multiple times (recursive). // - the same thread can take multiple times (recursive).
// - advanced options m.give() and m.take() allow you fine control within a function. // - advanced options m.give() and m.take() allow you fine control within a function.
class TasAutoMutex { class TasAutoMutex {
#ifdef ESP32
SemaphoreHandle_t mutex; SemaphoreHandle_t mutex;
#endif
bool taken; bool taken;
public: public:
TasAutoMutex(void * mutex, bool take=true); TasAutoMutex(void * mutex, bool take=true);
@ -142,7 +140,6 @@ class TasAutoMutex {
////////////////////////////////////////// //////////////////////////////////////////
TasAutoMutex::TasAutoMutex(void * mutex, bool take) { TasAutoMutex::TasAutoMutex(void * mutex, bool take) {
#ifdef ESP32
if (mutex) { if (mutex) {
if (take) { if (take) {
xSemaphoreTakeRecursive(mutex, portMAX_DELAY); xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
@ -152,52 +149,43 @@ TasAutoMutex::TasAutoMutex(void * mutex, bool take){
} else { } else {
this->mutex = (SemaphoreHandle_t)nullptr; this->mutex = (SemaphoreHandle_t)nullptr;
} }
#endif
} }
TasAutoMutex::~TasAutoMutex() { TasAutoMutex::~TasAutoMutex() {
#ifdef ESP32
if (this->mutex) { if (this->mutex) {
if (this->taken) { if (this->taken) {
xSemaphoreGiveRecursive(this->mutex); xSemaphoreGiveRecursive(this->mutex);
this->taken = false; this->taken = false;
} }
} }
#endif
} }
void TasAutoMutex::init(void ** ptr) { void TasAutoMutex::init(void ** ptr) {
#ifdef ESP32
SemaphoreHandle_t mutex = xSemaphoreCreateRecursiveMutex(); SemaphoreHandle_t mutex = xSemaphoreCreateRecursiveMutex();
(*ptr) = (void *) mutex; (*ptr) = (void *) mutex;
#else // needed, else for ESP8266 as we will initialis more than once in logging
// needed, else we will initialis more than once in logging // (*ptr) = (void *) 1;
(*ptr) = (void *) 1;
#endif
} }
void TasAutoMutex::give() { void TasAutoMutex::give() {
#ifdef ESP32
if (this->mutex) { if (this->mutex) {
if (this->taken) { if (this->taken) {
xSemaphoreGiveRecursive(this->mutex); xSemaphoreGiveRecursive(this->mutex);
this->taken= false; this->taken= false;
} }
} }
#endif
} }
void TasAutoMutex::take() { void TasAutoMutex::take() {
#ifdef ESP32
if (this->mutex) { if (this->mutex) {
if (!this->taken) { if (!this->taken) {
xSemaphoreTakeRecursive(this->mutex, portMAX_DELAY); xSemaphoreTakeRecursive(this->mutex, portMAX_DELAY);
this->taken = true; this->taken = true;
} }
} }
#endif
} }
#endif // ESP32
/*********************************************************************************************\ /*********************************************************************************************\
* Miscellaneous * Miscellaneous
@ -2054,9 +2042,12 @@ void SyslogAsync(bool refresh) {
} }
bool NeedLogRefresh(uint32_t req_loglevel, uint32_t index) { bool NeedLogRefresh(uint32_t req_loglevel, uint32_t index) {
#ifdef ESP32
// this takes the mutex, and will be release when the class is destroyed - // this takes the mutex, and will be release when the class is destroyed -
// i.e. when the functon leaves You CAN call mutex.give() to leave early. // i.e. when the functon leaves You CAN call mutex.give() to leave early.
TasAutoMutex mutex(TasmotaGlobal.log_buffer_mutex); TasAutoMutex mutex(TasmotaGlobal.log_buffer_mutex);
#endif // ESP32
// Skip initial buffer fill // Skip initial buffer fill
if (strlen(TasmotaGlobal.log_buffer) < LOG_BUFFER_SIZE - LOGSZ) { return false; } if (strlen(TasmotaGlobal.log_buffer) < LOG_BUFFER_SIZE - LOGSZ) { return false; }
@ -2073,9 +2064,11 @@ bool GetLog(uint32_t req_loglevel, uint32_t* index_p, char** entry_pp, size_t* l
if (TasmotaGlobal.uptime < 3) { return false; } // Allow time to setup correct log level if (TasmotaGlobal.uptime < 3) { return false; } // Allow time to setup correct log level
if (!req_loglevel || (index == TasmotaGlobal.log_buffer_pointer)) { return false; } if (!req_loglevel || (index == TasmotaGlobal.log_buffer_pointer)) { return false; }
#ifdef ESP32
// this takes the mutex, and will be release when the class is destroyed - // this takes the mutex, and will be release when the class is destroyed -
// i.e. when the functon leaves You CAN call mutex.give() to leave early. // i.e. when the functon leaves You CAN call mutex.give() to leave early.
TasAutoMutex mutex(TasmotaGlobal.log_buffer_mutex); TasAutoMutex mutex(TasmotaGlobal.log_buffer_mutex);
#endif // ESP32
if (!index) { // Dump all if (!index) { // Dump all
index = TasmotaGlobal.log_buffer_pointer +1; index = TasmotaGlobal.log_buffer_pointer +1;
@ -2116,16 +2109,17 @@ bool GetLog(uint32_t req_loglevel, uint32_t* index_p, char** entry_pp, size_t* l
void AddLogData(uint32_t loglevel, const char* log_data) { void AddLogData(uint32_t loglevel, const char* log_data) {
#ifdef ESP32
if (!TasmotaGlobal.log_buffer_mutex) { if (!TasmotaGlobal.log_buffer_mutex) {
TasAutoMutex::init(&TasmotaGlobal.log_buffer_mutex); TasAutoMutex::init(&TasmotaGlobal.log_buffer_mutex);
} }
char mxtime[14]; // "13:45:21.999 "
snprintf_P(mxtime, sizeof(mxtime), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d.%03d "), RtcTime.hour, RtcTime.minute, RtcTime.second, RtcMillis());
// this takes the mutex, and will be release when the class is destroyed - // this takes the mutex, and will be release when the class is destroyed -
// i.e. when the functon leaves You CAN call mutex.give() to leave early. // i.e. when the functon leaves You CAN call mutex.give() to leave early.
TasAutoMutex mutex(TasmotaGlobal.log_buffer_mutex); TasAutoMutex mutex(TasmotaGlobal.log_buffer_mutex);
#endif // ESP32
char mxtime[14]; // "13:45:21.999 "
snprintf_P(mxtime, sizeof(mxtime), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d.%03d "), RtcTime.hour, RtcTime.minute, RtcTime.second, RtcMillis());
if ((loglevel <= TasmotaGlobal.seriallog_level) && if ((loglevel <= TasmotaGlobal.seriallog_level) &&
(TasmotaGlobal.masterlog_level <= TasmotaGlobal.seriallog_level)) { (TasmotaGlobal.masterlog_level <= TasmotaGlobal.seriallog_level)) {

View File

@ -413,4 +413,5 @@ uint8_t* FlashDirectAccess(void) {
*/ */
return data; return data;
} }
#endif // ESP32 #endif // ESP32

View File

@ -89,6 +89,7 @@ struct {
uint32_t log_buffer_pointer; // Index in log buffer uint32_t log_buffer_pointer; // Index in log buffer
uint32_t uptime; // Counting every second until 4294967295 = 130 year uint32_t uptime; // Counting every second until 4294967295 = 130 year
GpioOptionABits gpio_optiona; // GPIO Option_A flags GpioOptionABits gpio_optiona; // GPIO Option_A flags
void *log_buffer_mutex; // Control access to log buffer
power_t power; // Current copy of Settings.power power_t power; // Current copy of Settings.power
power_t rel_inverted; // Relay inverted flag (1 = (0 = On, 1 = Off)) power_t rel_inverted; // Relay inverted flag (1 = (0 = On, 1 = Off))
@ -171,7 +172,6 @@ struct {
char mqtt_topic[TOPSZ]; // Composed MQTT topic char mqtt_topic[TOPSZ]; // Composed MQTT topic
char mqtt_data[MESSZ]; // MQTT publish buffer and web page ajax buffer char mqtt_data[MESSZ]; // MQTT publish buffer and web page ajax buffer
char log_buffer[LOG_BUFFER_SIZE]; // Web log buffer char log_buffer[LOG_BUFFER_SIZE]; // Web log buffer
void *log_buffer_mutex; // control, access to log buffer
} TasmotaGlobal; } TasmotaGlobal;
#ifdef SUPPORT_IF_STATEMENT #ifdef SUPPORT_IF_STATEMENT