a3e12260e302538d4e6839a1bcc2156cb10f227d
[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/list.h"
39 #include "util/u_thread.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 #define UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY (1 << 0)
46 #define UTIL_QUEUE_INIT_RESIZE_IF_FULL (1 << 1)
47
48 /* Job completion fence.
49 * Put this into your job structure.
50 */
51 struct util_queue_fence {
52 mtx_t mutex;
53 cnd_t cond;
54 int signalled;
55 };
56
57 void util_queue_fence_init(struct util_queue_fence *fence);
58 void util_queue_fence_destroy(struct util_queue_fence *fence);
59 void util_queue_fence_wait(struct util_queue_fence *fence);
60 void util_queue_fence_signal(struct util_queue_fence *fence);
61
62 /**
63 * Move \p fence back into unsignalled state.
64 *
65 * \warning The caller must ensure that no other thread may currently be
66 * waiting (or about to wait) on the fence.
67 */
68 static inline void
69 util_queue_fence_reset(struct util_queue_fence *fence)
70 {
71 assert(fence->signalled);
72 fence->signalled = 0;
73 }
74
75 static inline bool
76 util_queue_fence_is_signalled(struct util_queue_fence *fence)
77 {
78 return fence->signalled != 0;
79 }
80
81 typedef void (*util_queue_execute_func)(void *job, int thread_index);
82
83 struct util_queue_job {
84 void *job;
85 struct util_queue_fence *fence;
86 util_queue_execute_func execute;
87 util_queue_execute_func cleanup;
88 };
89
90 /* Put this into your context. */
91 struct util_queue {
92 const char *name;
93 mtx_t lock;
94 cnd_t has_queued_cond;
95 cnd_t has_space_cond;
96 thrd_t *threads;
97 unsigned flags;
98 int num_queued;
99 unsigned num_threads;
100 int kill_threads;
101 int max_jobs;
102 int write_idx, read_idx; /* ring buffer pointers */
103 struct util_queue_job *jobs;
104
105 /* for cleanup at exit(), protected by exit_mutex */
106 struct list_head head;
107 };
108
109 bool util_queue_init(struct util_queue *queue,
110 const char *name,
111 unsigned max_jobs,
112 unsigned num_threads,
113 unsigned flags);
114 void util_queue_destroy(struct util_queue *queue);
115
116 /* optional cleanup callback is called after fence is signaled: */
117 void util_queue_add_job(struct util_queue *queue,
118 void *job,
119 struct util_queue_fence *fence,
120 util_queue_execute_func execute,
121 util_queue_execute_func cleanup);
122 void util_queue_drop_job(struct util_queue *queue,
123 struct util_queue_fence *fence);
124
125 int64_t util_queue_get_thread_time_nano(struct util_queue *queue,
126 unsigned thread_index);
127
128 /* util_queue needs to be cleared to zeroes for this to work */
129 static inline bool
130 util_queue_is_initialized(struct util_queue *queue)
131 {
132 return queue->threads != NULL;
133 }
134
135 /* Convenient structure for monitoring the queue externally and passing
136 * the structure between Mesa components. The queue doesn't use it directly.
137 */
138 struct util_queue_monitoring
139 {
140 /* For querying the thread busyness. */
141 struct util_queue *queue;
142
143 /* Counters updated by the user of the queue. */
144 unsigned num_offloaded_items;
145 unsigned num_direct_items;
146 unsigned num_syncs;
147 };
148
149 #ifdef __cplusplus
150 }
151 #endif
152
153 #endif