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 finalization function callback.
39  */
40 typedef void (*as_fini_fn)();
41 
42 /**
43  * @private
44  * Thread pool.
45  */
46 typedef struct as_thread_pool_s {
47  pthread_mutex_t lock;
48  cf_queue* dispatch_queue;
49  cf_queue* complete_queue;
52  uint32_t task_size;
54  uint32_t thread_size;
55  uint32_t initialized;
57 
58 /******************************************************************************
59  * FUNCTIONS
60  *****************************************************************************/
61 
62 /**
63  * @private
64  * Initialize variable task thread pool and start thread_size threads.
65  * Multiple task types can be handled in variable task thread pools.
66  *
67  * Returns:
68  * 0 : Success
69  * -1 : Failed to initialize mutex lock
70  * -2 : Failed to lock mutex
71  * -3 : Some threads failed to start
72  */
73 int
74 as_thread_pool_init(as_thread_pool* pool, uint32_t thread_size);
75 
76 /**
77  * @private
78  * Initialize fixed task thread pool and start thread_size threads.
79  * Only one task type structure can be handled in fixed task thread pools.
80  * Fixed task thread pools do save an extra malloc when queuing the task,
81  * because a shallow copy is made when pushing the task onto the queue.
82  *
83  * Returns:
84  * 0 : Success
85  * -1 : Failed to initialize mutex lock
86  * -2 : Failed to lock mutex
87  * -3 : Some threads failed to start
88  */
89 int
90 as_thread_pool_init_fixed(as_thread_pool* pool, uint32_t thread_size, as_task_fn task_fn,
91  uint32_t task_size, uint32_t task_complete_offset);
92 
93 /**
94  * @private
95  * Resize number of running threads in thread pool.
96  *
97  * Returns:
98  * 0 : Success
99  * -1 : Failed to lock mutex
100  * -2 : Pool has already been closed
101  * -3 : Some threads failed to start
102  */
103 int
104 as_thread_pool_resize(as_thread_pool* pool, uint32_t thread_size);
105 
106 /**
107  * @private
108  * Queue a variable task onto thread pool.
109  *
110  * Returns:
111  * 0 : Success
112  * -1 : No threads are running to process task.
113  * -2 : Failed to push task onto dispatch queue
114  */
115 int
116 as_thread_pool_queue_task(as_thread_pool* pool, as_task_fn task_fn, void* task);
117 
118 /**
119  * @private
120  * Queue a fixed task onto thread pool.
121  *
122  * Returns:
123  * 0 : Success
124  * -1 : No threads are running to process task.
125  * -2 : Failed to push task onto dispatch queue
126  */
127 int
129 
130 /**
131  * @private
132  * Destroy thread pool.
133  *
134  * Returns:
135  * 0 : Success
136  * -1 : Failed to lock mutex
137  * -2 : Pool has already been closed
138  */
139 int
141 
142 #ifdef __cplusplus
143 } // end extern "C"
144 #endif
int as_thread_pool_init_fixed(as_thread_pool *pool, uint32_t thread_size, as_task_fn task_fn, uint32_t task_size, uint32_t task_complete_offset)
uint32_t task_size
pthread_mutex_t lock
cf_queue * complete_queue
uint32_t task_complete_offset
as_fini_fn fini_fn
uint32_t initialized
int as_thread_pool_destroy(as_thread_pool *pool)
int as_thread_pool_init(as_thread_pool *pool, uint32_t thread_size)
as_task_fn task_fn
cf_queue * dispatch_queue
int as_thread_pool_queue_task(as_thread_pool *pool, as_task_fn task_fn, void *task)
void(* as_task_fn)(void *user_data)
uint32_t thread_size
int as_thread_pool_queue_task_fixed(as_thread_pool *pool, void *task)
void(* as_fini_fn)()
int as_thread_pool_resize(as_thread_pool *pool, uint32_t thread_size)