gallium/util: import the multithreaded job queue from amdgpu winsys (v2)
[mesa.git] / src / gallium / auxiliary / util / u_queue.h
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 /* Job queue with execution in a separate thread.
28 *
29 * Jobs can be added from any thread. After that, the wait call can be used
30 * to wait for completion of the job.
31 */
32
33 #ifndef U_QUEUE_H
34 #define U_QUEUE_H
35
36 #include "os/os_thread.h"
37
38 /* Job completion fence.
39 * Put this into your job structure.
40 */
41 struct util_queue_fence {
42 pipe_semaphore done;
43 };
44
45 struct util_queue_job {
46 void *job;
47 struct util_queue_fence *fence;
48 };
49
50 /* Put this into your context. */
51 struct util_queue {
52 pipe_mutex lock;
53 pipe_semaphore has_space;
54 pipe_semaphore queued;
55 pipe_thread thread;
56 int kill_thread;
57 int num_jobs;
58 struct util_queue_job jobs[8];
59 void (*execute_job)(void *job);
60 };
61
62 void util_queue_init(struct util_queue *queue,
63 void (*execute_job)(void *));
64 void util_queue_destroy(struct util_queue *queue);
65 void util_queue_fence_init(struct util_queue_fence *fence);
66 void util_queue_fence_destroy(struct util_queue_fence *fence);
67
68 void util_queue_add_job(struct util_queue *queue,
69 void *job,
70 struct util_queue_fence *fence);
71 void util_queue_job_wait(struct util_queue_fence *fence);
72
73 /* util_queue needs to be cleared to zeroes for this to work */
74 static inline bool
75 util_queue_is_initialized(struct util_queue *queue)
76 {
77 return queue->thread != 0;
78 }
79
80 #endif