Implementation of connectivity abstraction feature Release v0.3
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / inc / uthreadpool.h
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  ******************************************************************/
19 /**
20  * @file    uthreadpool.h
21  * @brief   This file provides APIs related to thread pool
22  */
23
24 #ifndef __UTHREAD_POOL_H_
25 #define __UTHREAD_POOL_H_
26
27 #include <stdio.h>
28 #include <malloc.h>
29 #include <glib.h>
30
31 #include "cacommon.h"
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #endif /* __cplusplus */
37
38 /**
39  * @var u_thread_func
40  * @brief Callback type can be registered to thread pool.
41  */
42 typedef void (*u_thread_func)(void *);
43
44 /**
45  * @struct u_thread_msg_t
46  * @brief Structure to maintain the data which needs to send to task function.
47  */
48 typedef struct
49 {
50     void *data;
51     u_thread_func func;
52 } u_thread_msg_t;
53
54 /**
55  * @var u_thread_pool_t
56  * @brief Thread pool type.
57  */
58 typedef void *u_thread_pool_t;
59
60 /**
61  * This function creates a newly allocated thread pool.
62  *
63  * @param num_of_threads The number of worker thread used in this pool.
64  * @param thread_pool_handle Handle to newly create thread pool.
65  * @return Error code, CA_STATUS_OK if success, else error number.
66  */
67 CAResult_t u_thread_pool_init(uint32_t num_of_threads, u_thread_pool_t *thread_pool_handle);
68
69 /**
70  * This function adds a routine to be executed by the thread pool at some future time.
71  *
72  * @param thread_pool The thread pool structure.
73  * @param routine The routine to be executed.
74  * @param data The data to be passed to the routine.
75  *
76  * @return CA_STATUS_OK on success.
77  * @return Error on failure.
78  */
79 CAResult_t u_thread_pool_add_task(u_thread_pool_t thread_pool, void (*routine)(void *), void *data);
80
81 /**
82  * This function stops all the worker threads (stop & exit). And frees all the allocated memory.
83  * Function will return only after joining all threads executing the currently scheduled tasks.
84  *
85  * @param thread_pool The thread pool structure.
86  */
87 void u_thread_pool_free(u_thread_pool_t thread_pool);
88
89 #ifdef __cplusplus
90 } /* extern "C" */
91 #endif /* __cplusplus */
92
93 #endif /* __UTHREAD_POOL_H_ */