]> granicus.if.org Git - esp-idf/commitdiff
bugfix/pthread : pthread_detach implementation fixed to correctly delete pthread...
authorAnurag Kar <anurag.kar@espressif.com>
Thu, 27 Dec 2018 13:02:14 +0000 (18:32 +0530)
committerAnurag Kar <anurag.kar@espressif.com>
Thu, 27 Dec 2018 13:17:46 +0000 (18:47 +0530)
components/pthread/pthread.c

index 1baa035c9b66b058364f6bce68832bec7521faaf..a1ae6d440e8b61c73a6ce28dedad3f13d983834c 100644 (file)
@@ -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);