Make NS Consumer message handler thread as joinable thread. 41/212241/1
authorSenthil Kumar G S <senthil.gs@samsung.com>
Thu, 8 Aug 2019 15:09:23 +0000 (20:39 +0530)
committerSudipto <sudipto.bal@samsung.com>
Mon, 19 Aug 2019 07:50:52 +0000 (13:20 +0530)
Background:
Currently, NS Consumer message handler thread is a detached thread.
When stopping NS Consumer, we have to destroy thread handle and other resources of message handler thread.
We cannot destroy if message handler thread is running.
So before we destroy, we need to wait for the message handler thread to terminate.
pthread_join() is called internally. As it is on a detached thread, it is useless and it could result in undefined behavior.

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/commit/93e66a00acc08ce0c7a3fb217bfb2cc92f2a6c15
(cherry-picked from 93e66a00acc08ce0c7a3fb217bfb2cc92f2a6c15)

Change-Id: I43113a1d6ac0dcbba15a6b12032d71c5369e8b3b
Signed-off-by: Senthil Kumar G S <senthil.gs@samsung.com>
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
service/notification/src/consumer/NSConsumerScheduler.c
service/notification/src/consumer/NSThread.c
service/notification/src/consumer/NSThread.h

index a414b37..9dd920f 100644 (file)
@@ -98,7 +98,7 @@ NSResult NSConsumerMessageHandlerInit()
     NSSetMsgHandleQueue(queue);
 
     NS_LOG(DEBUG, "queue thread init");
-    handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
+    handle = NSJoinableThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
     NS_VERIFY_NOT_NULL(handle, NS_ERROR);
     NSSetMsgHandleThreadHandle(handle);
 
index 4c1ec93..f8fe0d3 100644 (file)
@@ -26,7 +26,7 @@
 #include <memory.h>
 #include "oic_malloc.h"
 
-NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
+static NSConsumerThread * NSThreadInitInternal(NSThreadFunc func, void * data, bool detach)
 {
     NS_VERIFY_NOT_NULL(func, NULL);
 
@@ -47,20 +47,39 @@ NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
 
     handle->isStarted = true;
 
-    pthread_attr_t attrDetached = {};
-    pthread_attr_init(& attrDetached);
-    pthread_attr_setdetachstate(& attrDetached, PTHREAD_CREATE_DETACHED);
+    if (detach)
+    {
+        pthread_attr_t attrDetached = {};
+        pthread_attr_init(& attrDetached);
+        pthread_attr_setdetachstate(& attrDetached, PTHREAD_CREATE_DETACHED);
+
+        pthreadResult = pthread_create(&(handle->thread_id), & attrDetached, func,
+                               (data == NULL) ? (void *) handle : (void *)data);
+
+        pthread_attr_destroy(& attrDetached);
+    }
+    else
+    {
+        pthreadResult = pthread_create(&(handle->thread_id), NULL, func,
+                               (data == NULL) ? (void *) handle : (void *)data);
+    }
 
-    pthreadResult = pthread_create(&(handle->thread_id), & attrDetached, func,
-                           (data == NULL) ? (void *) handle : (void *)data);
     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
             NULL, NSDestroyThreadHandle(handle));
 
-    pthread_attr_destroy(& attrDetached);
-
     return handle;
 }
 
+NSConsumerThread * NSJoinableThreadInit(NSThreadFunc func, void * data)
+{
+    return NSThreadInitInternal(func, data, false);
+}
+
+NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
+{
+    return NSThreadInitInternal(func, data, true);
+}
+
 void NSThreadLock(NSConsumerThread * handle)
 {
     NS_VERIFY_NOT_NULL_V(handle);
index a78fb5a..53e112f 100644 (file)
@@ -51,6 +51,8 @@ typedef void *(*NSThreadFunc)(void *);
 
 NSConsumerThread * NSThreadInit(NSThreadFunc, void *);
 
+NSConsumerThread * NSJoinableThreadInit(NSThreadFunc func, void *);
+
 void NSThreadLock(NSConsumerThread *);
 
 void NSThreadUnlock(NSConsumerThread *);