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 ca_mutex_lock(thread->threadMutex);
96 ca_cond_signal(thread->threadCond);
97 ca_mutex_unlock(thread->threadMutex);
99 OIC_LOG(DEBUG, TAG, "message handler main thread end..");
102 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, ca_thread_pool_t handle,
103 CAThreadTask task, CADataDestroyFunction destroy)
107 OIC_LOG(ERROR, TAG, "thread instance is empty..");
108 return CA_STATUS_INVALID_PARAM;
113 OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
114 return CA_STATUS_INVALID_PARAM;
117 OIC_LOG(DEBUG, TAG, "thread initialize..");
119 // set send thread data
120 thread->threadPool = handle;
121 thread->dataQueue = u_queue_create();
122 thread->threadMutex = ca_mutex_new();
123 thread->threadCond = ca_cond_new();
124 thread->isStop = true;
125 thread->threadTask = task;
126 thread->destroy = destroy;
127 if(NULL == thread->dataQueue || NULL == thread->threadMutex || NULL == thread->threadCond)
128 goto ERROR_MEM_FAILURE;
132 if(thread->dataQueue)
134 u_queue_delete(thread->dataQueue);
135 thread->dataQueue = NULL;
137 if(thread->threadMutex)
139 ca_mutex_free(thread->threadMutex);
140 thread->threadMutex = NULL;
142 if(thread->threadCond)
144 ca_cond_free(thread->threadCond);
145 thread->threadCond = NULL;
147 return CA_MEMORY_ALLOC_FAILED;
151 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
155 OIC_LOG(ERROR, TAG, "thread instance is empty..");
156 return CA_STATUS_INVALID_PARAM;
159 if (NULL == thread->threadPool)
161 OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
162 return CA_STATUS_INVALID_PARAM;
165 if (false == thread->isStop) //Queueing thread already running
167 OIC_LOG(DEBUG, TAG, "queueing thread already running..");
172 ca_mutex_lock(thread->threadMutex);
173 thread->isStop = false;
175 ca_mutex_unlock(thread->threadMutex);
177 CAResult_t res = ca_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
179 if (res != CA_STATUS_OK)
181 OIC_LOG(ERROR, TAG, "thread pool add task error(send thread).");
187 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
191 OIC_LOG(ERROR, TAG, "thread instance is empty..");
192 return CA_STATUS_INVALID_PARAM;
195 if (NULL == data || 0 == size)
197 OIC_LOG(ERROR, TAG, "data is empty..");
199 return CA_STATUS_INVALID_PARAM;
202 // create thread data
203 u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
207 OIC_LOG(ERROR, TAG, "memory error!!");
208 return CA_MEMORY_ALLOC_FAILED;
212 message->size = size;
215 ca_mutex_lock(thread->threadMutex);
217 // add thread data into list
218 u_queue_add_element(thread->dataQueue, message);
221 ca_cond_signal(thread->threadCond);
224 ca_mutex_unlock(thread->threadMutex);
229 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
233 OIC_LOG(ERROR, TAG, "thread instance is empty..");
234 return CA_STATUS_INVALID_PARAM;
237 OIC_LOG(DEBUG, TAG, "thread destroy..");
239 ca_mutex_free(thread->threadMutex);
240 thread->threadMutex = NULL;
241 ca_cond_free(thread->threadCond);
243 // remove all remained list data.
244 while (u_queue_get_size(thread->dataQueue) > 0)
247 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
252 if (NULL != thread->destroy)
254 thread->destroy(message->msg, message->size);
258 OICFree(message->msg);
265 u_queue_delete(thread->dataQueue);
270 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
274 OIC_LOG(ERROR, TAG, "thread instance is empty..");
275 return CA_STATUS_INVALID_PARAM;
278 OIC_LOG(DEBUG, TAG, "thread stop request!!");
283 ca_mutex_lock(thread->threadMutex);
286 thread->isStop = true;
289 ca_cond_signal(thread->threadCond);
291 ca_cond_wait(thread->threadCond, thread->threadMutex);
294 ca_mutex_unlock(thread->threadMutex);