IoT-243 Connectivity Abstraction single-threaded module missing definition for CASend...
[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_V(DEBUG, TAG, "message handler main thread start..");
37
38     CAQueueingThread_t *thread = (CAQueueingThread_t *) threadValue;
39
40     if (thread == NULL)
41     {
42         OIC_LOG_V(DEBUG, TAG, "thread data passing error!!");
43
44         return;
45     }
46
47     while (!thread->isStop)
48     {
49         // mutex lock
50         u_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_V(DEBUG, TAG, "wait..");
56
57             // wait
58             u_cond_wait(thread->threadCond, thread->threadMutex);
59
60             OIC_LOG_V(DEBUG, TAG, "wake up..");
61         }
62
63         // mutex unlock
64         u_mutex_unlock(thread->threadMutex);
65
66         // check stop flag
67         if (thread->isStop)
68             continue;
69
70         // get data
71         u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
72         if (message == NULL)
73         {
74             continue;
75         }
76
77         void *data = message->msg;
78
79         // process data
80         thread->threadTask(data);
81
82         // free
83         if (thread->destroy != NULL)
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 (thread->destroy != NULL)
103         {
104             thread->destroy(message->msg, message->size);
105         }
106         else
107         {
108             OICFree(message->msg);
109         }
110
111         OICFree(message);
112     }
113
114     u_cond_signal(thread->threadCond);
115
116     OIC_LOG_V(DEBUG, TAG, "message handler main thread end..");
117 }
118
119 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, u_thread_pool_t handle,
120                                       CAThreadTask task, CADataDestroyFunction destroy)
121 {
122     if (thread == NULL)
123     {
124         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
125         return CA_STATUS_FAILED;
126     }
127
128     if (handle == NULL)
129     {
130         OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
131         return CA_STATUS_FAILED;
132     }
133
134     OIC_LOG_V(DEBUG, TAG, "thread initialize..");
135
136     memset(thread, 0, sizeof(CAQueueingThread_t));
137
138     // mutex init
139     u_mutex_init();
140
141     // set send thread data
142     thread->threadPool = handle;
143     thread->dataQueue = u_queue_create();
144     thread->threadMutex = u_mutex_new();
145     thread->threadCond = u_cond_new();
146     thread->isStop = CA_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 (thread == NULL)
156     {
157         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
158         return CA_STATUS_FAILED;
159     }
160
161     if (thread->threadPool == NULL)
162     {
163         OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
164         return CA_STATUS_FAILED;
165     }
166
167     if (CA_FALSE == thread->isStop) //Queueing thread already running
168     {
169         OIC_LOG_V(DEBUG, TAG, "queueing thread already running..");
170         return CA_STATUS_OK;
171     }
172
173     thread->isStop = CA_FALSE;
174     CAResult_t res = u_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
175                                             thread);
176     if (res != CA_STATUS_OK)
177     {
178         OIC_LOG_V(DEBUG, TAG, "thread pool add task error(send thread).");
179         thread->isStop = CA_TRUE;
180         return res;
181     }
182
183     return res;
184 }
185
186 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
187 {
188     if (thread == NULL)
189     {
190         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
191         return CA_STATUS_FAILED;
192     }
193
194     if (data == NULL || size == 0)
195     {
196         OIC_LOG_V(DEBUG, TAG, "data is empty..");
197
198         return CA_STATUS_FAILED;
199     }
200
201     // create thread data
202     u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
203
204     if (message == NULL)
205     {
206         OIC_LOG_V(DEBUG, TAG, "memory error!!");
207         return CA_MEMORY_ALLOC_FAILED;
208     }
209     memset(message, 0, sizeof(u_queue_message_t));
210
211     message->msg = data;
212     message->size = sizeof(size);
213
214     // mutex lock
215     u_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     u_cond_signal(thread->threadCond);
222
223     // mutex unlock
224     u_mutex_unlock(thread->threadMutex);
225
226     return CA_STATUS_OK;
227 }
228
229 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
230 {
231     if (thread == NULL)
232     {
233         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
234         return CA_STATUS_FAILED;
235     }
236
237     OIC_LOG_V(DEBUG, TAG, "thread destroy..");
238
239     u_mutex_free(thread->threadMutex);
240     thread->threadMutex = NULL;
241     u_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 (thread == NULL)
250     {
251         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
252         return CA_STATUS_FAILED;
253     }
254
255     OIC_LOG_V(DEBUG, TAG, "thread stop request!!");
256
257     if (!thread->isStop)
258     {
259         // mutex lock
260         u_mutex_lock(thread->threadMutex);
261
262         // set stop flag
263         thread->isStop = CA_TRUE;
264
265         // notity the thread
266         u_cond_signal(thread->threadCond);
267
268         u_cond_wait(thread->threadCond, thread->threadMutex);
269
270         // mutex unlock
271         u_mutex_unlock(thread->threadMutex);
272     }
273
274     return CA_STATUS_OK;
275 }
276