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 ******************************************************************/
22 * @brief This file provides APIs related to thread pool
25 #include "uthreadpool.h"
27 #include "oic_malloc.h"
29 #define TAG PCF("UTHREADPOOL")
33 * @brief Glib thread pool.
35 static GThreadPool *gThreadpool = NULL;
39 * @brief function which is registed to glib thread pool.
41 static void run(void *thread_data, void *user_data);
43 CAResult_t u_thread_pool_init(uint32_t num_of_threads, u_thread_pool_t *thread_pool)
45 OIC_LOG(DEBUG, TAG, "IN");
50 //If not intialized, gthreadpool intialize check fails
54 gThreadpool = g_thread_pool_new(run, NULL, num_of_threads, TRUE, &error);
55 if (NULL == gThreadpool)
57 OIC_LOG(ERROR, TAG, "g_thread_pool_new failed!");
60 OIC_LOG_V(ERROR, TAG, "Error is: %s", error->message);
63 return CA_STATUS_FAILED;
66 *thread_pool = (u_thread_pool_t) gThreadpool;
68 OIC_LOG(DEBUG, TAG, "OUT");
72 CAResult_t u_thread_pool_add_task(u_thread_pool_t thread_pool, void (*routine)(void *),
75 OIC_LOG(DEBUG, TAG, "IN");
79 OIC_LOG(ERROR, TAG, "routine is NULL!");
80 return CA_STATUS_FAILED;
83 u_thread_msg_t *message = (u_thread_msg_t *) OICMalloc(sizeof(u_thread_msg_t));
86 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
87 return CA_MEMORY_ALLOC_FAILED;
91 message->func = routine;
93 g_thread_pool_push((GThreadPool *) thread_pool, (void *) message, NULL);
95 OIC_LOG(DEBUG, TAG, "OUT");
99 void u_thread_pool_free(u_thread_pool_t thread_pool)
101 OIC_LOG(DEBUG, TAG, "IN");
102 if (NULL == thread_pool)
104 OIC_LOG(DEBUG, TAG, "thread_pool is NULL. Its already freed.");
108 GThreadPool *threadpool = (GThreadPool *) thread_pool;
109 g_thread_pool_free(threadpool, TRUE, TRUE);
111 OIC_LOG(DEBUG, TAG, "OUT");
114 void run(void *thread_data, void *user_data)
116 u_thread_msg_t *message = (u_thread_msg_t *) thread_data;
120 OIC_LOG(ERROR, TAG, "Invalid task data");
126 OIC_LOG(DEBUG, TAG, "Calling routine with data as parameter");
127 message->func(message->data);