2 * Copyright (c) 2007-2009 Kip Macy <kmacy@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #ifndef _SYS_BUF_RING_H_
31 #define _SYS_BUF_RING_H_
33 #include <machine/cpu.h>
35 #if defined(INVARIANTS) && !defined(DEBUG_BUFRING)
36 #define DEBUG_BUFRING 1
41 #include <sys/mutex.h>
45 volatile uint32_t br_prod_head;
46 volatile uint32_t br_prod_tail;
50 volatile uint32_t br_cons_head __aligned(CACHE_LINE_SIZE);
51 volatile uint32_t br_cons_tail;
57 void *br_ring[0] __aligned(CACHE_LINE_SIZE);
61 * multi-producer safe lock-free ring buffer enqueue
65 buf_ring_enqueue(struct buf_ring *br, void *buf)
67 uint32_t prod_head, prod_next;
71 for (i = br->br_cons_head; i != br->br_prod_head;
72 i = ((i + 1) & br->br_cons_mask))
73 if(br->br_ring[i] == buf)
74 panic("buf=%p already enqueue at %d prod=%d cons=%d",
75 buf, i, br->br_prod_tail, br->br_cons_tail);
79 prod_head = br->br_prod_head;
80 cons_tail = br->br_cons_tail;
82 prod_next = (prod_head + 1) & br->br_prod_mask;
84 if (prod_next == cons_tail) {
89 } while (!atomic_cmpset_int(&br->br_prod_head, prod_head, prod_next));
91 if (br->br_ring[prod_head] != NULL)
92 panic("dangling value in enqueue");
94 br->br_ring[prod_head] = buf;
97 * The full memory barrier also avoids that br_prod_tail store
98 * is reordered before the br_ring[prod_head] is full setup.
103 * If there are other enqueues in progress
104 * that preceeded us, we need to wait for them
107 while (br->br_prod_tail != prod_head)
109 br->br_prod_tail = prod_next;
115 * multi-consumer safe dequeue
118 static __inline void *
119 buf_ring_dequeue_mc(struct buf_ring *br)
121 uint32_t cons_head, cons_next;
128 cons_head = br->br_cons_head;
129 prod_tail = br->br_prod_tail;
131 cons_next = (cons_head + 1) & br->br_cons_mask;
133 if (cons_head == prod_tail) {
138 success = atomic_cmpset_int(&br->br_cons_head, cons_head,
140 } while (success == 0);
142 buf = br->br_ring[cons_head];
144 br->br_ring[cons_head] = NULL;
148 * The full memory barrier also avoids that br_ring[cons_read]
149 * load is reordered after br_cons_tail is set.
154 * If there are other dequeues in progress
155 * that preceeded us, we need to wait for them
158 while (br->br_cons_tail != cons_head)
161 br->br_cons_tail = cons_next;
168 * single-consumer dequeue
169 * use where dequeue is protected by a lock
170 * e.g. a network driver's tx queue lock
172 static __inline void *
173 buf_ring_dequeue_sc(struct buf_ring *br)
175 uint32_t cons_head, cons_next, cons_next_next;
179 cons_head = br->br_cons_head;
180 prod_tail = br->br_prod_tail;
182 cons_next = (cons_head + 1) & br->br_cons_mask;
183 cons_next_next = (cons_head + 2) & br->br_cons_mask;
185 if (cons_head == prod_tail)
188 #ifdef PREFETCH_DEFINED
189 if (cons_next != prod_tail) {
190 prefetch(br->br_ring[cons_next]);
191 if (cons_next_next != prod_tail)
192 prefetch(br->br_ring[cons_next_next]);
195 br->br_cons_head = cons_next;
196 buf = br->br_ring[cons_head];
199 br->br_ring[cons_head] = NULL;
200 if (!mtx_owned(br->br_lock))
201 panic("lock not held on single consumer dequeue");
202 if (br->br_cons_tail != cons_head)
203 panic("inconsistent list cons_tail=%d cons_head=%d",
204 br->br_cons_tail, cons_head);
206 br->br_cons_tail = cons_next;
211 * return a pointer to the first entry in the ring
212 * without modifying it, or NULL if the ring is empty
213 * race-prone if not protected by a lock
215 static __inline void *
216 buf_ring_peek(struct buf_ring *br)
220 if ((br->br_lock != NULL) && !mtx_owned(br->br_lock))
221 panic("lock not held on single consumer dequeue");
224 * I believe it is safe to not have a memory barrier
225 * here because we control cons and tail is worst case
226 * a lagging indicator so we worst case we might
227 * return NULL immediately after a buffer has been enqueued
229 if (br->br_cons_head == br->br_prod_tail)
232 return (br->br_ring[br->br_cons_head]);
236 buf_ring_full(struct buf_ring *br)
239 return (((br->br_prod_head + 1) & br->br_prod_mask) == br->br_cons_tail);
243 buf_ring_empty(struct buf_ring *br)
246 return (br->br_cons_head == br->br_prod_tail);
250 buf_ring_count(struct buf_ring *br)
253 return ((br->br_prod_size + br->br_prod_tail - br->br_cons_tail)
257 struct buf_ring *buf_ring_alloc(int count, struct malloc_type *type, int flags,
259 void buf_ring_free(struct buf_ring *br, struct malloc_type *type);