util/queue: don't hang at exit
[mesa.git] / src / util / u_queue.c
1 /*
2 * Copyright © 2016 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26
27 #include "u_queue.h"
28 #include "util/u_string.h"
29
30 static void util_queue_killall_and_wait(struct util_queue *queue);
31
32 /****************************************************************************
33 * Wait for all queues to assert idle when exit() is called.
34 *
35 * Otherwise, C++ static variable destructors can be called while threads
36 * are using the static variables.
37 */
38
39 static once_flag atexit_once_flag = ONCE_FLAG_INIT;
40 static struct list_head queue_list;
41 static mtx_t exit_mutex = _MTX_INITIALIZER_NP;
42
43 static void
44 atexit_handler(void)
45 {
46 struct util_queue *iter;
47
48 mtx_lock(&exit_mutex);
49 /* Wait for all queues to assert idle. */
50 LIST_FOR_EACH_ENTRY(iter, &queue_list, head) {
51 util_queue_killall_and_wait(iter);
52 }
53 mtx_unlock(&exit_mutex);
54 }
55
56 static void
57 global_init(void)
58 {
59 LIST_INITHEAD(&queue_list);
60 atexit(atexit_handler);
61 }
62
63 static void
64 add_to_atexit_list(struct util_queue *queue)
65 {
66 call_once(&atexit_once_flag, global_init);
67
68 mtx_lock(&exit_mutex);
69 LIST_ADD(&queue->head, &queue_list);
70 mtx_unlock(&exit_mutex);
71 }
72
73 static void
74 remove_from_atexit_list(struct util_queue *queue)
75 {
76 struct util_queue *iter, *tmp;
77
78 mtx_lock(&exit_mutex);
79 LIST_FOR_EACH_ENTRY_SAFE(iter, tmp, &queue_list, head) {
80 if (iter == queue) {
81 LIST_DEL(&iter->head);
82 break;
83 }
84 }
85 mtx_unlock(&exit_mutex);
86 }
87
88 /****************************************************************************
89 * util_queue_fence
90 */
91
92 static void
93 util_queue_fence_signal(struct util_queue_fence *fence)
94 {
95 mtx_lock(&fence->mutex);
96 fence->signalled = true;
97 cnd_broadcast(&fence->cond);
98 mtx_unlock(&fence->mutex);
99 }
100
101 void
102 util_queue_fence_wait(struct util_queue_fence *fence)
103 {
104 mtx_lock(&fence->mutex);
105 while (!fence->signalled)
106 cnd_wait(&fence->cond, &fence->mutex);
107 mtx_unlock(&fence->mutex);
108 }
109
110 void
111 util_queue_fence_init(struct util_queue_fence *fence)
112 {
113 memset(fence, 0, sizeof(*fence));
114 (void) mtx_init(&fence->mutex, mtx_plain);
115 cnd_init(&fence->cond);
116 fence->signalled = true;
117 }
118
119 void
120 util_queue_fence_destroy(struct util_queue_fence *fence)
121 {
122 assert(fence->signalled);
123 cnd_destroy(&fence->cond);
124 mtx_destroy(&fence->mutex);
125 }
126
127 /****************************************************************************
128 * util_queue implementation
129 */
130
131 struct thread_input {
132 struct util_queue *queue;
133 int thread_index;
134 };
135
136 static int
137 util_queue_thread_func(void *input)
138 {
139 struct util_queue *queue = ((struct thread_input*)input)->queue;
140 int thread_index = ((struct thread_input*)input)->thread_index;
141
142 free(input);
143
144 if (queue->name) {
145 char name[16];
146 util_snprintf(name, sizeof(name), "%s:%i", queue->name, thread_index);
147 u_thread_setname(name);
148 }
149
150 while (1) {
151 struct util_queue_job job;
152
153 mtx_lock(&queue->lock);
154 assert(queue->num_queued >= 0 && queue->num_queued <= queue->max_jobs);
155
156 /* wait if the queue is empty */
157 while (!queue->kill_threads && queue->num_queued == 0)
158 cnd_wait(&queue->has_queued_cond, &queue->lock);
159
160 if (queue->kill_threads) {
161 mtx_unlock(&queue->lock);
162 break;
163 }
164
165 job = queue->jobs[queue->read_idx];
166 memset(&queue->jobs[queue->read_idx], 0, sizeof(struct util_queue_job));
167 queue->read_idx = (queue->read_idx + 1) % queue->max_jobs;
168
169 queue->num_queued--;
170 cnd_signal(&queue->has_space_cond);
171 mtx_unlock(&queue->lock);
172
173 if (job.job) {
174 job.execute(job.job, thread_index);
175 util_queue_fence_signal(job.fence);
176 if (job.cleanup)
177 job.cleanup(job.job, thread_index);
178 }
179 }
180
181 /* signal remaining jobs before terminating */
182 mtx_lock(&queue->lock);
183 while (queue->jobs[queue->read_idx].job) {
184 util_queue_fence_signal(queue->jobs[queue->read_idx].fence);
185
186 queue->jobs[queue->read_idx].job = NULL;
187 queue->read_idx = (queue->read_idx + 1) % queue->max_jobs;
188 }
189 queue->num_queued = 0; /* reset this when exiting the thread */
190 mtx_unlock(&queue->lock);
191 return 0;
192 }
193
194 bool
195 util_queue_init(struct util_queue *queue,
196 const char *name,
197 unsigned max_jobs,
198 unsigned num_threads)
199 {
200 unsigned i;
201
202 memset(queue, 0, sizeof(*queue));
203 queue->name = name;
204 queue->num_threads = num_threads;
205 queue->max_jobs = max_jobs;
206
207 queue->jobs = (struct util_queue_job*)
208 calloc(max_jobs, sizeof(struct util_queue_job));
209 if (!queue->jobs)
210 goto fail;
211
212 (void) mtx_init(&queue->lock, mtx_plain);
213
214 queue->num_queued = 0;
215 cnd_init(&queue->has_queued_cond);
216 cnd_init(&queue->has_space_cond);
217
218 queue->threads = (thrd_t*) calloc(num_threads, sizeof(thrd_t));
219 if (!queue->threads)
220 goto fail;
221
222 /* start threads */
223 for (i = 0; i < num_threads; i++) {
224 struct thread_input *input =
225 (struct thread_input *) malloc(sizeof(struct thread_input));
226 input->queue = queue;
227 input->thread_index = i;
228
229 queue->threads[i] = u_thread_create(util_queue_thread_func, input);
230
231 if (!queue->threads[i]) {
232 free(input);
233
234 if (i == 0) {
235 /* no threads created, fail */
236 goto fail;
237 } else {
238 /* at least one thread created, so use it */
239 queue->num_threads = i;
240 break;
241 }
242 }
243 }
244
245 add_to_atexit_list(queue);
246 return true;
247
248 fail:
249 free(queue->threads);
250
251 if (queue->jobs) {
252 cnd_destroy(&queue->has_space_cond);
253 cnd_destroy(&queue->has_queued_cond);
254 mtx_destroy(&queue->lock);
255 free(queue->jobs);
256 }
257 /* also util_queue_is_initialized can be used to check for success */
258 memset(queue, 0, sizeof(*queue));
259 return false;
260 }
261
262 static void
263 util_queue_killall_and_wait(struct util_queue *queue)
264 {
265 unsigned i;
266
267 /* Signal all threads to terminate. */
268 mtx_lock(&queue->lock);
269 queue->kill_threads = 1;
270 cnd_broadcast(&queue->has_queued_cond);
271 mtx_unlock(&queue->lock);
272
273 for (i = 0; i < queue->num_threads; i++)
274 thrd_join(queue->threads[i], NULL);
275 queue->num_threads = 0;
276 }
277
278 void
279 util_queue_destroy(struct util_queue *queue)
280 {
281 util_queue_killall_and_wait(queue);
282 remove_from_atexit_list(queue);
283
284 cnd_destroy(&queue->has_space_cond);
285 cnd_destroy(&queue->has_queued_cond);
286 mtx_destroy(&queue->lock);
287 free(queue->jobs);
288 free(queue->threads);
289 }
290
291 void
292 util_queue_add_job(struct util_queue *queue,
293 void *job,
294 struct util_queue_fence *fence,
295 util_queue_execute_func execute,
296 util_queue_execute_func cleanup)
297 {
298 struct util_queue_job *ptr;
299
300 assert(fence->signalled);
301
302 mtx_lock(&queue->lock);
303 if (queue->kill_threads) {
304 mtx_unlock(&queue->lock);
305 /* well no good option here, but any leaks will be
306 * short-lived as things are shutting down..
307 */
308 return;
309 }
310
311 fence->signalled = false;
312
313 assert(queue->num_queued >= 0 && queue->num_queued <= queue->max_jobs);
314
315 /* if the queue is full, wait until there is space */
316 while (queue->num_queued == queue->max_jobs)
317 cnd_wait(&queue->has_space_cond, &queue->lock);
318
319 ptr = &queue->jobs[queue->write_idx];
320 assert(ptr->job == NULL);
321 ptr->job = job;
322 ptr->fence = fence;
323 ptr->execute = execute;
324 ptr->cleanup = cleanup;
325 queue->write_idx = (queue->write_idx + 1) % queue->max_jobs;
326
327 queue->num_queued++;
328 cnd_signal(&queue->has_queued_cond);
329 mtx_unlock(&queue->lock);
330 }
331
332 int64_t
333 util_queue_get_thread_time_nano(struct util_queue *queue, unsigned thread_index)
334 {
335 /* Allow some flexibility by not raising an error. */
336 if (thread_index >= queue->num_threads)
337 return 0;
338
339 return u_thread_get_time_nano(queue->threads[thread_index]);
340 }