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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/**************************************************************************
*
* Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Tx., USA
* All Rights Reserved.
* Copyright 2009 VMware, Inc., Palo Alto, CA., USA
* 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 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 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.
*
**************************************************************************/
/*
* Authors: Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "wsbm_fencemgr.h"
#include "wsbm_pool.h"
#include "wsbm_manager.h"
#include <string.h>
#include <unistd.h>
struct _WsbmFenceClass
{
struct _WsbmListHead head;
struct _WsbmMutex mutex;
struct _WsbmMutex cmd_mutex;
};
/*
* Note: The struct _WsbmFenceMgr::Mutex should never be held
* during sleeps, since that may block fast concurrent access to
* fence data.
*/
struct _WsbmFenceMgr
{
/*
* Constant members. Need no mutex protection.
*/
struct _WsbmFenceMgrCreateInfo info;
void *private;
/*
* Atomic members. No mutex protection.
*/
struct _WsbmAtomic count;
/*
* These members are protected by this->mutex
*/
struct _WsbmFenceClass *classes;
uint32_t num_classes;
};
struct _WsbmFenceObject
{
/*
* These members are constant and need no mutex protection.
* Note that @private may point to a structure with its own
* mutex protection, that we don't care about.
*/
struct _WsbmFenceMgr *mgr;
uint32_t fence_class;
uint32_t fence_type;
void *private;
/*
* Atomic members. No mutex protection. note that
* @signaled types is updated using a compare-and-swap
* scheme to guarantee atomicity.
*/
struct _WsbmAtomic refCount;
struct _WsbmAtomic signaled_types;
/*
* These members are protected by mgr->mutex.
*/
struct _WsbmListHead head;
};
uint32_t
wsbmFenceType(struct _WsbmFenceObject *fence)
{
return fence->fence_type;
}
struct _WsbmFenceMgr *
wsbmFenceMgrCreate(const struct _WsbmFenceMgrCreateInfo *info)
{
struct _WsbmFenceMgr *tmp;
uint32_t i, j;
int ret;
tmp = calloc(1, sizeof(*tmp));
if (!tmp)
return NULL;
tmp->info = *info;
tmp->classes = calloc(tmp->info.num_classes, sizeof(*tmp->classes));
if (!tmp->classes)
goto out_err;
for (i = 0; i < tmp->info.num_classes; ++i) {
struct _WsbmFenceClass *fc = &tmp->classes[i];
WSBMINITLISTHEAD(&fc->head);
ret = WSBM_MUTEX_INIT(&fc->mutex);
if (ret)
goto out_err1;
ret = WSBM_MUTEX_INIT(&fc->cmd_mutex);
if (ret) {
WSBM_MUTEX_FREE(&fc->mutex);
goto out_err1;
}
}
wsbmAtomicSet(&tmp->count, 0);
tmp->private = info->private;
return tmp;
out_err1:
for (j = 0; j < i; ++j) {
WSBM_MUTEX_FREE(&tmp->classes[j].mutex);
WSBM_MUTEX_FREE(&tmp->classes[j].cmd_mutex);
}
free(tmp->classes);
out_err:
if (tmp)
free(tmp);
return NULL;
}
void
wsbmFenceUnreference(struct _WsbmFenceObject **pFence)
{
struct _WsbmFenceObject *fence = *pFence;
struct _WsbmFenceMgr *mgr;
*pFence = NULL;
if (fence == NULL)
return;
mgr = fence->mgr;
if (wsbmAtomicDecZero(&fence->refCount)) {
struct _WsbmFenceClass *fc = &mgr->classes[fence->fence_class];
WSBM_MUTEX_LOCK(&fc->mutex);
WSBMLISTDELINIT(&fence->head);
WSBM_MUTEX_UNLOCK(&fc->mutex);
if (fence->private)
mgr->info.unreference(mgr, &fence->private);
fence->mgr = NULL;
wsbmAtomicDecZero(&mgr->count);
free(fence);
}
}
static void
wsbmSignalPreviousFences(struct _WsbmFenceMgr *mgr,
struct _WsbmListHead *list,
uint32_t fence_class, uint32_t signaled_types)
{
struct _WsbmFenceClass *fc = &mgr->classes[fence_class];
struct _WsbmFenceObject *entry;
struct _WsbmListHead *prev;
uint32_t old_signaled_types;
uint32_t ret_st;
WSBM_MUTEX_LOCK(&fc->mutex);
while (list != &fc->head && list->next != list) {
entry = WSBMLISTENTRY(list, struct _WsbmFenceObject, head);
prev = list->prev;
do {
old_signaled_types = wsbmAtomicRead(&entry->signaled_types);
signaled_types =
old_signaled_types | (signaled_types & entry->fence_type);
if (signaled_types == old_signaled_types)
break;
ret_st =
wsbmAtomicCmpXchg(&entry->signaled_types, old_signaled_types,
signaled_types);
} while (ret_st != old_signaled_types);
if (signaled_types == entry->fence_type)
WSBMLISTDELINIT(list);
list = prev;
}
WSBM_MUTEX_UNLOCK(&fc->mutex);
}
int
wsbmFenceFinish(struct _WsbmFenceObject *fence, uint32_t fence_type,
int lazy_hint)
{
struct _WsbmFenceMgr *mgr = fence->mgr;
int ret = 0;
if ((wsbmAtomicRead(&fence->signaled_types) & fence_type) == fence_type)
goto out;
ret = mgr->info.finish(mgr, fence->private, fence_type, lazy_hint);
if (ret)
goto out;
wsbmSignalPreviousFences(mgr, &fence->head, fence->fence_class,
fence_type);
out:
return ret;
}
uint32_t
wsbmFenceSignaledTypeCached(struct _WsbmFenceObject * fence)
{
return wsbmAtomicRead(&fence->signaled_types);
}
int
wsbmFenceSignaledType(struct _WsbmFenceObject *fence, uint32_t flush_type,
uint32_t * signaled)
{
int ret = 0;
struct _WsbmFenceMgr *mgr;
uint32_t signaled_types;
uint32_t old_signaled_types;
uint32_t ret_st;
mgr = fence->mgr;
*signaled = wsbmAtomicRead(&fence->signaled_types);
if ((*signaled & flush_type) == flush_type)
goto out0;
ret = mgr->info.signaled(mgr, fence->private, flush_type, signaled);
if (ret) {
*signaled = wsbmAtomicRead(&fence->signaled_types);
goto out0;
}
do {
old_signaled_types = wsbmAtomicRead(&fence->signaled_types);
signaled_types = old_signaled_types | *signaled;
if (signaled_types == old_signaled_types)
break;
ret_st = wsbmAtomicCmpXchg(&fence->signaled_types, old_signaled_types,
signaled_types);
if (old_signaled_types == ret_st)
wsbmSignalPreviousFences(mgr, &fence->head, fence->fence_class,
*signaled);
} while (old_signaled_types != ret_st);
return 0;
out0:
return ret;
}
struct _WsbmFenceObject *
wsbmFenceReference(struct _WsbmFenceObject *fence)
{
if (fence == NULL)
return NULL;
wsbmAtomicInc(&fence->refCount);
return fence;
}
struct _WsbmFenceObject *
wsbmFenceCreateSig(struct _WsbmFenceMgr *mgr, uint32_t fence_class,
uint32_t fence_type, uint32_t signaled_types,
void *private, size_t private_size)
{
struct _WsbmFenceClass *fc = &mgr->classes[fence_class];
struct _WsbmFenceObject *fence;
size_t fence_size = sizeof(*fence);
if (private_size)
fence_size = ((fence_size + 15) & ~15);
fence = calloc(1, fence_size + private_size);
if (!fence)
goto out_err;
wsbmAtomicSet(&fence->refCount, 1);
fence->mgr = mgr;
fence->fence_class = fence_class;
fence->fence_type = fence_type;
wsbmAtomicSet(&fence->signaled_types, signaled_types);
fence->private = private;
if (private_size) {
fence->private = (void *)(((uint8_t *) fence) + fence_size);
memcpy(fence->private, private, private_size);
}
WSBM_MUTEX_LOCK(&fc->mutex);
WSBMLISTADDTAIL(&fence->head, &fc->head);
WSBM_MUTEX_UNLOCK(&fc->mutex);
wsbmAtomicInc(&mgr->count);
return fence;
out_err:
{
int ret = mgr->info.finish(mgr, private, fence_type, 0);
if (ret)
usleep(10000000);
}
if (fence)
free(fence);
mgr->info.unreference(mgr, &private);
return NULL;
}
struct _WsbmFenceObject *
wsbmFenceCreate(struct _WsbmFenceMgr *mgr, uint32_t fence_class,
uint32_t fence_type, void *private, size_t private_size)
{
return wsbmFenceCreateSig(mgr, fence_class, fence_type, 0, private,
private_size);
}
void
wsbmFenceCmdLock(struct _WsbmFenceMgr *mgr, uint32_t fence_class)
{
WSBM_MUTEX_LOCK(&mgr->classes[fence_class].cmd_mutex);
}
void
wsbmFenceCmdUnlock(struct _WsbmFenceMgr *mgr, uint32_t fence_class)
{
WSBM_MUTEX_UNLOCK(&mgr->classes[fence_class].cmd_mutex);
}
void
wsbmFenceMgrTakedown(struct _WsbmFenceMgr *mgr)
{
int i;
if (!mgr)
return;
for (i = 0; i < mgr->info.num_classes; ++i) {
WSBM_MUTEX_FREE(&mgr->classes[i].mutex);
WSBM_MUTEX_FREE(&mgr->classes[i].cmd_mutex);
}
free(mgr->classes);
free(mgr);
}
extern void *wsbmFenceMgrPrivate(struct _WsbmFenceMgr *mgr)
{
if (!mgr)
return NULL;
return mgr->private;
}
|