1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/*
* Copyright © 2016 Advanced Micro Devices, Inc.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
* AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*/
#include "u_queue.h"
#include "u_memory.h"
#include "u_string.h"
#include "os/os_time.h"
static void
util_queue_fence_signal(struct util_queue_fence *fence)
{
pipe_mutex_lock(fence->mutex);
fence->signalled = true;
pipe_condvar_broadcast(fence->cond);
pipe_mutex_unlock(fence->mutex);
}
void
util_queue_job_wait(struct util_queue_fence *fence)
{
if (fence->signalled)
return;
pipe_mutex_lock(fence->mutex);
while (!fence->signalled)
pipe_condvar_wait(fence->cond, fence->mutex);
pipe_mutex_unlock(fence->mutex);
}
struct thread_input {
struct util_queue *queue;
int thread_index;
};
static PIPE_THREAD_ROUTINE(util_queue_thread_func, input)
{
struct util_queue *queue = ((struct thread_input*)input)->queue;
int thread_index = ((struct thread_input*)input)->thread_index;
FREE(input);
if (queue->name) {
char name[16];
util_snprintf(name, sizeof(name), "%s:%i", queue->name, thread_index);
pipe_thread_setname(name);
}
while (1) {
struct util_queue_job job;
pipe_semaphore_wait(&queue->queued);
if (queue->kill_threads)
break;
pipe_mutex_lock(queue->lock);
job = queue->jobs[queue->read_idx];
queue->jobs[queue->read_idx].job = NULL;
queue->read_idx = (queue->read_idx + 1) % queue->max_jobs;
pipe_mutex_unlock(queue->lock);
pipe_semaphore_signal(&queue->has_space);
if (job.job) {
queue->execute_job(job.job, thread_index);
util_queue_fence_signal(job.fence);
}
}
/* signal remaining jobs before terminating */
pipe_mutex_lock(queue->lock);
while (queue->jobs[queue->read_idx].job) {
util_queue_fence_signal(queue->jobs[queue->read_idx].fence);
queue->jobs[queue->read_idx].job = NULL;
queue->read_idx = (queue->read_idx + 1) % queue->max_jobs;
}
pipe_mutex_unlock(queue->lock);
return 0;
}
bool
util_queue_init(struct util_queue *queue,
const char *name,
unsigned max_jobs,
unsigned num_threads,
void (*execute_job)(void *, int))
{
unsigned i;
memset(queue, 0, sizeof(*queue));
queue->name = name;
queue->num_threads = num_threads;
queue->max_jobs = max_jobs;
queue->jobs = (struct util_queue_job*)
CALLOC(max_jobs, sizeof(struct util_queue_job));
if (!queue->jobs)
goto fail;
queue->execute_job = execute_job;
pipe_mutex_init(queue->lock);
pipe_semaphore_init(&queue->has_space, max_jobs);
pipe_semaphore_init(&queue->queued, 0);
queue->threads = (pipe_thread*)CALLOC(num_threads, sizeof(pipe_thread));
if (!queue->threads)
goto fail;
/* start threads */
for (i = 0; i < num_threads; i++) {
struct thread_input *input = MALLOC_STRUCT(thread_input);
input->queue = queue;
input->thread_index = i;
queue->threads[i] = pipe_thread_create(util_queue_thread_func, input);
if (!queue->threads[i]) {
FREE(input);
if (i == 0) {
/* no threads created, fail */
goto fail;
} else {
/* at least one thread created, so use it */
queue->num_threads = i+1;
break;
}
}
}
return true;
fail:
FREE(queue->threads);
if (queue->jobs) {
pipe_semaphore_destroy(&queue->has_space);
pipe_semaphore_destroy(&queue->queued);
pipe_mutex_destroy(queue->lock);
FREE(queue->jobs);
}
/* also util_queue_is_initialized can be used to check for success */
memset(queue, 0, sizeof(*queue));
return false;
}
void
util_queue_destroy(struct util_queue *queue)
{
unsigned i;
/* Signal all threads to terminate. */
pipe_mutex_lock(queue->queued.mutex);
queue->kill_threads = 1;
queue->queued.counter = queue->num_threads;
pipe_condvar_broadcast(queue->queued.cond);
pipe_mutex_unlock(queue->queued.mutex);
for (i = 0; i < queue->num_threads; i++)
pipe_thread_wait(queue->threads[i]);
pipe_semaphore_destroy(&queue->has_space);
pipe_semaphore_destroy(&queue->queued);
pipe_mutex_destroy(queue->lock);
FREE(queue->jobs);
FREE(queue->threads);
}
void
util_queue_fence_init(struct util_queue_fence *fence)
{
memset(fence, 0, sizeof(*fence));
pipe_mutex_init(fence->mutex);
pipe_condvar_init(fence->cond);
fence->signalled = true;
}
void
util_queue_fence_destroy(struct util_queue_fence *fence)
{
pipe_condvar_destroy(fence->cond);
pipe_mutex_destroy(fence->mutex);
}
void
util_queue_add_job(struct util_queue *queue,
void *job,
struct util_queue_fence *fence)
{
struct util_queue_job *ptr;
assert(fence->signalled);
fence->signalled = false;
/* if the queue is full, wait until there is space */
pipe_semaphore_wait(&queue->has_space);
pipe_mutex_lock(queue->lock);
ptr = &queue->jobs[queue->write_idx];
assert(ptr->job == NULL);
ptr->job = job;
ptr->fence = fence;
queue->write_idx = (queue->write_idx + 1) % queue->max_jobs;
pipe_mutex_unlock(queue->lock);
pipe_semaphore_signal(&queue->queued);
}
|