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..");
58 u_cond_wait(thread->threadCond, thread->threadMutex);
60 OIC_LOG_V(DEBUG, TAG, "wake up..");
64 u_mutex_unlock(thread->threadMutex);
71 u_queue_message_t *message = u_queue_get_element(thread->dataQueue);
77 void *data = message->msg;
80 thread->threadTask(data);
86 u_cond_signal(thread->threadCond);
88 OIC_LOG_V(DEBUG, TAG, "message handler main thread end..");
91 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, u_thread_pool_t handle,
96 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
97 return CA_STATUS_FAILED;
102 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
103 return CA_STATUS_FAILED;
106 OIC_LOG_V(DEBUG, TAG, "thread initialize..");
108 memset(thread, 0, sizeof(CAQueueingThread_t));
113 // set send thread data
114 thread->threadPool = handle;
115 thread->dataQueue = u_queue_create();
116 thread->threadMutex = u_mutex_new();
117 thread->threadCond = u_cond_new();
118 thread->isStop = CA_TRUE;
119 thread->threadTask = task;
124 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread)
128 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
129 return CA_STATUS_FAILED;
132 if (thread->threadPool == NULL)
134 OIC_LOG_V(DEBUG, TAG, "thread pool handle is empty..");
135 return CA_STATUS_FAILED;
138 if (CA_FALSE == thread->isStop) //Queueing thread already running
140 OIC_LOG_V(DEBUG, TAG, "queueing thread already running..");
144 thread->isStop = CA_FALSE;
145 CAResult_t res = u_thread_pool_add_task(thread->threadPool, CAQueueingThreadBaseRoutine,
147 if (res != CA_STATUS_OK)
149 OIC_LOG_V(DEBUG, TAG, "thread pool add task error(send thread).");
150 thread->isStop = CA_TRUE;
157 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size)
161 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
162 return CA_STATUS_FAILED;
165 if (data == NULL || size == 0)
167 OIC_LOG_V(DEBUG, TAG, "data is empty..");
169 return CA_STATUS_FAILED;
172 // create thread data
173 u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
177 OIC_LOG_V(DEBUG, TAG, "memory error!!");
178 return CA_MEMORY_ALLOC_FAILED;
180 memset(message, 0, sizeof(u_queue_message_t));
183 message->size = sizeof(size);
186 u_mutex_lock(thread->threadMutex);
188 // add thread data into list
189 u_queue_add_element(thread->dataQueue, message);
192 u_cond_signal(thread->threadCond);
195 u_mutex_unlock(thread->threadMutex);
200 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread)
204 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
205 return CA_STATUS_FAILED;
208 OIC_LOG_V(DEBUG, TAG, "thread destroy..");
210 u_mutex_free(thread->threadMutex);
211 thread->threadMutex = NULL;
212 u_cond_free(thread->threadCond);
213 u_queue_delete(thread->dataQueue);
218 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread)
222 OIC_LOG_V(DEBUG, TAG, "thread instance is empty..");
223 return CA_STATUS_FAILED;
226 OIC_LOG_V(DEBUG, TAG, "thread stop request!!");
231 u_mutex_lock(thread->threadMutex);
234 thread->isStop = CA_TRUE;
237 u_cond_signal(thread->threadCond);
239 u_cond_wait(thread->threadCond, thread->threadMutex);
242 u_mutex_unlock(thread->threadMutex);