From 54a529f59670bb13d4ac884076caa2a32a44dfeb Mon Sep 17 00:00:00 2001 From: Alexey Gerenkov Date: Thu, 28 Sep 2017 17:55:17 +0300 Subject: [PATCH] freertos: Fixes hangup upon pthread_join on finished thread --- components/pthread/pthread.c | 4 +++- components/pthread/test/test_pthread_cxx.cpp | 21 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/components/pthread/pthread.c b/components/pthread/pthread.c index 73f23a00d7..c7d6f67075 100644 --- a/components/pthread/pthread.c +++ b/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!"); diff --git a/components/pthread/test/test_pthread_cxx.cpp b/components/pthread/test/test_pthread_cxx.cpp index 287a1da1b1..bed906f610 100644 --- a/components/pthread/test/test_pthread_cxx.cpp +++ b/components/pthread/test/test_pthread_cxx.cpp @@ -1,10 +1,15 @@ #include +#include #include #include #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 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