Quellcode durchsuchen

freertos: Fixes hangup upon pthread_join on finished thread

Alexey Gerenkov vor 8 Jahren
Ursprung
Commit
54a529f596
2 geänderte Dateien mit 24 neuen und 1 gelöschten Zeilen
  1. 3 1
      components/pthread/pthread.c
  2. 21 0
      components/pthread/test/test_pthread_cxx.cpp

+ 3 - 1
components/pthread/pthread.c

@@ -232,6 +232,7 @@ int pthread_join(pthread_t thread, void **retval)
 {
     esp_pthread_t *pthread = (esp_pthread_t *)thread;
     int ret = 0;
+    bool wait = false;
 
     ESP_LOGV(TAG, "%s %p", __FUNCTION__, pthread);
 
@@ -257,6 +258,7 @@ int pthread_join(pthread_t thread, void **retval)
         } else {
             if (pthread->state == PTHREAD_TASK_STATE_RUN) {
                 pthread->join_task = xTaskGetCurrentTaskHandle();
+                wait = true;
             } else {
                 pthread_delete(pthread);
             }
@@ -264,7 +266,7 @@ int pthread_join(pthread_t thread, void **retval)
     }
     xSemaphoreGive(s_threads_mux);
 
-    if (ret == 0 && pthread->join_task) {
+    if (ret == 0 && wait) {
         xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
         if (xSemaphoreTake(s_threads_mux, portMAX_DELAY) != pdTRUE) {
             assert(false && "Failed to lock threads list!");

+ 21 - 0
components/pthread/test/test_pthread_cxx.cpp

@@ -1,10 +1,15 @@
 #include <iostream>
+#include <sstream>
 #include <thread>
 #include <mutex>
 #include "unity.h"
 
 #if __GTHREADS && __GTHREADS_CXX0X
 
+#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL
+#include "esp_log.h"
+const static char *TAG = "pthread_test";
+
 static std::shared_ptr<int> global_sp;
 static std::mutex           mtx;
 static std::recursive_mutex recur_mtx;
@@ -80,4 +85,20 @@ TEST_CASE("pthread CXX", "[pthread]")
     }
 }
 
+static void task_test_sandbox(void *ignore) {
+    ESP_LOGI(TAG, "About to create a string stream");
+    std::stringstream ss;
+    ESP_LOGI(TAG, "About to write to string stream");
+    ss << "Hello World!";
+}
+
+TEST_CASE("pthread CXX 2", "[pthread]")
+{
+    std::thread t1(task_test_sandbox, (void *)NULL);
+    if (t1.joinable()) {
+        std::cout << "Join thread " << std::hex << t1.get_id() << std::endl;
+        t1.join();
+    }
+}
+
 #endif