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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/*
* Copyright 2016 Patrick Rudolph <siro@das-labor.org>
*
* 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
* on 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 above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* 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 AUTHOR(S) AND/OR THEIR 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. */
#include "nine_queue.h"
#include "os/os_thread.h"
#include "util/macros.h"
#include "nine_helpers.h"
#define NINE_CMD_BUF_INSTR (256)
#define NINE_CMD_BUFS (32)
#define NINE_CMD_BUFS_MASK (NINE_CMD_BUFS - 1)
#define NINE_QUEUE_SIZE (8192 * 16 + 128)
#define DBG_CHANNEL DBG_DEVICE
/*
* Single producer - single consumer pool queue
*
* Producer:
* Calls nine_queue_alloc to get a slice of memory in current cmdbuf.
* Calls nine_queue_flush to flush the queue by request.
* The queue is flushed automatically on insufficient space or once the
* cmdbuf contains NINE_CMD_BUF_INSTR instructions.
*
* nine_queue_flush does block, while nine_queue_alloc doesn't block.
*
* nine_queue_alloc returns NULL on insufficent space.
*
* Consumer:
* Calls nine_queue_wait_flush to wait for a cmdbuf.
* After waiting for a cmdbuf it calls nine_queue_get until NULL is returned.
*
* nine_queue_wait_flush does block, while nine_queue_get doesn't block.
*
* Constrains:
* Only a single consumer and a single producer are supported.
*
*/
struct nine_cmdbuf {
unsigned instr_size[NINE_CMD_BUF_INSTR];
unsigned num_instr;
unsigned offset;
void *mem_pool;
BOOL full;
};
struct nine_queue_pool {
struct nine_cmdbuf pool[NINE_CMD_BUFS];
unsigned head;
unsigned tail;
unsigned cur_instr;
BOOL worker_wait;
pipe_condvar event_pop;
pipe_condvar event_push;
mtx_t mutex_pop;
mtx_t mutex_push;
};
/* Consumer functions: */
void
nine_queue_wait_flush(struct nine_queue_pool* ctx)
{
struct nine_cmdbuf *cmdbuf = &ctx->pool[ctx->tail];
/* wait for cmdbuf full */
pipe_mutex_lock(ctx->mutex_push);
while (!cmdbuf->full)
{
DBG("waiting for full cmdbuf\n");
cnd_wait(&ctx->event_push, &ctx->mutex_push);
}
DBG("got cmdbuf=%p\n", cmdbuf);
pipe_mutex_unlock(ctx->mutex_push);
cmdbuf->offset = 0;
ctx->cur_instr = 0;
}
/* Gets a pointer to the next memory slice.
* Does not block.
* Returns NULL on empty cmdbuf. */
void *
nine_queue_get(struct nine_queue_pool* ctx)
{
struct nine_cmdbuf *cmdbuf = &ctx->pool[ctx->tail];
unsigned offset;
/* At this pointer there's always a cmdbuf. */
if (ctx->cur_instr == cmdbuf->num_instr) {
/* signal waiting producer */
pipe_mutex_lock(ctx->mutex_pop);
DBG("freeing cmdbuf=%p\n", cmdbuf);
cmdbuf->full = 0;
cnd_signal(&ctx->event_pop);
pipe_mutex_unlock(ctx->mutex_pop);
ctx->tail = (ctx->tail + 1) & NINE_CMD_BUFS_MASK;
return NULL;
}
/* At this pointer there's always a cmdbuf with instruction to process. */
offset = cmdbuf->offset;
cmdbuf->offset += cmdbuf->instr_size[ctx->cur_instr];
ctx->cur_instr ++;
return cmdbuf->mem_pool + offset;
}
/* Producer functions: */
/* Flushes the queue.
* Moves the current cmdbuf to worker thread.
* Blocks until next cmdbuf is free. */
void
nine_queue_flush(struct nine_queue_pool* ctx)
{
struct nine_cmdbuf *cmdbuf = &ctx->pool[ctx->head];
DBG("flushing cmdbuf=%p instr=%d size=%d\n",
cmdbuf, cmdbuf->num_instr, cmdbuf->offset);
/* Nothing to flush */
if (!cmdbuf->num_instr)
return;
/* signal waiting worker */
pipe_mutex_lock(ctx->mutex_push);
cmdbuf->full = 1;
cnd_signal(&ctx->event_push);
pipe_mutex_unlock(ctx->mutex_push);
ctx->head = (ctx->head + 1) & NINE_CMD_BUFS_MASK;
cmdbuf = &ctx->pool[ctx->head];
/* wait for queue empty */
pipe_mutex_lock(ctx->mutex_pop);
while (cmdbuf->full)
{
DBG("waiting for empty cmdbuf\n");
cnd_wait(&ctx->event_pop, &ctx->mutex_pop);
}
DBG("got empty cmdbuf=%p\n", cmdbuf);
pipe_mutex_unlock(ctx->mutex_pop);
cmdbuf->offset = 0;
cmdbuf->num_instr = 0;
}
/* Gets a a pointer to slice of memory with size @space.
* Does block if queue is full.
* Returns NULL on @space > NINE_QUEUE_SIZE. */
void *
nine_queue_alloc(struct nine_queue_pool* ctx, unsigned space)
{
unsigned offset;
struct nine_cmdbuf *cmdbuf = &ctx->pool[ctx->head];
if (space > NINE_QUEUE_SIZE)
return NULL;
/* at this pointer there's always a free queue available */
if ((cmdbuf->offset + space > NINE_QUEUE_SIZE) ||
(cmdbuf->num_instr == NINE_CMD_BUF_INSTR)) {
nine_queue_flush(ctx);
cmdbuf = &ctx->pool[ctx->head];
}
DBG("cmdbuf=%p space=%d\n", cmdbuf, space);
/* at this pointer there's always a free queue with sufficient space available */
offset = cmdbuf->offset;
cmdbuf->offset += space;
cmdbuf->instr_size[cmdbuf->num_instr] = space;
cmdbuf->num_instr ++;
return cmdbuf->mem_pool + offset;
}
/* Returns the current queue flush state.
* TRUE nothing flushed
* FALSE one ore more instructions queued flushed. */
bool
nine_queue_no_flushed_work(struct nine_queue_pool* ctx)
{
return (ctx->tail == ctx->head);
}
/* Returns the current queue empty state.
* TRUE no instructions queued.
* FALSE one ore more instructions queued. */
bool
nine_queue_isempty(struct nine_queue_pool* ctx)
{
struct nine_cmdbuf *cmdbuf = &ctx->pool[ctx->head];
return (ctx->tail == ctx->head) && !cmdbuf->num_instr;
}
struct nine_queue_pool*
nine_queue_create(void)
{
unsigned i;
struct nine_queue_pool *ctx;
ctx = CALLOC_STRUCT(nine_queue_pool);
if (!ctx)
goto failed;
for (i = 0; i < NINE_CMD_BUFS; i++) {
ctx->pool[i].mem_pool = MALLOC(NINE_QUEUE_SIZE);
if (!ctx->pool[i].mem_pool)
goto failed;
}
cnd_init(&ctx->event_pop);
(void) mtx_init(&ctx->mutex_pop, mtx_plain);
cnd_init(&ctx->event_push);
(void) mtx_init(&ctx->mutex_push, mtx_plain);
/* Block until first cmdbuf has been flushed. */
ctx->worker_wait = TRUE;
return ctx;
failed:
if (ctx) {
for (i = 0; i < NINE_CMD_BUFS; i++) {
if (ctx->pool[i].mem_pool)
FREE(ctx->pool[i].mem_pool);
}
FREE(ctx);
}
return NULL;
}
void
nine_queue_delete(struct nine_queue_pool *ctx)
{
unsigned i;
mtx_destroy(&ctx->mutex_pop);
mtx_destroy(&ctx->mutex_push);
for (i = 0; i < NINE_CMD_BUFS; i++)
FREE(ctx->pool[i].mem_pool);
FREE(ctx);
}
|