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