Add pthread_join() in case of failure of ca_thread_pool_add_task()
authorhyuna0213.jo <hyuna0213.jo@samsung.com>
Tue, 29 Nov 2016 08:08:38 +0000 (17:08 +0900)
committerZiran Sun <ziran.sun@samsung.com>
Tue, 29 Nov 2016 14:21:35 +0000 (14:21 +0000)
In case of failure, we should call pthread_join() to free memory or
other resources that were allocated to the target thread.

Change-Id: Iabcb25751301b8cdb95565355cc0b6d3cffafc1d
Signed-off-by: hyuna0213.jo <hyuna0213.jo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14889
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jaehong Jo <jaehong.jo@samsung.com>
Reviewed-by: jihwan seo <jihwan.seo@samsung.com>
Reviewed-by: Ziran Sun <ziran.sun@samsung.com>
resource/csdk/connectivity/common/src/cathreadpool_pthreads.c

index 3865df6dcfcecd6bf6f809277c7914aa2b17076e..7157005db6bc79d1c2cac55b93a5475e29ada9a4 100644 (file)
@@ -175,9 +175,23 @@ CAResult_t ca_thread_pool_add_task(ca_thread_pool_t thread_pool, ca_thread_func
     bool addResult = u_arraylist_add(thread_pool->details->threads_list, (void*)threadHandle);
     ca_mutex_unlock(thread_pool->details->list_lock);
 
-    if(!addResult)
+    if (!addResult)
     {
         OIC_LOG_V(ERROR, TAG, "Arraylist Add failed, may not be properly joined: %d", addResult);
+#if defined(_WIN32)
+        DWORD joinres = WaitForSingleObject(threadHandle, INFINITE);
+        if (WAIT_OBJECT_0 != joinres)
+        {
+            OIC_LOG_V(ERROR, TAG, "Failed to join thread with error %d", joinres);
+        }
+        CloseHandle(threadHandle);
+#else
+        int joinres = pthread_join(threadHandle, NULL);
+        if (0 != joinres)
+        {
+            OIC_LOG_V(ERROR, TAG, "Failed to join thread with error %d", joinres);
+        }
+#endif
         return CA_STATUS_FAILED;
     }