Fix bugs for result of static analizer.
[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 static pthread_mutex_t g_create_mutex;
30
31 NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
32 {
33     NS_VERIFY_NOT_NULL(func, NULL);
34
35     pthread_mutex_init(&g_create_mutex, NULL);
36
37     NSConsumerThread * handle = (NSConsumerThread *)OICMalloc(sizeof(NSConsumerThread));
38     NS_VERIFY_NOT_NULL(handle, NULL);
39
40     memset(handle, 0, sizeof(NSConsumerThread));
41
42     pthread_mutexattr_init(&(handle->mutex_attr));
43
44     int pthreadResult = pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE);
45     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
46             NULL, NSDestroyThreadHandle(handle));
47
48     pthreadResult = pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr));
49     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
50             NULL, NSDestroyThreadHandle(handle));
51
52     pthread_mutex_lock(&g_create_mutex);
53
54     handle->isStarted = true;
55
56     pthread_attr_t attrDetached = {};
57     pthread_attr_init(& attrDetached);
58     pthread_attr_setdetachstate(& attrDetached, PTHREAD_CREATE_DETACHED);
59
60     pthreadResult = pthread_create(&(handle->thread_id), & attrDetached, func,
61                            (data == NULL) ? (void *) handle : (void *)data);
62     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
63             NULL, NSDestroyThreadHandle(handle));
64
65     pthread_attr_destroy(& attrDetached);
66
67     pthread_mutex_unlock(&g_create_mutex);
68
69     return handle;
70 }
71
72 void NSThreadLock(NSConsumerThread * handle)
73 {
74     NS_VERIFY_NOT_NULL_V(handle);
75
76     pthread_mutex_lock(&(handle->mutex));
77 }
78
79 void NSThreadUnlock(NSConsumerThread * handle)
80 {
81     NS_VERIFY_NOT_NULL_V(handle);
82
83     pthread_mutex_unlock(&(handle->mutex));
84 }
85
86 void NSThreadStop(NSConsumerThread * handle)
87 {
88     NS_VERIFY_NOT_NULL_V(handle);
89
90     handle->isStarted = false;
91     NSThreadJoin(handle);
92
93     NSDestroyThreadHandle(handle);
94 }
95
96 void NSThreadJoin(NSConsumerThread * handle)
97 {
98     NS_VERIFY_NOT_NULL_V(handle);
99
100     if (handle->thread_id)
101     {
102         void * retData = NULL;
103         pthread_join(handle->thread_id, & retData);
104         NSOICFree(retData);
105     }
106 }
107
108 void NSDestroyThreadHandle(NSConsumerThread * handle)
109 {
110     NS_VERIFY_NOT_NULL_V(handle);
111
112     pthread_mutex_destroy(&(handle->mutex));
113     pthread_mutexattr_destroy(&(handle->mutex_attr));
114
115     pthread_mutex_unlock(&g_create_mutex);
116 }
117
118 void NSThreadDetach()
119 {
120     pthread_detach(pthread_self());
121 }