]> granicus.if.org Git - esp-idf/commitdiff
esp32/Add mutex for dual core hooks
authorDarian Leung <darian@espressif.com>
Mon, 16 Oct 2017 03:47:38 +0000 (11:47 +0800)
committerDarian Leung <darian@espressif.com>
Mon, 16 Oct 2017 03:47:38 +0000 (11:47 +0800)
This commit adds critical sections for the dual core hooks

components/esp32/freertos_hooks.c

index 599bc196207694364977e1281e4746f1a4417613..ed88be88358ab2b1c6881cee38be231f22df9238 100644 (file)
@@ -24,6 +24,7 @@
 //an idle or tick hook.
 #define MAX_HOOKS 8
 
+static portMUX_TYPE hooks_spinlock = portMUX_INITIALIZER_UNLOCKED;
 static esp_freertos_idle_cb_t idle_cb[portNUM_PROCESSORS][MAX_HOOKS]={0};
 static esp_freertos_tick_cb_t tick_cb[portNUM_PROCESSORS][MAX_HOOKS]={0};
 
@@ -61,12 +62,15 @@ esp_err_t esp_register_freertos_idle_hook_for_cpu(esp_freertos_idle_cb_t new_idl
     if(cpuid >= portNUM_PROCESSORS){
         return ESP_ERR_INVALID_ARG;
     }
+    portENTER_CRITICAL(&hooks_spinlock);
     for(int n = 0; n < MAX_HOOKS; n++){
         if (idle_cb[cpuid][n]==NULL) {
             idle_cb[cpuid][n]=new_idle_cb;
+            portEXIT_CRITICAL(&hooks_spinlock);
             return ESP_OK;
         }
     }
+    portEXIT_CRITICAL(&hooks_spinlock);
     return ESP_ERR_NO_MEM;
 }
 
@@ -80,12 +84,15 @@ esp_err_t esp_register_freertos_tick_hook_for_cpu(esp_freertos_tick_cb_t new_tic
     if(cpuid >= portNUM_PROCESSORS){
         return ESP_ERR_INVALID_ARG;
     }
+    portENTER_CRITICAL(&hooks_spinlock);
     for(int n = 0; n < MAX_HOOKS; n++){
         if (tick_cb[cpuid][n]==NULL) {
             tick_cb[cpuid][n]=new_tick_cb;
+            portEXIT_CRITICAL(&hooks_spinlock);
             return ESP_OK;
         }
     }
+    portEXIT_CRITICAL(&hooks_spinlock);
     return ESP_ERR_NO_MEM;
 }
 
@@ -96,19 +103,23 @@ esp_err_t esp_register_freertos_tick_hook(esp_freertos_tick_cb_t new_tick_cb)
 
 void esp_deregister_freertos_idle_hook(esp_freertos_idle_cb_t old_idle_cb)
 {
+    portENTER_CRITICAL(&hooks_spinlock);
     for(int m = 0; m < portNUM_PROCESSORS; m++) {
         for(int n = 0; n < MAX_HOOKS; n++){
             if(idle_cb[m][n] == old_idle_cb) idle_cb[m][n] = NULL;
         }
     }
+    portEXIT_CRITICAL(&hooks_spinlock);
 }
 
 void esp_deregister_freertos_tick_hook(esp_freertos_tick_cb_t old_tick_cb)
 {
+    portENTER_CRITICAL(&hooks_spinlock);
     for(int m = 0; m < portNUM_PROCESSORS; m++){
         for(int n = 0; n < MAX_HOOKS; n++){
             if(tick_cb[m][n] == old_tick_cb) tick_cb[m][n] = NULL;
         }
     }
+    portEXIT_CRITICAL(&hooks_spinlock);
 }