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/types.h>
27 #include "caqueueingthread.h"
28 #include "oic_malloc.h"
31 #define TAG PCF("OIC_CA_QING")
33 static void CAQueueingThreadBaseRoutine(void *threadValue)
35 OIC_LOG(DEBUG, TAG, "message handler main thread start..");
37 CAQueueingThread_t *thread = (CAQueueingThread_t *) threadValue;
41 OIC_LOG(ERROR, TAG, "thread data passing error!!");
46 while (!thread->isStop)
49 ca_mutex_lock(thread->threadMutex);
51 // if queue is empty, thread will wait
52 if (!thread->isStop && u_queue_get_size(thread->dataQueue) <= 0)
54 OIC_LOG(DEBUG, TAG, "wait..");
57 ca_cond_wait(thread->threadCond, thread->threadMutex);
59 OIC_LOG(DEBUG, TAG, "wake up..");
66 ca_mutex_unlock(thread->threadMutex);
71 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
73 ca_mutex_unlock(thread->threadMutex);
80 thread->threadTask(message->msg);
83 if (NULL != thread->destroy)
85 thread->destroy(message->msg, message->size);
89 OICFree(message->msg);
95 // remove all remained list data.
96 while (u_queue_get_size(thread->dataQueue) > 0)
99 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
104 if (NULL != thread->destroy)
106 thread->destroy(message->msg, message->size);
110 OICFree(message->msg);
117 ca_mutex_lock(thread->threadMutex);
118 ca_cond_signal(thread->threadCond);
119 ca_mutex_unlock(thread->threadMutex);
121 OIC_LOG(DEBUG, TAG, "message handler main thread end..");
124 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, ca_thread_pool_t handle,
125 CAThreadTask task, CADataDestroyFunction destroy)
129 OIC_LOG(ERROR, TAG, "thread instance is empty..");
130 return CA_STATUS_INVALID_PARAM;
135 OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
136 return CA_STATUS_INVALID_PARAM;
139 OIC_LOG(DEBUG, TAG, "thread initialize..");
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 if(NULL == thread->dataQueue || NULL == thread->threadMutex || NULL == thread->threadCond)
150 goto ERROR_MEM_FAILURE;
154 if(thread->dataQueue)
156 u_queue_delete(thread->dataQueue);
157 thread->dataQueue = NULL;
159 if(thread->threadMutex)
161 ca_mutex_free(thread->threadMutex);
162 thread->threadMutex = NULL;
164 if(thread->threadCond)
166 ca_cond_free(thread->threadCond);
167 thread->threadCond = NULL;
169 return CA_MEMORY_ALLOC_FAILED;
173 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
177 OIC_LOG(ERROR, TAG, "thread instance is empty..");
178 return CA_STATUS_INVALID_PARAM;
181 if (NULL == thread->threadPool)
183 OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
184 return CA_STATUS_INVALID_PARAM;
187 if (false == thread->isStop) //Queueing thread already running
189 OIC_LOG(DEBUG, TAG, "queueing thread already running..");
194 ca_mutex_lock(thread->threadMutex);
195 thread->isStop = false;
197 ca_mutex_unlock(thread->threadMutex);
199 CAResult_t res = ca_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
201 if (res != CA_STATUS_OK)
203 OIC_LOG(ERROR, TAG, "thread pool add task error(send thread).");
209 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
213 OIC_LOG(ERROR, TAG, "thread instance is empty..");
214 return CA_STATUS_INVALID_PARAM;
217 if (NULL == data || 0 == size)
219 OIC_LOG(ERROR, TAG, "data is empty..");
221 return CA_STATUS_INVALID_PARAM;
224 // create thread data
225 u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
229 OIC_LOG(ERROR, TAG, "memory error!!");
230 return CA_MEMORY_ALLOC_FAILED;
234 message->size = size;
237 ca_mutex_lock(thread->threadMutex);
239 // add thread data into list
240 u_queue_add_element(thread->dataQueue, message);
243 ca_cond_signal(thread->threadCond);
246 ca_mutex_unlock(thread->threadMutex);
251 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
255 OIC_LOG(ERROR, TAG, "thread instance is empty..");
256 return CA_STATUS_INVALID_PARAM;
259 OIC_LOG(DEBUG, TAG, "thread destroy..");
261 ca_mutex_free(thread->threadMutex);
262 thread->threadMutex = NULL;
263 ca_cond_free(thread->threadCond);
264 u_queue_delete(thread->dataQueue);
269 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
273 OIC_LOG(ERROR, TAG, "thread instance is empty..");
274 return CA_STATUS_INVALID_PARAM;
277 OIC_LOG(DEBUG, TAG, "thread stop request!!");
282 ca_mutex_lock(thread->threadMutex);
285 thread->isStop = true;
288 ca_cond_signal(thread->threadCond);
290 ca_cond_wait(thread->threadCond, thread->threadMutex);
293 ca_mutex_unlock(thread->threadMutex);