From 240762162713cef7e9961b2136fc6560a77b222e Mon Sep 17 00:00:00 2001 From: Senthil Kumar G S Date: Thu, 8 Aug 2019 20:39:23 +0530 Subject: [PATCH] Make NS Consumer message handler thread as joinable thread. 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 Signed-off-by: Sudipto --- .../src/consumer/NSConsumerScheduler.c | 2 +- service/notification/src/consumer/NSThread.c | 35 +++++++++++++++++----- service/notification/src/consumer/NSThread.h | 2 ++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/service/notification/src/consumer/NSConsumerScheduler.c b/service/notification/src/consumer/NSConsumerScheduler.c index a414b37..9dd920f 100644 --- a/service/notification/src/consumer/NSConsumerScheduler.c +++ b/service/notification/src/consumer/NSConsumerScheduler.c @@ -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); diff --git a/service/notification/src/consumer/NSThread.c b/service/notification/src/consumer/NSThread.c index 4c1ec93..f8fe0d3 100644 --- a/service/notification/src/consumer/NSThread.c +++ b/service/notification/src/consumer/NSThread.c @@ -26,7 +26,7 @@ #include #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); diff --git a/service/notification/src/consumer/NSThread.h b/service/notification/src/consumer/NSThread.h index a78fb5a..53e112f 100644 --- a/service/notification/src/consumer/NSThread.h +++ b/service/notification/src/consumer/NSThread.h @@ -51,6 +51,8 @@ typedef void *(*NSThreadFunc)(void *); NSConsumerThread * NSThreadInit(NSThreadFunc, void *); +NSConsumerThread * NSJoinableThreadInit(NSThreadFunc func, void *); + void NSThreadLock(NSConsumerThread *); void NSThreadUnlock(NSConsumerThread *); -- 2.7.4