读书人

FreeBSD的buf_ring,该如何解决

发布时间: 2013-09-28 10:01:20 作者: rapoo

FreeBSD的buf_ring
公司需要用到一个不加锁的队列实现线程同步,说是BSD上有,辛辛苦苦找来的.h .c源码,看不太懂,希望高手指教,小弟不胜感激。
buf_ring.h
32#ifndef _SYS_BUF_RING_H_
33#define _SYS_BUF_RING_H_
34
35#include <machine/cpu.h>
36
37#if defined(INVARIANTS) && !defined(DEBUG_BUFRING)
38#define DEBUG_BUFRING 1
39#endif
40
41#ifdef DEBUG_BUFRING
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#endif
45
46struct buf_ring {
47 volatile uint32_t br_prod_head;
48 volatile uint32_t br_prod_tail;
49 int br_prod_size;
50 int br_prod_mask;
51 uint64_t br_drops;
52 uint64_t br_prod_bufs;
53 uint64_t br_prod_bytes;
54 /*
55 * Pad out to next L2 cache line
56 */
57 uint64_t _pad0[11];
58
59 volatile uint32_t br_cons_head;
60 volatile uint32_t br_cons_tail;
61 int br_cons_size;
62 int br_cons_mask;
63
64 /*
65 * Pad out to next L2 cache line
66 */
67 uint64_t _pad1[14];
68#ifdef DEBUG_BUFRING
69 struct mtx *br_lock;
70#endif
71 void *br_ring[0];
72};
7374/*
75 * multi-producer safe lock-free ring buffer enqueue
76 *
77 */
78static __inline int
79buf_ring_enqueue_bytes(struct buf_ring *br, void *buf, int nbytes)
80{
81 uint32_t prod_head, prod_next;
82 uint32_t cons_tail;
83 int success;
84#ifdef DEBUG_BUFRING
85 int i;
86 for (i = br->br_cons_head; i != br->br_prod_head;
87 i = ((i + 1) & br->br_cons_mask))
88 if(br->br_ring[i] == buf)
89 panic("buf=%p already enqueue at %d prod=%d cons=%d",
90 buf, i, br->br_prod_tail, br->br_cons_tail);
91#endif
92 critical_enter();
93 do {
94 prod_head = br->br_prod_head;
95 cons_tail = br->br_cons_tail;
96
97 prod_next = (prod_head + 1) & br->br_prod_mask;
98


99 if (prod_next == cons_tail) {
100 critical_exit();
101 return (ENOBUFS);
102 }
103
104 success = atomic_cmpset_int(&br->br_prod_head, prod_head,
105 prod_next);
106 } while (success == 0);
107#ifdef DEBUG_BUFRING
108 if (br->br_ring[prod_head] != NULL)
109 panic("dangling value in enqueue");
110#endif
111 br->br_ring[prod_head] = buf;
112 wmb();
113
114 /*
115 * If there are other enqueues in progress
116 * that preceeded us, we need to wait for them
117 * to complete
118 */
119 while (br->br_prod_tail != prod_head)
120 cpu_spinwait();
121 br->br_prod_bufs++;
122 br->br_prod_bytes += nbytes;
123 br->br_prod_tail = prod_next;
124 critical_exit();
125 return (0);
126}
127128static __inline int
129buf_ring_enqueue(struct buf_ring *br, void *buf)
130{
131
132 return (buf_ring_enqueue_bytes(br, buf, 0));
133}
134
135/*
136 * multi-consumer safe dequeue
137 *
138 */
139static __inline void *
140buf_ring_dequeue_mc(struct buf_ring *br)
141{
142 uint32_t cons_head, cons_next;
143 uint32_t prod_tail;
144 void *buf;
145 int success;
146 critical_enter();
148 do {
149 cons_head = br->br_cons_head;
150 prod_tail = br->br_prod_tail;
151
152 cons_next = (cons_head + 1) & br->br_cons_mask;
153
154 if (cons_head == prod_tail) {
155 critical_exit();
156 return (NULL);
157 }
158
159 success = atomic_cmpset_int(&br->br_cons_head, cons_head,
160 cons_next);
161 } while (success == 0);
162
163 buf = br->br_ring[cons_head];
164#ifdef DEBUG_BUFRING
165 br->br_ring[cons_head] = NULL;
166#endif
167 rmb();
169 /*
170 * If there are other dequeues in progress


171 * that preceeded us, we need to wait for them
172 * to complete
173 */
174 while (br->br_cons_tail != cons_head)
175 cpu_spinwait();
176
177 br->br_cons_tail = cons_next;
178 critical_exit();
179
180 return (buf);
181}
182
183/*
184 * single-consumer dequeue
185 * use where dequeue is protected by a lock
186 * e.g. a network driver's tx queue lock
187 */
188static __inline void *
189buf_ring_dequeue_sc(struct buf_ring *br)
190{
191 uint32_t cons_head, cons_next, cons_next_next;
192 uint32_t prod_tail;
193 void *buf;
194
195 cons_head = br->br_cons_head;
196 prod_tail = br->br_prod_tail;
197
198 cons_next = (cons_head + 1) & br->br_cons_mask;
199 cons_next_next = (cons_head + 2) & br->br_cons_mask;
200
201 if (cons_head == prod_tail)
202 return (NULL);
203
204#ifdef PREFETCH_DEFINED
205 if (cons_next != prod_tail) {
206 prefetch(br->br_ring[cons_next]);
207 if (cons_next_next != prod_tail)
208 prefetch(br->br_ring[cons_next_next]);
209 }
210#endif
211 br->br_cons_head = cons_next;
212 buf = br->br_ring[cons_head];
213
214#ifdef DEBUG_BUFRING
215 br->br_ring[cons_head] = NULL;
216 if (!mtx_owned(br->br_lock))
217 panic("lock not held on single consumer dequeue");
218 if (br->br_cons_tail != cons_head)
219 panic("inconsistent list cons_tail=%d cons_head=%d",
220 br->br_cons_tail, cons_head);
221#endif
222 br->br_cons_tail = cons_next;
223 return (buf);
224}
225
226/*
227 * return a pointer to the first entry in the ring
228 * without modifying it, or NULL if the ring is empty
229 * race-prone if not protected by a lock
230 */
231static __inline void *
232buf_ring_peek(struct buf_ring *br)
233{
234
235#ifdef DEBUG_BUFRING
236 if ((br->br_lock != NULL) && !mtx_owned(br->br_lock))
237 panic("lock not held on single consumer dequeue");
238#endif
239 /*
240 * I believe it is safe to not have a memory barrier
241 * here because we control cons and tail is worst case
242 * a lagging indicator so we worst case we might
243 * return NULL immediately after a buffer has been enqueued


244 */
245 if (br->br_cons_head == br->br_prod_tail)
246 return (NULL);
247
248 return (br->br_ring[br->br_cons_head]);
249}
250
251static __inline int
252buf_ring_full(struct buf_ring *br)
253{
254
255 return (((br->br_prod_head + 1) & br->br_prod_mask) == br->br_cons_tail);
256}
257
258static __inline int
259buf_ring_empty(struct buf_ring *br)
260{
261
262 return (br->br_cons_head == br->br_prod_tail);
263}
264
265static __inline int
266buf_ring_count(struct buf_ring *br)
267{
268
269 return ((br->br_prod_size + br->br_prod_tail - br->br_cons_tail)
270 & br->br_prod_mask);
271}
272
273struct buf_ring *buf_ring_alloc(int count, struct malloc_type *type, int flags,
274 struct mtx *);
275void buf_ring_free(struct buf_ring *br, struct malloc_type *type);
279#endif

buf_ring.c
struct buf_ring *
43buf_ring_alloc(int count, struct malloc_type *type, int flags, struct mtx *lock)
44{
45 struct buf_ring *br;
46
47 KASSERT(powerof2(count), ("buf ring must be size power of 2"));
48
49 br = malloc(sizeof(struct buf_ring) + count*sizeof(caddr_t),
50 type, flags|M_ZERO);
51 if (br == NULL)
52 return (NULL);
53#ifdef DEBUG_BUFRING
54 br->br_lock = lock;
55#endif
56 br->br_prod_size = br->br_cons_size = count;
57 br->br_prod_mask = br->br_cons_mask = count-1;
58 br->br_prod_head = br->br_cons_head = 0;
59 br->br_prod_tail = br->br_cons_tail = 0;
60
61 return (br);
62}
63
64void
65buf_ring_free(struct buf_ring *br, struct malloc_type *type)
66{
67 free(br, type);
68}
[解决办法]
类似circular buffer的结构,参考http://en.wikipedia.org/wiki/Circular_buffer
[解决办法]
楼主陷入了垂直扩展的陷阱,而不是水平扩展。

读书人网 >C语言

热点推荐