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 "caqueueingthread.h"
26 #include "oic_malloc.h"
31 static void CAQueueingThreadBaseRoutine(void* treadValue)
33 OIC_LOG_V(DEBUG, TAG, "message handler main thread start..");
35 CAQueueingThread_t* thread = (CAQueueingThread_t*) treadValue;
39 OIC_LOG_V(DEBUG, TAG, "thread data passing error!!");
44 while (!thread->isStop)
47 u_mutex_lock(thread->threadMutex);
49 // if queue is empty, thread will wait
50 if (u_queue_get_size(thread->dataQueue) <= 0)
52 OIC_LOG_V(DEBUG, TAG, "wait..");
54 u_cond_wait(thread->threadCond, thread->threadMutex);
56 OIC_LOG_V(DEBUG, TAG, "wake up..");
60 u_mutex_unlock(thread->threadMutex);
67 u_queue_message_t* message = u_queue_get_element(thread->dataQueue);
69 void* data = message->msg;
72 thread->threadTask(data);
77 OIC_LOG_V(DEBUG, TAG, "message handler main thread end..");
80 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t* thread, u_thread_pool_t handle,
85 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
86 return CA_STATUS_FAILED;
91 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
92 return CA_STATUS_FAILED;
95 OIC_LOG_V(DEBUG, TAG, "thread initialize..");
97 memset(thread, 0, sizeof(CAQueueingThread_t));
102 // set send thread data
103 thread->threadPool = handle;
104 thread->dataQueue = u_queue_create();
105 thread->threadMutex = u_mutex_new();
106 thread->threadCond = u_cond_new();
107 thread->isStop = CA_FALSE;
108 thread->threadTask = task;
113 CAResult_t CAQueueingThreadStart(CAQueueingThread_t* thread)
117 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
118 return CA_STATUS_FAILED;
121 if (thread->threadPool == NULL)
123 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
124 return CA_STATUS_FAILED;
127 CAResult_t res = u_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
130 if (res != CA_STATUS_OK)
132 OIC_LOG_V(DEBUG, TAG, "thread pool add task error(send thread).");
139 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t* thread, void* data, uint32_t size)
143 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
144 return CA_STATUS_FAILED;
147 if (data == NULL || size == 0)
149 OIC_LOG_V(DEBUG, TAG, "data is empty..");
151 return CA_STATUS_FAILED;
154 // create thread data
155 u_queue_message_t* message = (u_queue_message_t*) OICMalloc(sizeof(u_queue_message_t));
159 OIC_LOG_V(DEBUG, TAG, "memory error!!");
160 return CA_MEMORY_ALLOC_FAILED;
162 memset(message, 0, sizeof(u_queue_message_t));
165 message->size = sizeof(size);
168 u_mutex_lock(thread->threadMutex);
170 // add thread data into list
171 u_queue_add_element(thread->dataQueue, message);
174 u_cond_signal(thread->threadCond);
177 u_mutex_unlock(thread->threadMutex);
182 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t* thread)
186 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
187 return CA_STATUS_FAILED;
190 OIC_LOG_V(DEBUG, TAG, "thread destroy..");
192 u_mutex_free(thread->threadMutex);
193 u_cond_free(thread->threadCond);
194 u_queue_delete(thread->dataQueue);
199 CAResult_t CAQueueingThreadStop(CAQueueingThread_t* thread)
203 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
204 return CA_STATUS_FAILED;
207 OIC_LOG_V(DEBUG, TAG, "thread stop request!!");
210 u_mutex_lock(thread->threadMutex);
213 thread->isStop = CA_TRUE;
216 u_cond_signal(thread->threadCond);
219 u_mutex_unlock(thread->threadMutex);