Fixed a pair of deadlocks on shutdown
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caqueueingthread.c
1 /******************************************************************
2  *
3  * Copyright 2014 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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/syscall.h>
26 #include <sys/types.h>
27
28 #include "caqueueingthread.h"
29 #include "oic_malloc.h"
30 #include "logger.h"
31
32 #define TAG PCF("CA")
33
34 static void CAQueueingThreadBaseRoutine(void *threadValue)
35 {
36     OIC_LOG(DEBUG, TAG, "message handler main thread start..");
37
38     CAQueueingThread_t *thread = (CAQueueingThread_t *) threadValue;
39
40     if (NULL == thread)
41     {
42         OIC_LOG(ERROR, TAG, "thread data passing error!!");
43
44         return;
45     }
46
47     while (!thread->isStop)
48     {
49         // mutex lock
50         ca_mutex_lock(thread->threadMutex);
51
52         // if queue is empty, thread will wait
53         if (!thread->isStop && u_queue_get_size(thread->dataQueue) <= 0)
54         {
55             OIC_LOG(DEBUG, TAG, "wait..");
56
57             // wait
58             ca_cond_wait(thread->threadCond, thread->threadMutex);
59
60             OIC_LOG(DEBUG, TAG, "wake up..");
61         }
62
63         // mutex unlock
64         ca_mutex_unlock(thread->threadMutex);
65
66         // check stop flag
67         if (thread->isStop)
68         {
69             continue;
70         }
71
72         // get data
73         u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
74         if (NULL == message)
75         {
76             continue;
77         }
78
79         // process data
80         thread->threadTask(message->msg);
81
82         // free
83         if (NULL != thread->destroy)
84         {
85             thread->destroy(message->msg, message->size);
86         }
87         else
88         {
89             OICFree(message->msg);
90         }
91
92         OICFree(message);
93     }
94
95     // remove all remained list data.
96     while (u_queue_get_size(thread->dataQueue) > 0)
97     {
98         // get data
99         u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
100
101         // free
102         if(NULL != message)
103         {
104             if (NULL != thread->destroy)
105             {
106                 thread->destroy(message->msg, message->size);
107             }
108             else
109             {
110                 OICFree(message->msg);
111             }
112
113             OICFree(message);
114         }
115     }
116
117     ca_mutex_lock(thread->threadMutex);
118     ca_cond_signal(thread->threadCond);
119     ca_mutex_unlock(thread->threadMutex);
120
121     OIC_LOG(DEBUG, TAG, "message handler main thread end..");
122 }
123
124 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, ca_thread_pool_t handle,
125                                       CAThreadTask task, CADataDestroyFunction destroy)
126 {
127     if (NULL == thread)
128     {
129         OIC_LOG(ERROR, TAG, "thread instance is empty..");
130         return CA_STATUS_INVALID_PARAM;
131     }
132
133     if (NULL == handle)
134     {
135         OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
136         return CA_STATUS_INVALID_PARAM;
137     }
138
139     OIC_LOG(DEBUG, TAG, "thread initialize..");
140
141     // set send thread data
142     thread->threadPool = handle;
143     thread->dataQueue = u_queue_create();
144     thread->threadMutex = ca_mutex_new();
145     thread->threadCond = ca_cond_new();
146     thread->isStop = true;
147     thread->threadTask = task;
148     thread->destroy = destroy;
149
150     return CA_STATUS_OK;
151 }
152
153 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
154 {
155     if (NULL == thread)
156     {
157         OIC_LOG(ERROR, TAG, "thread instance is empty..");
158         return CA_STATUS_INVALID_PARAM;
159     }
160
161     if (NULL == thread->threadPool)
162     {
163         OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
164         return CA_STATUS_INVALID_PARAM;
165     }
166
167     if (false == thread->isStop) //Queueing thread already running
168     {
169         OIC_LOG(DEBUG, TAG, "queueing thread already running..");
170         return CA_STATUS_OK;
171     }
172
173     // mutex lock
174     ca_mutex_lock(thread->threadMutex);
175     thread->isStop = false;
176     // mutex unlock
177     ca_mutex_unlock(thread->threadMutex);
178
179     CAResult_t res = ca_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
180                                             thread);
181     if (res != CA_STATUS_OK)
182     {
183         OIC_LOG(ERROR, TAG, "thread pool add task error(send thread).");
184     }
185
186     return res;
187 }
188
189 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
190 {
191     if (NULL == thread)
192     {
193         OIC_LOG(ERROR, TAG, "thread instance is empty..");
194         return CA_STATUS_INVALID_PARAM;
195     }
196
197     if (NULL == data || 0 == size)
198     {
199         OIC_LOG(ERROR, TAG, "data is empty..");
200
201         return CA_STATUS_INVALID_PARAM;
202     }
203
204     // create thread data
205     u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
206
207     if (NULL == message)
208     {
209         OIC_LOG(ERROR, TAG, "memory error!!");
210         return CA_MEMORY_ALLOC_FAILED;
211     }
212
213     message->msg = data;
214     message->size = size;
215
216     // mutex lock
217     ca_mutex_lock(thread->threadMutex);
218
219     // add thread data into list
220     u_queue_add_element(thread->dataQueue, message);
221
222     // notity the thread
223     ca_cond_signal(thread->threadCond);
224
225     // mutex unlock
226     ca_mutex_unlock(thread->threadMutex);
227
228     return CA_STATUS_OK;
229 }
230
231 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
232 {
233     if (NULL == thread)
234     {
235         OIC_LOG(ERROR, TAG, "thread instance is empty..");
236         return CA_STATUS_INVALID_PARAM;
237     }
238
239     OIC_LOG(DEBUG, TAG, "thread destroy..");
240
241     ca_mutex_free(thread->threadMutex);
242     thread->threadMutex = NULL;
243     ca_cond_free(thread->threadCond);
244     u_queue_delete(thread->dataQueue);
245
246     return CA_STATUS_OK;
247 }
248
249 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
250 {
251     if (NULL == thread)
252     {
253         OIC_LOG(ERROR, TAG, "thread instance is empty..");
254         return CA_STATUS_INVALID_PARAM;
255     }
256
257     OIC_LOG(DEBUG, TAG, "thread stop request!!");
258
259     if (!thread->isStop)
260     {
261         // mutex lock
262         ca_mutex_lock(thread->threadMutex);
263
264         // set stop flag
265         thread->isStop = true;
266
267         // notify the thread
268         ca_cond_signal(thread->threadCond);
269
270         ca_cond_wait(thread->threadCond, thread->threadMutex);
271
272         // mutex unlock
273         ca_mutex_unlock(thread->threadMutex);
274     }
275
276     return CA_STATUS_OK;
277 }