4c1ec934a0044c717ceeecc06cbe142dc9443b65
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSThread.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "NSThread.h"
22
23 #include "NSConstants.h"
24 #include "NSConsumerCommon.h"
25
26 #include <memory.h>
27 #include "oic_malloc.h"
28
29 NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
30 {
31     NS_VERIFY_NOT_NULL(func, NULL);
32
33     NSConsumerThread * handle = (NSConsumerThread *)OICMalloc(sizeof(NSConsumerThread));
34     NS_VERIFY_NOT_NULL(handle, NULL);
35
36     memset(handle, 0, sizeof(NSConsumerThread));
37
38     pthread_mutexattr_init(&(handle->mutex_attr));
39
40     int pthreadResult = pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE);
41     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
42             NULL, NSDestroyThreadHandle(handle));
43
44     pthreadResult = pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr));
45     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
46             NULL, NSDestroyThreadHandle(handle));
47
48     handle->isStarted = true;
49
50     pthread_attr_t attrDetached = {};
51     pthread_attr_init(& attrDetached);
52     pthread_attr_setdetachstate(& attrDetached, PTHREAD_CREATE_DETACHED);
53
54     pthreadResult = pthread_create(&(handle->thread_id), & attrDetached, func,
55                            (data == NULL) ? (void *) handle : (void *)data);
56     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
57             NULL, NSDestroyThreadHandle(handle));
58
59     pthread_attr_destroy(& attrDetached);
60
61     return handle;
62 }
63
64 void NSThreadLock(NSConsumerThread * handle)
65 {
66     NS_VERIFY_NOT_NULL_V(handle);
67
68     pthread_mutex_lock(&(handle->mutex));
69 }
70
71 void NSThreadUnlock(NSConsumerThread * handle)
72 {
73     NS_VERIFY_NOT_NULL_V(handle);
74
75     pthread_mutex_unlock(&(handle->mutex));
76 }
77
78 void NSThreadStop(NSConsumerThread * handle)
79 {
80     NS_VERIFY_NOT_NULL_V(handle);
81
82     handle->isStarted = false;
83     NSThreadJoin(handle);
84
85     NSDestroyThreadHandle(handle);
86 }
87
88 void NSThreadJoin(NSConsumerThread * handle)
89 {
90     NS_VERIFY_NOT_NULL_V(handle);
91
92     if (handle->thread_id)
93     {
94         void * retData = NULL;
95         pthread_join(handle->thread_id, & retData);
96         NSOICFree(retData);
97     }
98 }
99
100 void NSDestroyThreadHandle(NSConsumerThread * handle)
101 {
102     NS_VERIFY_NOT_NULL_V(handle);
103
104     pthread_mutex_destroy(&(handle->mutex));
105     pthread_mutexattr_destroy(&(handle->mutex_attr));
106 }
107
108 void NSThreadDetach()
109 {
110     pthread_detach(pthread_self());
111 }