1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
25 #include <sys/syscall.h>
26 #include <sys/types.h>
28 #include "caqueueingthread.h"
29 #include "oic_malloc.h"
32 #define TAG PCF("CA_QING")
34 static void CAQueueingThreadBaseRoutine(void *threadValue)
36 OIC_LOG(DEBUG, TAG, "message handler main thread start..");
38 CAQueueingThread_t *thread = (CAQueueingThread_t *) threadValue;
42 OIC_LOG(ERROR, TAG, "thread data passing error!!");
47 while (!thread->isStop)
50 ca_mutex_lock(thread->threadMutex);
52 // if queue is empty, thread will wait
53 if (!thread->isStop && u_queue_get_size(thread->dataQueue) <= 0)
55 OIC_LOG(DEBUG, TAG, "wait..");
58 ca_cond_wait(thread->threadCond, thread->threadMutex);
60 OIC_LOG(DEBUG, TAG, "wake up..");
69 ca_mutex_unlock(thread->threadMutex);
74 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
76 ca_mutex_unlock(thread->threadMutex);
83 thread->threadTask(message->msg);
86 if (NULL != thread->destroy)
88 thread->destroy(message->msg, message->size);
92 OICFree(message->msg);
98 // remove all remained list data.
99 while (u_queue_get_size(thread->dataQueue) > 0)
102 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
107 if (NULL != thread->destroy)
109 thread->destroy(message->msg, message->size);
113 OICFree(message->msg);
120 ca_mutex_lock(thread->threadMutex);
121 ca_cond_signal(thread->threadCond);
122 ca_mutex_unlock(thread->threadMutex);
124 OIC_LOG(DEBUG, TAG, "message handler main thread end..");
127 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, ca_thread_pool_t handle,
128 CAThreadTask task, CADataDestroyFunction destroy)
132 OIC_LOG(ERROR, TAG, "thread instance is empty..");
133 return CA_STATUS_INVALID_PARAM;
138 OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
139 return CA_STATUS_INVALID_PARAM;
142 OIC_LOG(DEBUG, TAG, "thread initialize..");
144 // set send thread data
145 thread->threadPool = handle;
146 thread->dataQueue = u_queue_create();
147 thread->threadMutex = ca_mutex_new();
148 thread->threadCond = ca_cond_new();
149 thread->isStop = true;
150 thread->threadTask = task;
151 thread->destroy = destroy;
152 if(NULL == thread->dataQueue || NULL == thread->threadMutex || NULL == thread->threadCond)
153 goto ERROR_MEM_FAILURE;
157 if(thread->dataQueue)
159 u_queue_delete(thread->dataQueue);
160 thread->dataQueue = NULL;
162 if(thread->threadMutex)
164 ca_mutex_free(thread->threadMutex);
165 thread->threadMutex = NULL;
167 if(thread->threadCond)
169 ca_cond_free(thread->threadCond);
170 thread->threadCond = NULL;
172 return CA_MEMORY_ALLOC_FAILED;
176 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
180 OIC_LOG(ERROR, TAG, "thread instance is empty..");
181 return CA_STATUS_INVALID_PARAM;
184 if (NULL == thread->threadPool)
186 OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
187 return CA_STATUS_INVALID_PARAM;
190 if (false == thread->isStop) //Queueing thread already running
192 OIC_LOG(DEBUG, TAG, "queueing thread already running..");
197 ca_mutex_lock(thread->threadMutex);
198 thread->isStop = false;
200 ca_mutex_unlock(thread->threadMutex);
202 CAResult_t res = ca_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
204 if (res != CA_STATUS_OK)
206 OIC_LOG(ERROR, TAG, "thread pool add task error(send thread).");
212 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
216 OIC_LOG(ERROR, TAG, "thread instance is empty..");
217 return CA_STATUS_INVALID_PARAM;
220 if (NULL == data || 0 == size)
222 OIC_LOG(ERROR, TAG, "data is empty..");
224 return CA_STATUS_INVALID_PARAM;
227 // create thread data
228 u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
232 OIC_LOG(ERROR, TAG, "memory error!!");
233 return CA_MEMORY_ALLOC_FAILED;
237 message->size = size;
240 ca_mutex_lock(thread->threadMutex);
242 // add thread data into list
243 u_queue_add_element(thread->dataQueue, message);
246 ca_cond_signal(thread->threadCond);
249 ca_mutex_unlock(thread->threadMutex);
254 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
258 OIC_LOG(ERROR, TAG, "thread instance is empty..");
259 return CA_STATUS_INVALID_PARAM;
262 OIC_LOG(DEBUG, TAG, "thread destroy..");
264 ca_mutex_free(thread->threadMutex);
265 thread->threadMutex = NULL;
266 ca_cond_free(thread->threadCond);
267 u_queue_delete(thread->dataQueue);
272 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
276 OIC_LOG(ERROR, TAG, "thread instance is empty..");
277 return CA_STATUS_INVALID_PARAM;
280 OIC_LOG(DEBUG, TAG, "thread stop request!!");
285 ca_mutex_lock(thread->threadMutex);
288 thread->isStop = true;
291 ca_cond_signal(thread->threadCond);
293 ca_cond_wait(thread->threadCond, thread->threadMutex);
296 ca_mutex_unlock(thread->threadMutex);