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 ******************************************************************/
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
31 #include "caqueueingthread.h"
32 #include "oic_malloc.h"
35 #define TAG PCF("OIC_CA_QING")
37 static void CAQueueingThreadBaseRoutine(void *threadValue)
39 OIC_LOG(DEBUG, TAG, "message handler main thread start..");
41 CAQueueingThread_t *thread = (CAQueueingThread_t *) threadValue;
45 OIC_LOG(ERROR, TAG, "thread data passing error!!");
50 while (!thread->isStop)
53 ca_mutex_lock(thread->threadMutex);
55 // if queue is empty, thread will wait
56 if (!thread->isStop && u_queue_get_size(thread->dataQueue) <= 0)
58 OIC_LOG(DEBUG, TAG, "wait..");
61 ca_cond_wait(thread->threadCond, thread->threadMutex);
63 OIC_LOG(DEBUG, TAG, "wake up..");
70 ca_mutex_unlock(thread->threadMutex);
75 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
77 ca_mutex_unlock(thread->threadMutex);
84 thread->threadTask(message->msg);
87 if (NULL != thread->destroy)
89 thread->destroy(message->msg, message->size);
93 OICFree(message->msg);
99 ca_mutex_lock(thread->threadMutex);
100 ca_cond_signal(thread->threadCond);
101 ca_mutex_unlock(thread->threadMutex);
103 OIC_LOG(DEBUG, TAG, "message handler main thread end..");
106 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, ca_thread_pool_t handle,
107 CAThreadTask task, CADataDestroyFunction destroy)
111 OIC_LOG(ERROR, TAG, "thread instance is empty..");
112 return CA_STATUS_INVALID_PARAM;
117 OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
118 return CA_STATUS_INVALID_PARAM;
121 OIC_LOG(DEBUG, TAG, "thread initialize..");
123 // set send thread data
124 thread->threadPool = handle;
125 thread->dataQueue = u_queue_create();
126 thread->threadMutex = ca_mutex_new();
127 thread->threadCond = ca_cond_new();
128 thread->isStop = true;
129 thread->threadTask = task;
130 thread->destroy = destroy;
131 if(NULL == thread->dataQueue || NULL == thread->threadMutex || NULL == thread->threadCond)
132 goto ERROR_MEM_FAILURE;
136 if(thread->dataQueue)
138 u_queue_delete(thread->dataQueue);
139 thread->dataQueue = NULL;
141 if(thread->threadMutex)
143 ca_mutex_free(thread->threadMutex);
144 thread->threadMutex = NULL;
146 if(thread->threadCond)
148 ca_cond_free(thread->threadCond);
149 thread->threadCond = NULL;
151 return CA_MEMORY_ALLOC_FAILED;
155 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
159 OIC_LOG(ERROR, TAG, "thread instance is empty..");
160 return CA_STATUS_INVALID_PARAM;
163 if (NULL == thread->threadPool)
165 OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
166 return CA_STATUS_INVALID_PARAM;
169 if (false == thread->isStop) //Queueing thread already running
171 OIC_LOG(DEBUG, TAG, "queueing thread already running..");
176 ca_mutex_lock(thread->threadMutex);
177 thread->isStop = false;
179 ca_mutex_unlock(thread->threadMutex);
181 CAResult_t res = ca_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
183 if (res != CA_STATUS_OK)
185 OIC_LOG(ERROR, TAG, "thread pool add task error(send thread).");
191 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
195 OIC_LOG(ERROR, TAG, "thread instance is empty..");
196 return CA_STATUS_INVALID_PARAM;
199 if (NULL == data || 0 == size)
201 OIC_LOG(ERROR, TAG, "data is empty..");
203 return CA_STATUS_INVALID_PARAM;
206 // create thread data
207 u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
211 OIC_LOG(ERROR, TAG, "memory error!!");
212 return CA_MEMORY_ALLOC_FAILED;
216 message->size = size;
219 ca_mutex_lock(thread->threadMutex);
221 // add thread data into list
222 u_queue_add_element(thread->dataQueue, message);
225 ca_cond_signal(thread->threadCond);
228 ca_mutex_unlock(thread->threadMutex);
233 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
237 OIC_LOG(ERROR, TAG, "thread instance is empty..");
238 return CA_STATUS_INVALID_PARAM;
241 OIC_LOG(DEBUG, TAG, "thread destroy..");
243 ca_mutex_free(thread->threadMutex);
244 thread->threadMutex = NULL;
245 ca_cond_free(thread->threadCond);
247 // remove all remained list data.
248 while (u_queue_get_size(thread->dataQueue) > 0)
251 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
256 if (NULL != thread->destroy)
258 thread->destroy(message->msg, message->size);
262 OICFree(message->msg);
269 u_queue_delete(thread->dataQueue);
274 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
278 OIC_LOG(ERROR, TAG, "thread instance is empty..");
279 return CA_STATUS_INVALID_PARAM;
282 OIC_LOG(DEBUG, TAG, "thread stop request!!");
287 ca_mutex_lock(thread->threadMutex);
290 thread->isStop = true;
293 ca_cond_signal(thread->threadCond);
295 ca_cond_wait(thread->threadCond, thread->threadMutex);
298 ca_mutex_unlock(thread->threadMutex);