From d816578e3a1ccf4aacb9f5a579fb0353cf889606 Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Thu, 27 Dec 2018 18:32:14 +0530 Subject: [PATCH] bugfix/pthread : pthread_detach implementation fixed to correctly delete pthread object when invoked after task completion --- components/pthread/pthread.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/components/pthread/pthread.c b/components/pthread/pthread.c index 1baa035c9b..a1ae6d440e 100644 --- a/components/pthread/pthread.c +++ b/components/pthread/pthread.c @@ -395,8 +395,19 @@ int pthread_detach(pthread_t thread) TaskHandle_t handle = pthread_find_handle(thread); if (!handle) { ret = ESRCH; - } else { + } else if (pthread->detached) { + // already detached + ret = EINVAL; + } else if (pthread->join_task) { + // already have waiting task to join + ret = EINVAL; + } else if (pthread->state == PTHREAD_TASK_STATE_RUN) { + // pthread still running pthread->detached = true; + } else { + // pthread already stopped + pthread_delete(pthread); + vTaskDelete(handle); } xSemaphoreGive(s_threads_mux); ESP_LOGV(TAG, "%s %p EXIT %d", __FUNCTION__, pthread, ret);