1 /**************************************************************************
3 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. The name of Kip Macy nor the names of other
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 ***************************************************************************/
32 #ifndef _SYS_BUF_RING_H_
33 #define _SYS_BUF_RING_H_
35 #include <machine/cpu.h>
37 #if defined(INVARIANTS) && !defined(DEBUG_BUFRING)
38 #define DEBUG_BUFRING 1
43 #include <sys/mutex.h>
47 volatile uint32_t br_prod_head;
48 volatile uint32_t br_prod_tail;
52 uint64_t br_prod_bufs;
53 uint64_t br_prod_bytes;
55 * Pad out to next L2 cache line
59 volatile uint32_t br_cons_head;
60 volatile uint32_t br_cons_tail;
65 * Pad out to next L2 cache line
75 * multi-producer safe lock-free ring buffer enqueue
79 buf_ring_enqueue_bytes(struct buf_ring *br, void *buf, int nbytes)
81 uint32_t prod_head, prod_next;
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);
94 prod_head = br->br_prod_head;
95 cons_tail = br->br_cons_tail;
97 prod_next = (prod_head + 1) & br->br_prod_mask;
99 if (prod_next == cons_tail) {
104 success = atomic_cmpset_int(&br->br_prod_head, prod_head,
106 } while (success == 0);
108 if (br->br_ring[prod_head] != NULL)
109 panic("dangling value in enqueue");
111 br->br_ring[prod_head] = buf;
115 * If there are other enqueues in progress
116 * that preceeded us, we need to wait for them
119 while (br->br_prod_tail != prod_head)
122 br->br_prod_bytes += nbytes;
123 br->br_prod_tail = prod_next;
129 buf_ring_enqueue(struct buf_ring *br, void *buf)
132 return (buf_ring_enqueue_bytes(br, buf, 0));
136 * multi-consumer safe dequeue
139 static __inline void *
140 buf_ring_dequeue_mc(struct buf_ring *br)
142 uint32_t cons_head, cons_next;
149 cons_head = br->br_cons_head;
150 prod_tail = br->br_prod_tail;
152 cons_next = (cons_head + 1) & br->br_cons_mask;
154 if (cons_head == prod_tail) {
159 success = atomic_cmpset_int(&br->br_cons_head, cons_head,
161 } while (success == 0);
163 buf = br->br_ring[cons_head];
165 br->br_ring[cons_head] = NULL;
170 * If there are other dequeues in progress
171 * that preceeded us, we need to wait for them
174 while (br->br_cons_tail != cons_head)
177 br->br_cons_tail = cons_next;
184 * single-consumer dequeue
185 * use where dequeue is protected by a lock
186 * e.g. a network driver's tx queue lock
188 static __inline void *
189 buf_ring_dequeue_sc(struct buf_ring *br)
191 uint32_t cons_head, cons_next, cons_next_next;
195 cons_head = br->br_cons_head;
196 prod_tail = br->br_prod_tail;
198 cons_next = (cons_head + 1) & br->br_cons_mask;
199 cons_next_next = (cons_head + 2) & br->br_cons_mask;
201 if (cons_head == prod_tail)
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]);
211 br->br_cons_head = cons_next;
212 buf = br->br_ring[cons_head];
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);
222 br->br_cons_tail = cons_next;
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
231 static __inline void *
232 buf_ring_peek(struct buf_ring *br)
236 if ((br->br_lock != NULL) && !mtx_owned(br->br_lock))
237 panic("lock not held on single consumer dequeue");
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
245 if (br->br_cons_head == br->br_prod_tail)
248 return (br->br_ring[br->br_cons_head]);
252 buf_ring_full(struct buf_ring *br)
255 return (((br->br_prod_head + 1) & br->br_prod_mask) == br->br_cons_tail);
259 buf_ring_empty(struct buf_ring *br)
262 return (br->br_cons_head == br->br_prod_tail);
266 buf_ring_count(struct buf_ring *br)
269 return ((br->br_prod_size + br->br_prod_tail - br->br_cons_tail)
273 struct buf_ring *buf_ring_alloc(int count, struct malloc_type *type, int flags,
275 void buf_ring_free(struct buf_ring *br, struct malloc_type *type);