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-2022 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 <aerospike/as_std.h>
20 #include <citrusleaf/cf_queue.h>
21 #include <pthread.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 //---------------------------------
28 // Types
29 //---------------------------------
30 
31 /**
32  * @private
33  * Task function callback.
34  */
35 typedef void (*as_task_fn)(void* user_data);
36 
37 /**
38  * @private
39  * Thread finalization function callback.
40  */
41 typedef void (*as_fini_fn)(void);
42 
43 /**
44  * @private
45  * Thread pool.
46  */
47 typedef struct as_thread_pool_s {
48  pthread_t* threads;
49  cf_queue* dispatch_queue;
51  uint32_t thread_size;
53 
54 //---------------------------------
55 // Functions
56 //---------------------------------
57 
58 /**
59  * @private
60  * Initialize variable task thread pool and start thread_size threads.
61  * Multiple task types can be handled in variable task thread pools.
62  *
63  * Returns:
64  * 0 : Success
65  * -1 : Failed to initialize mutex lock
66  * -2 : Failed to lock mutex
67  * -3 : Some threads failed to start
68  */
69 int
70 as_thread_pool_init(as_thread_pool* pool, uint32_t thread_size);
71 
72 /**
73  * @private
74  * Queue a variable task onto thread pool.
75  *
76  * Returns:
77  * 0 : Success
78  * -1 : No threads are running to process task.
79  * -2 : Failed to push task onto dispatch queue
80  */
81 int
82 as_thread_pool_queue_task(as_thread_pool* pool, as_task_fn task_fn, void* task);
83 
84 /**
85  * @private
86  * Destroy thread pool.
87  *
88  * Returns:
89  * 0 : Success
90  * -1 : Failed to lock mutex
91  * -2 : Pool has already been closed
92  */
93 int
95 
96 #ifdef __cplusplus
97 } // end extern "C"
98 #endif
void(* as_fini_fn)(void)
as_fini_fn fini_fn
int as_thread_pool_destroy(as_thread_pool *pool)
int as_thread_pool_init(as_thread_pool *pool, uint32_t thread_size)
pthread_t * threads
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