All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_thread_pool.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2016 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <citrusleaf/cf_queue.h>
20 #include <pthread.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /******************************************************************************
27  * TYPES
28  *****************************************************************************/
29 
30 /**
31  * @private
32  * Task function callback.
33  */
34 typedef void (*as_task_fn)(void* user_data);
35 
36 /**
37  * @private
38  * Thread pool.
39  */
40 typedef struct as_thread_pool_s {
41  pthread_mutex_t lock;
42  cf_queue* dispatch_queue;
43  cf_queue* complete_queue;
45  uint32_t task_size;
47  uint32_t thread_size;
48  uint32_t initialized;
50 
51 /******************************************************************************
52  * FUNCTIONS
53  *****************************************************************************/
54 
55 /**
56  * @private
57  * Initialize variable task thread pool and start thread_size threads.
58  * Multiple task types can be handled in variable task thread pools.
59  *
60  * Returns:
61  * 0 : Success
62  * -1 : Failed to initialize mutex lock
63  * -2 : Failed to lock mutex
64  * -3 : Some threads failed to start
65  */
66 int
67 as_thread_pool_init(as_thread_pool* pool, uint32_t thread_size);
68 
69 /**
70  * @private
71  * Initialize fixed task thread pool and start thread_size threads.
72  * Only one task type structure can be handled in fixed task thread pools.
73  * Fixed task thread pools do save an extra malloc when queuing the task,
74  * because a shallow copy is made when pushing the task onto the queue.
75  *
76  * Returns:
77  * 0 : Success
78  * -1 : Failed to initialize mutex lock
79  * -2 : Failed to lock mutex
80  * -3 : Some threads failed to start
81  */
82 int
83 as_thread_pool_init_fixed(as_thread_pool* pool, uint32_t thread_size, as_task_fn task_fn,
84  uint32_t task_size, uint32_t task_complete_offset);
85 
86 /**
87  * @private
88  * Resize number of running threads in thread pool.
89  *
90  * Returns:
91  * 0 : Success
92  * -1 : Failed to lock mutex
93  * -2 : Pool has already been closed
94  * -3 : Some threads failed to start
95  */
96 int
97 as_thread_pool_resize(as_thread_pool* pool, uint32_t thread_size);
98 
99 /**
100  * @private
101  * Queue a variable task onto thread pool.
102  *
103  * Returns:
104  * 0 : Success
105  * -1 : No threads are running to process task.
106  * -2 : Failed to push task onto dispatch queue
107  */
108 int
109 as_thread_pool_queue_task(as_thread_pool* pool, as_task_fn task_fn, void* task);
110 
111 /**
112  * @private
113  * Queue a fixed task onto thread pool.
114  *
115  * Returns:
116  * 0 : Success
117  * -1 : No threads are running to process task.
118  * -2 : Failed to push task onto dispatch queue
119  */
120 int
122 
123 /**
124  * @private
125  * Destroy thread pool.
126  *
127  * Returns:
128  * 0 : Success
129  * -1 : Failed to lock mutex
130  * -2 : Pool has already been closed
131  */
132 int
134 
135 #ifdef __cplusplus
136 } // end extern "C"
137 #endif