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"
34 static void CAQueueingThreadBaseRoutine(void *threadValue)
36 OIC_LOG_V(DEBUG, TAG, "message handler main thread start..");
38 CAQueueingThread_t *thread = (CAQueueingThread_t *) threadValue;
42 OIC_LOG_V(ERROR, TAG, "thread data passing error!!");
47 while (!thread->isStop)
50 u_mutex_lock(thread->threadMutex);
52 // if queue is empty, thread will wait
53 if (u_queue_get_size(thread->dataQueue) <= 0)
55 OIC_LOG_V(DEBUG, TAG, "wait..");
58 u_cond_wait(thread->threadCond, thread->threadMutex);
60 OIC_LOG_V(DEBUG, TAG, "wake up..");
64 u_mutex_unlock(thread->threadMutex);
73 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
80 thread->threadTask(message->msg);
83 if (thread->destroy != NULL)
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);
102 if(message != NULL) {
103 if (thread->destroy != NULL)
105 thread->destroy(message->msg, message->size);
109 OICFree(message->msg);
116 u_cond_signal(thread->threadCond);
118 OIC_LOG_V(DEBUG, TAG, "message handler main thread end..");
121 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, u_thread_pool_t handle,
122 CAThreadTask task, CADataDestroyFunction destroy)
126 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
127 return CA_STATUS_FAILED;
132 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
133 return CA_STATUS_FAILED;
136 OIC_LOG_V(DEBUG, TAG, "thread initialize..");
138 // set send thread data
139 thread->threadPool = handle;
140 thread->dataQueue = u_queue_create();
141 thread->threadMutex = u_mutex_new();
142 thread->threadCond = u_cond_new();
143 thread->isStop = true;
144 thread->threadTask = task;
145 thread->destroy = destroy;
150 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
154 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
155 return CA_STATUS_FAILED;
158 if (thread->threadPool == NULL)
160 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
161 return CA_STATUS_FAILED;
164 if (false == thread->isStop) //Queueing thread already running
166 OIC_LOG_V(DEBUG, TAG, "queueing thread already running..");
171 u_mutex_lock(thread->threadMutex);
172 thread->isStop = false;
174 u_mutex_unlock(thread->threadMutex);
176 CAResult_t res = u_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
178 if (res != CA_STATUS_OK)
180 OIC_LOG_V(DEBUG, TAG, "thread pool add task error(send thread).");
186 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
190 OIC_LOG_V(ERROR, TAG, "thread instance is empty..");
191 return CA_STATUS_FAILED;
194 if (data == NULL || size == 0)
196 OIC_LOG_V(ERROR, TAG, "data is empty..");
198 return CA_STATUS_FAILED;
201 // create thread data
202 u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
206 OIC_LOG_V(ERROR, TAG, "memory error!!");
207 return CA_MEMORY_ALLOC_FAILED;
211 message->size = size;
214 u_mutex_lock(thread->threadMutex);
216 // add thread data into list
217 u_queue_add_element(thread->dataQueue, message);
220 u_cond_signal(thread->threadCond);
223 u_mutex_unlock(thread->threadMutex);
228 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
232 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
233 return CA_STATUS_FAILED;
236 OIC_LOG_V(DEBUG, TAG, "thread destroy..");
238 u_mutex_free(thread->threadMutex);
239 thread->threadMutex = NULL;
240 u_cond_free(thread->threadCond);
241 u_queue_delete(thread->dataQueue);
246 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
250 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
251 return CA_STATUS_FAILED;
254 OIC_LOG_V(DEBUG, TAG, "thread stop request!!");
259 u_mutex_lock(thread->threadMutex);
262 thread->isStop = true;
265 u_cond_signal(thread->threadCond);
267 u_cond_wait(thread->threadCond, thread->threadMutex);
270 u_mutex_unlock(thread->threadMutex);