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 ******************************************************************/
24 * This file provides APIs related to thread pool.
30 #include "iotivity_config.h"
32 #if defined HAVE_WINSOCK2_H
35 #include "cathreadpool.h"
37 #include "oic_malloc.h"
38 #include "uarraylist.h"
40 #include "platform_features.h"
42 #define TAG PCF("OIC_CA_UTHREADPOOL")
45 * empty struct to represent the details. This implementation has no data
46 * that it needs to keep track of, so it only uses NULL for the internal value.
48 typedef struct ca_thread_pool_details_t
50 u_arraylist_t* threads_list;
52 } ca_thread_pool_details_t;
55 * struct to wrap the pthreads callback properly. The function pointer for
56 * pthreads requires a void* return value, however u_thread_func is a void.
58 typedef struct ca_thread_pool_callback_info_t
62 } ca_thread_pool_callback_info_t;
64 typedef struct ca_thread_pool_thread_info_t
67 } ca_thread_pool_thread_info_t;
69 // passthrough function to convert the pthreads call to a u_thread_func call
70 void* ca_thread_pool_pthreads_delegate(void* data)
72 ca_thread_pool_callback_info_t* info = (ca_thread_pool_callback_info_t*)data;
73 info->func(info->data);
78 // this implementation doesn't do a thread pool, so this function is essentially
79 // a no-op besides creating a valid ca_thread_pool_t object. It was determined after
80 // reading through the existing implementation that the thread-pooling was unnecessary
81 // for the posix platforms. Behavior shouldn't be changed since previously num_of_threads
82 // was greater than the number of requested threads.
83 CAResult_t ca_thread_pool_init(int32_t num_of_threads, ca_thread_pool_t *thread_pool)
85 OIC_LOG(DEBUG, TAG, "IN");
89 OIC_LOG(ERROR, TAG, "Parameter thread_pool was null!");
90 return CA_STATUS_INVALID_PARAM;
93 if(num_of_threads <= 0)
95 OIC_LOG(ERROR, TAG, "num_of_threads must be positive and non-zero");
96 return CA_STATUS_INVALID_PARAM;
99 *thread_pool = OICMalloc(sizeof(struct ca_thread_pool));
103 OIC_LOG(ERROR, TAG, "Failed to allocate for thread-pool");
104 return CA_MEMORY_ALLOC_FAILED;
107 (*thread_pool)->details = OICMalloc(sizeof(struct ca_thread_pool_details_t));
108 if(!(*thread_pool)->details)
110 OIC_LOG(ERROR, TAG, "Failed to allocate for thread-pool details");
111 OICFree(*thread_pool);
113 return CA_MEMORY_ALLOC_FAILED;
116 (*thread_pool)->details->list_lock = oc_mutex_new();
118 if(!(*thread_pool)->details->list_lock)
120 OIC_LOG(ERROR, TAG, "Failed to create thread-pool mutex");
124 (*thread_pool)->details->threads_list = u_arraylist_create();
126 if(!(*thread_pool)->details->threads_list)
128 OIC_LOG(ERROR, TAG, "Failed to create thread-pool list");
129 if(!oc_mutex_free((*thread_pool)->details->list_lock))
131 OIC_LOG(ERROR, TAG, "Failed to free thread-pool mutex");
136 OIC_LOG(DEBUG, TAG, "OUT");
140 OICFree((*thread_pool)->details);
141 OICFree(*thread_pool);
143 return CA_STATUS_FAILED;
146 CAResult_t ca_thread_pool_add_task(ca_thread_pool_t thread_pool, ca_thread_func method,
149 OIC_LOG(DEBUG, TAG, "IN");
151 if(NULL == thread_pool || NULL == method)
153 OIC_LOG(ERROR, TAG, "thread_pool or method was NULL");
154 return CA_STATUS_INVALID_PARAM;
157 ca_thread_pool_callback_info_t* info = OICMalloc(sizeof(ca_thread_pool_callback_info_t));
160 OIC_LOG(ERROR, TAG, "Failed to allocate for memory wrapper");
161 return CA_MEMORY_ALLOC_FAILED;
167 ca_thread_pool_thread_info_t *threadInfo =
168 (ca_thread_pool_thread_info_t *) OICCalloc(1, sizeof(ca_thread_pool_thread_info_t));
171 OIC_LOG(ERROR, TAG, "Memory allocation failed");
173 return CA_STATUS_FAILED;
176 oc_mutex_lock(thread_pool->details->list_lock);
177 bool addResult = u_arraylist_add(thread_pool->details->threads_list, (void*) threadInfo);
180 // Note that this is considered non-fatal.
181 oc_mutex_unlock(thread_pool->details->list_lock);
182 OIC_LOG(ERROR, TAG, "Arraylist add failed");
185 return CA_STATUS_FAILED;
188 int thrRet = oc_thread_new(&threadInfo->thread, ca_thread_pool_pthreads_delegate, info);
192 if (u_arraylist_get_index(thread_pool->details->threads_list, threadInfo, &index))
194 u_arraylist_remove(thread_pool->details->threads_list, index);
196 oc_mutex_unlock(thread_pool->details->list_lock);
197 OIC_LOG_V(ERROR, TAG, "Thread start failed with error %d", thrRet);
199 return CA_STATUS_FAILED;
201 oc_mutex_unlock(thread_pool->details->list_lock);
203 OIC_LOG(DEBUG, TAG, "OUT");
207 void ca_thread_pool_free(ca_thread_pool_t thread_pool)
209 OIC_LOG(DEBUG, TAG, "IN");
213 OIC_LOG(ERROR, TAG, "Invalid parameter thread_pool was NULL");
217 oc_mutex_lock(thread_pool->details->list_lock);
219 for (uint32_t i = 0; i < u_arraylist_length(thread_pool->details->threads_list); ++i)
221 ca_thread_pool_thread_info_t *threadInfo = (ca_thread_pool_thread_info_t *)
222 u_arraylist_get(thread_pool->details->threads_list, i);
225 if (threadInfo->thread)
227 oc_thread_wait(threadInfo->thread);
228 oc_thread_free(threadInfo->thread);
234 u_arraylist_free(&(thread_pool->details->threads_list));
236 oc_mutex_unlock(thread_pool->details->list_lock);
237 oc_mutex_free(thread_pool->details->list_lock);
239 OICFree(thread_pool->details);
240 OICFree(thread_pool);
242 OIC_LOG(DEBUG, TAG, "OUT");