1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ******************************************************************/
21 * @brief This file provides APIs related to thread pool
24 #include "uthreadpool.h"
26 #include "oic_malloc.h"
27 #define TAG PCF("UTHREADPOOL")
31 * @brief Glib thread pool.
33 static GThreadPool *gThreadpool;
37 * @brief function which is registed to glib thread pool.
39 static void run(void *thread_data, void *user_data);
41 CAResult_t u_thread_pool_init(uint32_t num_of_threads, u_thread_pool_t *thread_pool)
43 OIC_LOG_V(DEBUG, TAG, "IN");
46 gThreadpool = g_thread_pool_new(run, NULL, num_of_threads, FALSE, &error);
47 if (NULL == gThreadpool)
49 OIC_LOG_V(ERROR, TAG, "Error: g_thread_pool_new failed!");
52 OIC_LOG_V(ERROR, TAG, "Error is: %s", error->message);
55 return CA_STATUS_FAILED;
57 *thread_pool = (u_thread_pool_t) gThreadpool;
59 OIC_LOG_V(DEBUG, TAG, "OUT");
63 CAResult_t u_thread_pool_add_task(u_thread_pool_t thread_pool, void (*routine)(void *), void *data)
65 OIC_LOG_V(DEBUG, TAG, "IN");
67 gboolean result = FALSE;
70 OIC_LOG_V(ERROR, TAG, "Error: routine is NULL!");
71 return CA_STATUS_FAILED;
74 u_thread_msg_t *message = (u_thread_msg_t *) OICMalloc(sizeof(u_thread_msg_t));
76 message->func = routine;
77 result = g_thread_pool_push((GThreadPool *) thread_pool, (void *) message, NULL);
80 OIC_LOG_V(ERROR, TAG, "Error: Failed to push the task to threadpool!");
81 return CA_STATUS_FAILED;
84 OIC_LOG_V(DEBUG, TAG, "OUT");
88 void u_thread_pool_free(u_thread_pool_t thread_pool)
90 OIC_LOG_V(DEBUG, TAG, "IN");
92 GThreadPool *threadpool = (GThreadPool *) thread_pool;
93 g_thread_pool_free(threadpool, TRUE, TRUE);
95 OIC_LOG_V(DEBUG, TAG, "OUT");
98 void run(void *thread_data, void *user_data)
100 u_thread_msg_t *message = (u_thread_msg_t *) thread_data;
102 if (message && message->func)
104 OIC_LOG_V(DEBUG, TAG, "Calling routine with data as parameter");
105 message->func(message->data);
109 OIC_LOG_V(ERROR, TAG, "Error: Invalid task data");