replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / inc / cathreadpool.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  *
24  * This file provides APIs related to thread pool.  Implementations are provided
25  * by adding a new .c file for each implementation, and adding them conditionally
26  * via the SCONS build script.  Currently, cathreadpool_pthreads.c is implemented,
27  * with cathreadpool_winthreads.c being considered.  RTOS implementations should use
28  * a name that best describes the used technology, not the OS.
29  */
30
31 #ifndef CA_THREAD_POOL_H_
32 #define CA_THREAD_POOL_H_
33
34 #include "cacommon.h"
35
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif // __cplusplus
40
41 /**
42  * Callback type can be registered to thread pool.
43  */
44 typedef void (*ca_thread_func)(void *);
45
46 struct ca_thread_pool_details_t;
47 /**
48  * Thread pool type.
49  */
50 typedef struct ca_thread_pool
51 {
52     struct ca_thread_pool_details_t* details;
53 }*ca_thread_pool_t;
54
55 /**
56  * This function creates a newly allocated thread pool.
57  *
58  * @param num_of_threads The number of worker thread used in this pool.
59  * @param thread_pool_handle Handle to newly create thread pool.
60  * @return Error code, CA_STATUS_OK if success, else error number.
61  */
62 CAResult_t ca_thread_pool_init(int32_t num_of_threads, ca_thread_pool_t *thread_pool_handle);
63
64 /**
65  * This function adds a routine to be executed by the thread pool at some future time.
66  *
67  * @param thread_pool The thread pool structure.
68  * @param method The routine to be executed.
69  * @param data The data to be passed to the routine.
70  * @param taskId An unique identifier of task.
71  *
72  * @return CA_STATUS_OK on success.
73  * @return Error on failure.
74  */
75 #ifndef __TIZENRT__
76 CAResult_t ca_thread_pool_add_task(ca_thread_pool_t thread_pool, ca_thread_func method, void *data,
77                                    uint32_t *taskId);
78 #else
79 CAResult_t ca_thread_pool_add_task(ca_thread_pool_t thread_pool, ca_thread_func method, void *data,
80                                    uint32_t *taskId, const char *task_name, int stack_size);
81 #endif
82
83 /**
84  * This function removes a routine to be executed by the thread pool.
85  *
86  * @param thread_pool The thread pool structure.
87  * @param taskId An unique identifier of task.
88  *
89  * @return CA_STATUS_OK on success.
90  * @return Error on failure.
91  */
92 CAResult_t ca_thread_pool_remove_task(ca_thread_pool_t thread_pool, uint32_t taskId);
93
94 /**
95  * This function stops all the worker threads (stop & exit). And frees all the allocated memory.
96  * Function will return only after joining all threads executing the currently scheduled tasks.
97  *
98  * @param thread_pool The thread pool structure.
99  */
100 void ca_thread_pool_free(ca_thread_pool_t thread_pool);
101
102 #ifdef __cplusplus
103 } /* extern "C" */
104 #endif /* __cplusplus */
105
106 #endif /* CA_THREAD_POOL_H_ */
107