Преглед изворни кода

Synchronize GPIO map rebuild and teardown with HTTP and worker access

SybexXx пре 3 дана
родитељ
комит
4608400579
1 измењених фајлова са 15 додато и 7 уклоњено
  1. 15 7
      code/components/jomjol_controlGPIO/server_GPIO.cpp

+ 15 - 7
code/components/jomjol_controlGPIO/server_GPIO.cpp

@@ -259,12 +259,15 @@ void GpioHandler::init()
     // Stop any previous task, queue and pin callbacks before rebuilding them.
     deinit();
 
-    if (gpioMap == NULL) {
-        gpioMap = new (std::nothrow) std::map<gpio_num_t, GpioPin*>();
+    {
+        std::lock_guard<std::recursive_mutex> lock(_mapMutex);
         if (gpioMap == NULL) {
-            LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GPIO map");
-            _isEnabled = false;
-            return;
+            gpioMap = new (std::nothrow) std::map<gpio_num_t, GpioPin*>();
+            if (gpioMap == NULL) {
+                LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GPIO map");
+                _isEnabled = false;
+                return;
+            }
         }
     }
 
@@ -359,7 +362,7 @@ void GpioHandler::init()
 }
 
 void GpioHandler::taskHandler() {
-    std::lock_guard<std::mutex> lock(_mapMutex);
+    std::lock_guard<std::recursive_mutex> lock(_mapMutex);
     if (gpioMap != NULL) {
         for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
             if ((it->second->getInterruptType() == GPIO_INTR_DISABLE))
@@ -408,7 +411,10 @@ void GpioHandler::deinit() {
         leds_global = NULL;
     }
 #endif
-    clear();
+    {
+        std::lock_guard<std::recursive_mutex> lock(_mapMutex);
+        clear();
+    }
 
     if (_workerStopped != NULL) {
         vSemaphoreDelete(_workerStopped);
@@ -461,6 +467,7 @@ void GpioHandler::gpioInterrupt(GpioResult* gpioResult) {
 
 bool GpioHandler::readConfig() 
 {
+    std::lock_guard<std::recursive_mutex> lock(_mapMutex);
     if (!gpioMap->empty())
         clear();
 
@@ -624,6 +631,7 @@ bool GpioHandler::readConfig()
 
 void GpioHandler::clear() 
 {
+    std::lock_guard<std::recursive_mutex> lock(_mapMutex);
     ESP_LOGD(TAG, "GpioHandler::clear");
 
     if (gpioMap != NULL) {