u_queue: add a futex-based implementation of fences
[mesa.git] / src / 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 <string.h>
37
38 #include "util/futex.h"
39 #include "util/list.h"
40 #include "util/macros.h"
41 #include "util/u_atomic.h"
42 #include "util/u_thread.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #define UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY (1 << 0)
49 #define UTIL_QUEUE_INIT_RESIZE_IF_FULL (1 << 1)
50
51 #if defined(__GNUC__) && defined(HAVE_FUTEX)
52 #define UTIL_QUEUE_FENCE_FUTEX
53 #else
54 #define UTIL_QUEUE_FENCE_STANDARD
55 #endif
56
57 #ifdef UTIL_QUEUE_FENCE_FUTEX
58 /* Job completion fence.
59 * Put this into your job structure.
60 */
61 struct util_queue_fence {
62 /* The fence can be in one of three states:
63 * 0 - signaled
64 * 1 - unsignaled
65 * 2 - unsignaled, may have waiters
66 */
67 uint32_t val;
68 };
69
70 static inline void
71 util_queue_fence_init(struct util_queue_fence *fence)
72 {
73 fence->val = 0;
74 }
75
76 static inline void
77 util_queue_fence_destroy(struct util_queue_fence *fence)
78 {
79 assert(fence->val == 0);
80 /* no-op */
81 }
82
83 static inline void
84 util_queue_fence_wait(struct util_queue_fence *fence)
85 {
86 uint32_t v = fence->val;
87
88 if (likely(v == 0))
89 return;
90
91 do {
92 if (v != 2) {
93 v = p_atomic_cmpxchg(&fence->val, 1, 2);
94 if (v == 0)
95 return;
96 }
97
98 futex_wait(&fence->val, 2);
99 v = fence->val;
100 } while(v != 0);
101 }
102
103 static inline void
104 util_queue_fence_signal(struct util_queue_fence *fence)
105 {
106 uint32_t val = p_atomic_xchg(&fence->val, 0);
107
108 assert(val != 0);
109
110 if (val == 2)
111 futex_wake(&fence->val, INT_MAX);
112 }
113
114 /**
115 * Move \p fence back into unsignalled state.
116 *
117 * \warning The caller must ensure that no other thread may currently be
118 * waiting (or about to wait) on the fence.
119 */
120 static inline void
121 util_queue_fence_reset(struct util_queue_fence *fence)
122 {
123 #ifdef NDEBUG
124 fence->val = 1;
125 #else
126 uint32_t v = p_atomic_xchg(&fence->val, 1);
127 assert(v == 0);
128 #endif
129 }
130
131 static inline bool
132 util_queue_fence_is_signalled(struct util_queue_fence *fence)
133 {
134 return fence->val == 0;
135 }
136 #endif
137
138 #ifdef UTIL_QUEUE_FENCE_STANDARD
139 /* Job completion fence.
140 * Put this into your job structure.
141 */
142 struct util_queue_fence {
143 mtx_t mutex;
144 cnd_t cond;
145 int signalled;
146 };
147
148 void util_queue_fence_init(struct util_queue_fence *fence);
149 void util_queue_fence_destroy(struct util_queue_fence *fence);
150 void util_queue_fence_wait(struct util_queue_fence *fence);
151 void util_queue_fence_signal(struct util_queue_fence *fence);
152
153 /**
154 * Move \p fence back into unsignalled state.
155 *
156 * \warning The caller must ensure that no other thread may currently be
157 * waiting (or about to wait) on the fence.
158 */
159 static inline void
160 util_queue_fence_reset(struct util_queue_fence *fence)
161 {
162 assert(fence->signalled);
163 fence->signalled = 0;
164 }
165
166 static inline bool
167 util_queue_fence_is_signalled(struct util_queue_fence *fence)
168 {
169 return fence->signalled != 0;
170 }
171 #endif
172
173 typedef void (*util_queue_execute_func)(void *job, int thread_index);
174
175 struct util_queue_job {
176 void *job;
177 struct util_queue_fence *fence;
178 util_queue_execute_func execute;
179 util_queue_execute_func cleanup;
180 };
181
182 /* Put this into your context. */
183 struct util_queue {
184 const char *name;
185 mtx_t lock;
186 cnd_t has_queued_cond;
187 cnd_t has_space_cond;
188 thrd_t *threads;
189 unsigned flags;
190 int num_queued;
191 unsigned num_threads;
192 int kill_threads;
193 int max_jobs;
194 int write_idx, read_idx; /* ring buffer pointers */
195 struct util_queue_job *jobs;
196
197 /* for cleanup at exit(), protected by exit_mutex */
198 struct list_head head;
199 };
200
201 bool util_queue_init(struct util_queue *queue,
202 const char *name,
203 unsigned max_jobs,
204 unsigned num_threads,
205 unsigned flags);
206 void util_queue_destroy(struct util_queue *queue);
207
208 /* optional cleanup callback is called after fence is signaled: */
209 void util_queue_add_job(struct util_queue *queue,
210 void *job,
211 struct util_queue_fence *fence,
212 util_queue_execute_func execute,
213 util_queue_execute_func cleanup);
214 void util_queue_drop_job(struct util_queue *queue,
215 struct util_queue_fence *fence);
216
217 int64_t util_queue_get_thread_time_nano(struct util_queue *queue,
218 unsigned thread_index);
219
220 /* util_queue needs to be cleared to zeroes for this to work */
221 static inline bool
222 util_queue_is_initialized(struct util_queue *queue)
223 {
224 return queue->threads != NULL;
225 }
226
227 /* Convenient structure for monitoring the queue externally and passing
228 * the structure between Mesa components. The queue doesn't use it directly.
229 */
230 struct util_queue_monitoring
231 {
232 /* For querying the thread busyness. */
233 struct util_queue *queue;
234
235 /* Counters updated by the user of the queue. */
236 unsigned num_offloaded_items;
237 unsigned num_direct_items;
238 unsigned num_syncs;
239 };
240
241 #ifdef __cplusplus
242 }
243 #endif
244
245 #endif