Implementation of connectivity abstraction feature Release v0.5
[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
73         void *data = message->msg;
74
75         // process data
76         thread->threadTask(data);
77
78         // free
79     }
80
81     u_cond_signal(thread->threadCond);
82
83     OIC_LOG_V(DEBUG, TAG, "message handler main thread end..");
84 }
85
86 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, u_thread_pool_t handle,
87                                       CAThreadTask task)
88 {
89     if (thread == NULL)
90     {
91         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
92         return CA_STATUS_FAILED;
93     }
94
95     if (handle == NULL)
96     {
97         OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
98         return CA_STATUS_FAILED;
99     }
100
101     OIC_LOG_V(DEBUG, TAG, "thread initialize..");
102
103     memset(thread, 0, sizeof(CAQueueingThread_t));
104
105     // mutex init
106     u_mutex_init();
107
108     // set send thread data
109     thread->threadPool = handle;
110     thread->dataQueue = u_queue_create();
111     thread->threadMutex = u_mutex_new();
112     thread->threadCond = u_cond_new();
113     thread->isStop = CA_TRUE;
114     thread->threadTask = task;
115
116     return CA_STATUS_OK;
117 }
118
119 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
120 {
121     if (thread == NULL)
122     {
123         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
124         return CA_STATUS_FAILED;
125     }
126
127     if (thread->threadPool == NULL)
128     {
129         OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
130         return CA_STATUS_FAILED;
131     }
132
133     CAResult_t res = u_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
134                                             thread);
135
136     if (res != CA_STATUS_OK)
137     {
138         OIC_LOG_V(DEBUG, TAG, "thread pool add task error(send thread).");
139         return res;
140     }
141
142     thread->isStop = CA_FALSE;
143
144     return res;
145 }
146
147 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
148 {
149     if (thread == NULL)
150     {
151         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
152         return CA_STATUS_FAILED;
153     }
154
155     if (data == NULL || size == 0)
156     {
157         OIC_LOG_V(DEBUG, TAG, "data is empty..");
158
159         return CA_STATUS_FAILED;
160     }
161
162     // create thread data
163     u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
164
165     if (message == NULL)
166     {
167         OIC_LOG_V(DEBUG, TAG, "memory error!!");
168         return CA_MEMORY_ALLOC_FAILED;
169     }
170     memset(message, 0, sizeof(u_queue_message_t));
171
172     message->msg = data;
173     message->size = sizeof(size);
174
175     // mutex lock
176     u_mutex_lock(thread->threadMutex);
177
178     // add thread data into list
179     u_queue_add_element(thread->dataQueue, message);
180
181     // notity the thread
182     u_cond_signal(thread->threadCond);
183
184     // mutex unlock
185     u_mutex_unlock(thread->threadMutex);
186
187     return CA_STATUS_OK;
188 }
189
190 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
191 {
192     if (thread == NULL)
193     {
194         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
195         return CA_STATUS_FAILED;
196     }
197
198     OIC_LOG_V(DEBUG, TAG, "thread destroy..");
199
200     u_mutex_free(thread->threadMutex);
201     thread->threadMutex = NULL;
202     u_cond_free(thread->threadCond);
203     u_queue_delete(thread->dataQueue);
204
205     return CA_STATUS_OK;
206 }
207
208 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
209 {
210     if (thread == NULL)
211     {
212         OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
213         return CA_STATUS_FAILED;
214     }
215
216     OIC_LOG_V(DEBUG, TAG, "thread stop request!!");
217
218     if (!thread->isStop)
219     {
220         // mutex lock
221         u_mutex_lock(thread->threadMutex);
222
223         // set stop flag
224         thread->isStop = CA_TRUE;
225
226         // notity the thread
227         u_cond_signal(thread->threadCond);
228
229         u_cond_wait(thread->threadCond, thread->threadMutex);
230
231         // mutex unlock
232         u_mutex_unlock(thread->threadMutex);
233     }
234
235     return CA_STATUS_OK;
236 }
237