util/u_queue: add util_queue_fence_wait_timeout
[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_signal(struct util_queue_fence *fence)
85 {
86 uint32_t val = p_atomic_xchg(&fence->val, 0);
87
88 assert(val != 0);
89
90 if (val == 2)
91 futex_wake(&fence->val, INT_MAX);
92 }
93
94 /**
95 * Move \p fence back into unsignalled state.
96 *
97 * \warning The caller must ensure that no other thread may currently be
98 * waiting (or about to wait) on the fence.
99 */
100 static inline void
101 util_queue_fence_reset(struct util_queue_fence *fence)
102 {
103 #ifdef NDEBUG
104 fence->val = 1;
105 #else
106 uint32_t v = p_atomic_xchg(&fence->val, 1);
107 assert(v == 0);
108 #endif
109 }
110
111 static inline bool
112 util_queue_fence_is_signalled(struct util_queue_fence *fence)
113 {
114 return fence->val == 0;
115 }
116 #endif
117
118 #ifdef UTIL_QUEUE_FENCE_STANDARD
119 /* Job completion fence.
120 * Put this into your job structure.
121 */
122 struct util_queue_fence {
123 mtx_t mutex;
124 cnd_t cond;
125 int signalled;
126 };
127
128 void util_queue_fence_init(struct util_queue_fence *fence);
129 void util_queue_fence_destroy(struct util_queue_fence *fence);
130 void util_queue_fence_signal(struct util_queue_fence *fence);
131
132 /**
133 * Move \p fence back into unsignalled state.
134 *
135 * \warning The caller must ensure that no other thread may currently be
136 * waiting (or about to wait) on the fence.
137 */
138 static inline void
139 util_queue_fence_reset(struct util_queue_fence *fence)
140 {
141 assert(fence->signalled);
142 fence->signalled = 0;
143 }
144
145 static inline bool
146 util_queue_fence_is_signalled(struct util_queue_fence *fence)
147 {
148 return fence->signalled != 0;
149 }
150 #endif
151
152 void
153 _util_queue_fence_wait(struct util_queue_fence *fence);
154
155 static inline void
156 util_queue_fence_wait(struct util_queue_fence *fence)
157 {
158 if (unlikely(!util_queue_fence_is_signalled(fence)))
159 _util_queue_fence_wait(fence);
160 }
161
162 bool
163 _util_queue_fence_wait_timeout(struct util_queue_fence *fence,
164 int64_t abs_timeout);
165
166 /**
167 * Wait for the fence to be signaled with a timeout.
168 *
169 * \param fence the fence
170 * \param abs_timeout the absolute timeout in nanoseconds, relative to the
171 * clock provided by os_time_get_nano.
172 *
173 * \return true if the fence was signaled, false if the timeout occurred.
174 */
175 static inline bool
176 util_queue_fence_wait_timeout(struct util_queue_fence *fence,
177 int64_t abs_timeout)
178 {
179 if (util_queue_fence_is_signalled(fence))
180 return true;
181
182 return _util_queue_fence_wait_timeout(fence, abs_timeout);
183 }
184
185 typedef void (*util_queue_execute_func)(void *job, int thread_index);
186
187 struct util_queue_job {
188 void *job;
189 struct util_queue_fence *fence;
190 util_queue_execute_func execute;
191 util_queue_execute_func cleanup;
192 };
193
194 /* Put this into your context. */
195 struct util_queue {
196 const char *name;
197 mtx_t lock;
198 cnd_t has_queued_cond;
199 cnd_t has_space_cond;
200 thrd_t *threads;
201 unsigned flags;
202 int num_queued;
203 unsigned num_threads;
204 int kill_threads;
205 int max_jobs;
206 int write_idx, read_idx; /* ring buffer pointers */
207 struct util_queue_job *jobs;
208
209 /* for cleanup at exit(), protected by exit_mutex */
210 struct list_head head;
211 };
212
213 bool util_queue_init(struct util_queue *queue,
214 const char *name,
215 unsigned max_jobs,
216 unsigned num_threads,
217 unsigned flags);
218 void util_queue_destroy(struct util_queue *queue);
219
220 /* optional cleanup callback is called after fence is signaled: */
221 void util_queue_add_job(struct util_queue *queue,
222 void *job,
223 struct util_queue_fence *fence,
224 util_queue_execute_func execute,
225 util_queue_execute_func cleanup);
226 void util_queue_drop_job(struct util_queue *queue,
227 struct util_queue_fence *fence);
228
229 void util_queue_finish(struct util_queue *queue);
230
231 int64_t util_queue_get_thread_time_nano(struct util_queue *queue,
232 unsigned thread_index);
233
234 /* util_queue needs to be cleared to zeroes for this to work */
235 static inline bool
236 util_queue_is_initialized(struct util_queue *queue)
237 {
238 return queue->threads != NULL;
239 }
240
241 /* Convenient structure for monitoring the queue externally and passing
242 * the structure between Mesa components. The queue doesn't use it directly.
243 */
244 struct util_queue_monitoring
245 {
246 /* For querying the thread busyness. */
247 struct util_queue *queue;
248
249 /* Counters updated by the user of the queue. */
250 unsigned num_offloaded_items;
251 unsigned num_direct_items;
252 unsigned num_syncs;
253 };
254
255 #ifdef __cplusplus
256 }
257 #endif
258
259 #endif