Make NS Consumer message handler thread as joinable thread. 42/212242/1
authorSenthil Kumar G S <senthil.gs@samsung.com>
Thu, 8 Aug 2019 15:09:23 +0000 (20:39 +0530)
committerSudipto Bal <sudipto.bal@samsung.com>
Mon, 19 Aug 2019 07:53:52 +0000 (07:53 +0000)
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 a414b374c4c0fdce7c8539aeab23bf7cc71bdeb4..9dd920f1c71b6bbbbc51e0ecbc599aa00b36a13a 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 4c1ec934a0044c717ceeecc06cbe142dc9443b65..f8fe0d3e0f385ac76aa49b8fbfc804fb921f6c59 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 a78fb5adaae52b3a13a0b5b7396863598933b11d..53e112fc12a4e1b39617e2098bc0af11ca23db24 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 *);