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.
28 #include "cathreadpool.h"
30 #include "oic_malloc.h"
32 #define TAG PCF("UTHREADPOOL")
35 * empty struct to represent the details. This implementation has no data
36 * that it needs to keep track of, so it only uses NULL for the internal value.
38 typedef struct ca_thread_pool_details_t
40 } ca_thread_pool_details_t;
43 * struct to wrap the pthreads callback properly. The function pointer for
44 * pthreads requires a void* return value, however u_thread_func is a void.
46 typedef struct ca_thread_pool_callback_info_t
50 } ca_thread_pool_callback_info_t;
52 // passthrough function to convert the pthreads call to a u_thread_func call
53 void* ca_thread_pool_pthreads_delegate(void* data)
55 ca_thread_pool_callback_info_t* info = (ca_thread_pool_callback_info_t*)data;
56 info->func(info->data);
61 // this implementation doesn't do a thread pool, so this function is essentially
62 // a no-op besides creating a valid ca_thread_pool_t object. It was determined after
63 // reading through the existing implementation that the thread-pooling was unnecessary
64 // for the posix platforms. Behavior shouldn't be changed since previously num_of_threads
65 // was greater than the number of requested threads.
66 CAResult_t ca_thread_pool_init(int32_t num_of_threads, ca_thread_pool_t *thread_pool)
68 OIC_LOG(DEBUG, TAG, "IN");
72 OIC_LOG(ERROR, TAG, "Parameter thraed_pool was null!");
73 return CA_STATUS_INVALID_PARAM;
76 if(num_of_threads <= 0)
78 OIC_LOG(ERROR, TAG, "num_of_threads must be positive and non-zero");
79 return CA_STATUS_INVALID_PARAM;
82 *thread_pool = OICMalloc(sizeof(struct ca_thread_pool));
86 OIC_LOG(ERROR, TAG, "Failed to allocate for thread-pool");
87 return CA_MEMORY_ALLOC_FAILED;
90 OIC_LOG(DEBUG, TAG, "OUT");
94 CAResult_t ca_thread_pool_add_task(ca_thread_pool_t thread_pool, ca_thread_func method,
97 OIC_LOG(DEBUG, TAG, "IN");
99 if(NULL == thread_pool || NULL == method)
101 OIC_LOG(ERROR, TAG, "thread_pool or method was NULL");
102 return CA_STATUS_INVALID_PARAM;
105 ca_thread_pool_callback_info_t* info = OICMalloc(sizeof(ca_thread_pool_callback_info_t));
108 OIC_LOG(ERROR, TAG, "Failed to allocate for memory wrapper");
109 return CA_MEMORY_ALLOC_FAILED;
115 pthread_t threadHandle;
117 int result = pthread_create(&threadHandle, NULL, ca_thread_pool_pthreads_delegate, info);
121 OIC_LOG_V(ERROR, TAG, "Thread start failed with error %d", result);
122 return CA_STATUS_FAILED;
125 // detach will cause the thread to either terminate normally and clean up after
126 // itself, which prevents us from having to do any manual join/cleanup later, or
127 // it will be terminated upon application exit.
128 result = pthread_detach(threadHandle);
132 OIC_LOG_V(ERROR, TAG, "Thread detach failed with error %d", result);
133 return CA_STATUS_FAILED;
136 OIC_LOG(DEBUG, TAG, "OUT");
140 void ca_thread_pool_free(ca_thread_pool_t thread_pool)
142 OIC_LOG(DEBUG, TAG, "IN");
144 OICFree(thread_pool);
145 OIC_LOG(DEBUG, TAG, "OUT");