Add pthread implementation for connectivity/common/umutex
[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 (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_cond_signal(thread->threadCond);
118
119     OIC_LOG(DEBUG, TAG, "message handler main thread end..");
120 }
121
122 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, u_thread_pool_t handle,
123                                       CAThreadTask task, CADataDestroyFunction destroy)
124 {
125     if (NULL == thread)
126     {
127         OIC_LOG(ERROR, TAG, "thread instance is empty..");
128         return CA_STATUS_INVALID_PARAM;
129     }
130
131     if (NULL == handle)
132     {
133         OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
134         return CA_STATUS_INVALID_PARAM;
135     }
136
137     OIC_LOG(DEBUG, TAG, "thread initialize..");
138
139     // set send thread data
140     thread->threadPool = handle;
141     thread->dataQueue = u_queue_create();
142     thread->threadMutex = ca_mutex_new();
143     thread->threadCond = ca_cond_new();
144     thread->isStop = true;
145     thread->threadTask = task;
146     thread->destroy = destroy;
147
148     return CA_STATUS_OK;
149 }
150
151 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
152 {
153     if (NULL == thread)
154     {
155         OIC_LOG(ERROR, TAG, "thread instance is empty..");
156         return CA_STATUS_INVALID_PARAM;
157     }
158
159     if (NULL == thread->threadPool)
160     {
161         OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
162         return CA_STATUS_INVALID_PARAM;
163     }
164
165     if (false == thread->isStop) //Queueing thread already running
166     {
167         OIC_LOG(DEBUG, TAG, "queueing thread already running..");
168         return CA_STATUS_OK;
169     }
170
171     // mutex lock
172     ca_mutex_lock(thread->threadMutex);
173     thread->isStop = false;
174     // mutex unlock
175     ca_mutex_unlock(thread->threadMutex);
176
177     CAResult_t res = u_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
178                                             thread);
179     if (res != CA_STATUS_OK)
180     {
181         OIC_LOG(ERROR, TAG, "thread pool add task error(send thread).");
182     }
183
184     return res;
185 }
186
187 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
188 {
189     if (NULL == thread)
190     {
191         OIC_LOG(ERROR, TAG, "thread instance is empty..");
192         return CA_STATUS_INVALID_PARAM;
193     }
194
195     if (NULL == data || 0 == size)
196     {
197         OIC_LOG(ERROR, TAG, "data is empty..");
198
199         return CA_STATUS_INVALID_PARAM;
200     }
201
202     // create thread data
203     u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
204
205     if (NULL == message)
206     {
207         OIC_LOG(ERROR, TAG, "memory error!!");
208         return CA_MEMORY_ALLOC_FAILED;
209     }
210
211     message->msg = data;
212     message->size = size;
213
214     // mutex lock
215     ca_mutex_lock(thread->threadMutex);
216
217     // add thread data into list
218     u_queue_add_element(thread->dataQueue, message);
219
220     // notity the thread
221     ca_cond_signal(thread->threadCond);
222
223     // mutex unlock
224     ca_mutex_unlock(thread->threadMutex);
225
226     return CA_STATUS_OK;
227 }
228
229 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
230 {
231     if (NULL == thread)
232     {
233         OIC_LOG(ERROR, TAG, "thread instance is empty..");
234         return CA_STATUS_INVALID_PARAM;
235     }
236
237     OIC_LOG(DEBUG, TAG, "thread destroy..");
238
239     ca_mutex_free(thread->threadMutex);
240     thread->threadMutex = NULL;
241     ca_cond_free(thread->threadCond);
242     u_queue_delete(thread->dataQueue);
243
244     return CA_STATUS_OK;
245 }
246
247 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
248 {
249     if (NULL == thread)
250     {
251         OIC_LOG(ERROR, TAG, "thread instance is empty..");
252         return CA_STATUS_INVALID_PARAM;
253     }
254
255     OIC_LOG(DEBUG, TAG, "thread stop request!!");
256
257     if (!thread->isStop)
258     {
259         // mutex lock
260         ca_mutex_lock(thread->threadMutex);
261
262         // set stop flag
263         thread->isStop = true;
264
265         // notify the thread
266         ca_cond_signal(thread->threadCond);
267
268         ca_cond_wait(thread->threadCond, thread->threadMutex);
269
270         // mutex unlock
271         ca_mutex_unlock(thread->threadMutex);
272     }
273
274     return CA_STATUS_OK;
275 }