2 * Copyright © 2016 Advanced Micro Devices, Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
27 /* Job queue with execution in a separate thread.
29 * Jobs can be added from any thread. After that, the wait call can be used
30 * to wait for completion of the job.
38 #include "util/list.h"
39 #include "util/u_thread.h"
45 #define UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY (1 << 0)
46 #define UTIL_QUEUE_INIT_RESIZE_IF_FULL (1 << 1)
48 /* Job completion fence.
49 * Put this into your job structure.
51 struct util_queue_fence
{
57 typedef void (*util_queue_execute_func
)(void *job
, int thread_index
);
59 struct util_queue_job
{
61 struct util_queue_fence
*fence
;
62 util_queue_execute_func execute
;
63 util_queue_execute_func cleanup
;
66 /* Put this into your context. */
70 cnd_t has_queued_cond
;
78 int write_idx
, read_idx
; /* ring buffer pointers */
79 struct util_queue_job
*jobs
;
81 /* for cleanup at exit(), protected by exit_mutex */
82 struct list_head head
;
85 bool util_queue_init(struct util_queue
*queue
,
90 void util_queue_destroy(struct util_queue
*queue
);
91 void util_queue_fence_init(struct util_queue_fence
*fence
);
92 void util_queue_fence_destroy(struct util_queue_fence
*fence
);
94 /* optional cleanup callback is called after fence is signaled: */
95 void util_queue_add_job(struct util_queue
*queue
,
97 struct util_queue_fence
*fence
,
98 util_queue_execute_func execute
,
99 util_queue_execute_func cleanup
);
100 void util_queue_drop_job(struct util_queue
*queue
,
101 struct util_queue_fence
*fence
);
103 void util_queue_fence_wait(struct util_queue_fence
*fence
);
104 int64_t util_queue_get_thread_time_nano(struct util_queue
*queue
,
105 unsigned thread_index
);
107 /* util_queue needs to be cleared to zeroes for this to work */
109 util_queue_is_initialized(struct util_queue
*queue
)
111 return queue
->threads
!= NULL
;
115 util_queue_fence_is_signalled(struct util_queue_fence
*fence
)
117 return fence
->signalled
!= 0;
120 /* Convenient structure for monitoring the queue externally and passing
121 * the structure between Mesa components. The queue doesn't use it directly.
123 struct util_queue_monitoring
125 /* For querying the thread busyness. */
126 struct util_queue
*queue
;
128 /* Counters updated by the user of the queue. */
129 unsigned num_offloaded_items
;
130 unsigned num_direct_items
;