blob: 3f43a19e018988f4187154c5074ade6aa9f71984 (
plain)
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
|
#include "pipe/p_thread.h"
#include "pipe/p_defines.h"
#include "util/u_ringbuffer.h"
#include "util/u_math.h"
#include "util/u_memory.h"
/* Generic ringbuffer:
*/
struct util_ringbuffer
{
struct util_packet *buf;
unsigned mask;
/* Can this be done with atomic variables??
*/
unsigned head;
unsigned tail;
pipe_condvar change;
pipe_mutex mutex;
};
struct util_ringbuffer *util_ringbuffer_create( unsigned dwords )
{
struct util_ringbuffer *ring = CALLOC_STRUCT(util_ringbuffer);
if (ring == NULL)
return NULL;
assert(util_is_power_of_two(dwords));
ring->buf = MALLOC( dwords * sizeof(unsigned) );
if (ring->buf == NULL)
goto fail;
ring->mask = dwords - 1;
pipe_condvar_init(ring->change);
pipe_mutex_init(ring->mutex);
return ring;
fail:
FREE(ring->buf);
FREE(ring);
return NULL;
}
void util_ringbuffer_destroy( struct util_ringbuffer *ring )
{
pipe_condvar_destroy(ring->change);
pipe_mutex_destroy(ring->mutex);
FREE(ring->buf);
FREE(ring);
}
static INLINE unsigned util_ringbuffer_space( const struct util_ringbuffer *ring )
{
return (ring->tail - (ring->head + 1)) & ring->mask;
}
void util_ringbuffer_enqueue( struct util_ringbuffer *ring,
const struct util_packet *packet )
{
unsigned i;
/* XXX: over-reliance on mutexes, etc:
*/
pipe_mutex_lock(ring->mutex);
/* Wait for free space:
*/
while (util_ringbuffer_space(ring) < packet->dwords)
pipe_condvar_wait(ring->change, ring->mutex);
/* Copy data to ring:
*/
for (i = 0; i < packet->dwords; i++) {
/* Copy all dwords of the packet. Note we're abusing the
* typesystem a little - we're being passed a pointer to
* something, but probably not an array of packet structs:
*/
ring->buf[ring->head] = packet[i];
ring->head++;
ring->head &= ring->mask;
}
/* Signal change:
*/
pipe_condvar_signal(ring->change);
pipe_mutex_unlock(ring->mutex);
}
enum pipe_error util_ringbuffer_dequeue( struct util_ringbuffer *ring,
struct util_packet *packet,
unsigned max_dwords,
boolean wait )
{
const struct util_packet *ring_packet;
unsigned i;
int ret = PIPE_OK;
/* XXX: over-reliance on mutexes, etc:
*/
pipe_mutex_lock(ring->mutex);
/* Wait for free space:
*/
if (wait) {
while (util_ringbuffer_space(ring) == 0)
pipe_condvar_wait(ring->change, ring->mutex);
}
else {
if (util_ringbuffer_space(ring) == 0) {
ret = PIPE_ERROR_OUT_OF_MEMORY;
goto out;
}
}
ring_packet = &ring->buf[ring->tail];
/* Both of these are considered bugs. Raise an assert on debug builds.
*/
if (ring_packet->dwords > ring->mask + 1 - util_ringbuffer_space(ring) ||
ring_packet->dwords > max_dwords) {
assert(0);
ret = PIPE_ERROR_BAD_INPUT;
goto out;
}
/* Copy data from ring:
*/
for (i = 0; i < ring_packet->dwords; i++) {
packet[i] = ring->buf[ring->tail];
ring->tail++;
ring->tail &= ring->mask;
}
out:
/* Signal change:
*/
pipe_condvar_signal(ring->change);
pipe_mutex_unlock(ring->mutex);
return ret;
}
|