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(DEBUG, 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..");
57 u_cond_wait(thread->threadCond, thread->threadMutex);
59 OIC_LOG_V(DEBUG, TAG, "wake up..");
63 u_mutex_unlock(thread->threadMutex);
70 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
72 void *data = message->msg;
75 thread->threadTask(data);
80 u_cond_signal(thread->threadCond);
82 OIC_LOG_V(DEBUG, TAG, "message handler main thread end..");
85 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, u_thread_pool_t handle,
90 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
91 return CA_STATUS_FAILED;
96 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
97 return CA_STATUS_FAILED;
100 OIC_LOG_V(DEBUG, TAG, "thread initialize..");
102 memset(thread, 0, sizeof(CAQueueingThread_t));
107 // set send thread data
108 thread->threadPool = handle;
109 thread->dataQueue = u_queue_create();
110 thread->threadMutex = u_mutex_new();
111 thread->threadCond = u_cond_new();
112 thread->isStop = CA_FALSE;
113 thread->threadTask = task;
118 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
122 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
123 return CA_STATUS_FAILED;
126 if (thread->threadPool == NULL)
128 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
129 return CA_STATUS_FAILED;
132 CAResult_t res = u_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
135 if (res != CA_STATUS_OK)
137 OIC_LOG_V(DEBUG, TAG, "thread pool add task error(send thread).");
144 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
148 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
149 return CA_STATUS_FAILED;
152 if (data == NULL || size == 0)
154 OIC_LOG_V(DEBUG, TAG, "data is empty..");
156 return CA_STATUS_FAILED;
159 // create thread data
160 u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
164 OIC_LOG_V(DEBUG, TAG, "memory error!!");
165 return CA_MEMORY_ALLOC_FAILED;
167 memset(message, 0, sizeof(u_queue_message_t));
170 message->size = sizeof(size);
173 u_mutex_lock(thread->threadMutex);
175 // add thread data into list
176 u_queue_add_element(thread->dataQueue, message);
179 u_cond_signal(thread->threadCond);
182 u_mutex_unlock(thread->threadMutex);
187 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
191 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
192 return CA_STATUS_FAILED;
195 OIC_LOG_V(DEBUG, TAG, "thread destroy..");
197 u_mutex_free(thread->threadMutex);
198 thread->threadMutex = NULL;
199 u_cond_free(thread->threadCond);
200 u_queue_delete(thread->dataQueue);
205 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
209 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
210 return CA_STATUS_FAILED;
213 OIC_LOG_V(DEBUG, TAG, "thread stop request!!");
216 u_mutex_lock(thread->threadMutex);
219 thread->isStop = CA_TRUE;
222 u_cond_signal(thread->threadCond);
224 u_cond_wait(thread->threadCond, thread->threadMutex);
227 u_mutex_unlock(thread->threadMutex);