1*35f8bc5fSChristian Brueffer.\" Copyright (c) 2009 Bitgravity Inc 2*35f8bc5fSChristian Brueffer.\" Written by: Kip Macy <kmacy@FreeBSD.org> 3*35f8bc5fSChristian Brueffer.\" All rights reserved. 4*35f8bc5fSChristian Brueffer.\" 5*35f8bc5fSChristian Brueffer.\" Redistribution and use in source and binary forms, with or without 6*35f8bc5fSChristian Brueffer.\" modification, are permitted provided that the following conditions 7*35f8bc5fSChristian Brueffer.\" are met: 8*35f8bc5fSChristian Brueffer.\" 1. Redistributions of source code must retain the above copyright 9*35f8bc5fSChristian Brueffer.\" notice, this list of conditions and the following disclaimer. 10*35f8bc5fSChristian Brueffer.\" 2. Redistributions in binary form must reproduce the above copyright 11*35f8bc5fSChristian Brueffer.\" notice, this list of conditions and the following disclaimer in the 12*35f8bc5fSChristian Brueffer.\" documentation and/or other materials provided with the distribution. 13*35f8bc5fSChristian Brueffer.\" 14*35f8bc5fSChristian Brueffer.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*35f8bc5fSChristian Brueffer.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*35f8bc5fSChristian Brueffer.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*35f8bc5fSChristian Brueffer.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*35f8bc5fSChristian Brueffer.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*35f8bc5fSChristian Brueffer.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*35f8bc5fSChristian Brueffer.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*35f8bc5fSChristian Brueffer.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*35f8bc5fSChristian Brueffer.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*35f8bc5fSChristian Brueffer.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*35f8bc5fSChristian Brueffer.\" SUCH DAMAGE. 25*35f8bc5fSChristian Brueffer.\" 26*35f8bc5fSChristian Brueffer.\" $FreeBSD$ 27*35f8bc5fSChristian Brueffer.\" 28*35f8bc5fSChristian Brueffer.Dd January 30, 2012 29*35f8bc5fSChristian Brueffer.Dt BUF_RING 9 30*35f8bc5fSChristian Brueffer.Os 31*35f8bc5fSChristian Brueffer.Sh NAME 32*35f8bc5fSChristian Brueffer.Nm buf_ring , 33*35f8bc5fSChristian Brueffer.Nm buf_ring_alloc , 34*35f8bc5fSChristian Brueffer.Nm buf_ring_free , 35*35f8bc5fSChristian Brueffer.Nm buf_ring_enqueue , 36*35f8bc5fSChristian Brueffer.Nm buf_ring_enqueue_bytes , 37*35f8bc5fSChristian Brueffer.Nm buf_ring_dequeue_mc , 38*35f8bc5fSChristian Brueffer.Nm buf_ring_dequeue_sc , 39*35f8bc5fSChristian Brueffer.Nm buf_ring_count , 40*35f8bc5fSChristian Brueffer.Nm buf_ring_empty , 41*35f8bc5fSChristian Brueffer.Nm buf_ring_full , 42*35f8bc5fSChristian Brueffer.Nm buf_ring_peek , 43*35f8bc5fSChristian Brueffer.Nd multi-producer, {single, multi}-consumer lock-less ring buffer 44*35f8bc5fSChristian Brueffer.Sh SYNOPSIS 45*35f8bc5fSChristian Brueffer.In sys/param.h 46*35f8bc5fSChristian Brueffer.In sys/buf_ring.h 47*35f8bc5fSChristian Brueffer.Ft struct buf_ring * 48*35f8bc5fSChristian Brueffer.Fn buf_ring_alloc "int count" "struct malloc_type *type" "int flags" "struct mtx *sc_lock" 49*35f8bc5fSChristian Brueffer.Ft void 50*35f8bc5fSChristian Brueffer.Fn buf_ring_free "struct buf_ring *br" "struct malloc_type *type" 51*35f8bc5fSChristian Brueffer.Ft int 52*35f8bc5fSChristian Brueffer.Fn buf_ring_enqueue "struct buf_ring *br" "void *buf" 53*35f8bc5fSChristian Brueffer.Ft int 54*35f8bc5fSChristian Brueffer.Fn buf_ring_enqueue_bytes "struct buf_ring *br" "void *buf" "int bytes" 55*35f8bc5fSChristian Brueffer.Ft void * 56*35f8bc5fSChristian Brueffer.Fn buf_ring_dequeue_mc "struct buf_ring *br" 57*35f8bc5fSChristian Brueffer.Ft void * 58*35f8bc5fSChristian Brueffer.Fn buf_ring_dequeue_sc "struct buf_ring *br" 59*35f8bc5fSChristian Brueffer.Ft int 60*35f8bc5fSChristian Brueffer.Fn buf_ring_count "struct buf_ring *br" 61*35f8bc5fSChristian Brueffer.Ft int 62*35f8bc5fSChristian Brueffer.Fn buf_ring_empty "struct buf_ring *br" 63*35f8bc5fSChristian Brueffer.Ft int 64*35f8bc5fSChristian Brueffer.Fn buf_ring_full "struct buf_ring *br" 65*35f8bc5fSChristian Brueffer.Ft void * 66*35f8bc5fSChristian Brueffer.Fn buf_ring_peek "struct buf_ring *br" 67*35f8bc5fSChristian Brueffer.Sh DESCRIPTION 68*35f8bc5fSChristian BruefferThe 69*35f8bc5fSChristian Brueffer.Nm 70*35f8bc5fSChristian Bruefferfunctions provide a lock-less multi-producer and lock-less multi-consumer as 71*35f8bc5fSChristian Bruefferwell as single-consumer ring buffer. 72*35f8bc5fSChristian Brueffer.Pp 73*35f8bc5fSChristian BruefferThe 74*35f8bc5fSChristian Brueffer.Fn buf_ring_alloc 75*35f8bc5fSChristian Bruefferfunction is used to allocate a buf_ring ring buffer with 76*35f8bc5fSChristian Brueffer.Fa count 77*35f8bc5fSChristian Bruefferslots using malloc_type 78*35f8bc5fSChristian Brueffer.Fa type 79*35f8bc5fSChristian Bruefferand memory flags 80*35f8bc5fSChristian Brueffer.Fa flags . 81*35f8bc5fSChristian BruefferThe single consumer interface is protected by 82*35f8bc5fSChristian Brueffer.Fa sc_lock . 83*35f8bc5fSChristian Brueffer.Pp 84*35f8bc5fSChristian BruefferThe 85*35f8bc5fSChristian Brueffer.Fn buf_ring_free 86*35f8bc5fSChristian Bruefferfunction is used to free a buf_ring. 87*35f8bc5fSChristian BruefferThe user is responsible for freeing any enqueued items. 88*35f8bc5fSChristian Brueffer.Pp 89*35f8bc5fSChristian BruefferThe 90*35f8bc5fSChristian Brueffer.Fn buf_ring_enqueue 91*35f8bc5fSChristian Bruefferfunction is used to enqueue a buffer to a buf_ring. 92*35f8bc5fSChristian Brueffer.Pp 93*35f8bc5fSChristian BruefferThe 94*35f8bc5fSChristian Brueffer.Fn buf_ring_enqueue_bytes 95*35f8bc5fSChristian Bruefferfunction is used to enqueue a buffer to a buf_ring and increment the 96*35f8bc5fSChristian Brueffernumber of bytes enqueued by 97*35f8bc5fSChristian Brueffer.Fa bytes . 98*35f8bc5fSChristian Brueffer.Pp 99*35f8bc5fSChristian BruefferThe 100*35f8bc5fSChristian Brueffer.Fn buf_ring_dequeue_mc 101*35f8bc5fSChristian Bruefferfunction is a multi-consumer safe way of dequeueing elements from a buf_ring. 102*35f8bc5fSChristian Brueffer.Pp 103*35f8bc5fSChristian BruefferThe 104*35f8bc5fSChristian Brueffer.Fn buf_ring_dequeue_sc 105*35f8bc5fSChristian Bruefferfunction is a single-consumer interface to dequeue elements - requiring 106*35f8bc5fSChristian Bruefferthe user to serialize accesses with a lock. 107*35f8bc5fSChristian Brueffer.Pp 108*35f8bc5fSChristian BruefferThe 109*35f8bc5fSChristian Brueffer.Fn buf_ring_count 110*35f8bc5fSChristian Bruefferfunction returns the number of elements in a buf_ring. 111*35f8bc5fSChristian Brueffer.Pp 112*35f8bc5fSChristian BruefferThe 113*35f8bc5fSChristian Brueffer.Fn buf_ring_empty 114*35f8bc5fSChristian Bruefferfunction returns 115*35f8bc5fSChristian Brueffer.Dv TRUE 116*35f8bc5fSChristian Bruefferif the buf_ring is empty, 117*35f8bc5fSChristian Brueffer.Dv FALSE 118*35f8bc5fSChristian Bruefferotherwise. 119*35f8bc5fSChristian Brueffer.Pp 120*35f8bc5fSChristian BruefferThe 121*35f8bc5fSChristian Brueffer.Fn buf_ring_full 122*35f8bc5fSChristian Bruefferfunction returns 123*35f8bc5fSChristian Brueffer.Dv TRUE 124*35f8bc5fSChristian Bruefferif no more items can be enqueued, 125*35f8bc5fSChristian Brueffer.Dv FALSE 126*35f8bc5fSChristian Bruefferotherwise. 127*35f8bc5fSChristian Brueffer.Pp 128*35f8bc5fSChristian BruefferThe 129*35f8bc5fSChristian Brueffer.Fn buf_ring_peek 130*35f8bc5fSChristian Bruefferfunction returns a pointer to the last element in the buf_ring if the 131*35f8bc5fSChristian Bruefferbuf_ring is not empty, 132*35f8bc5fSChristian Brueffer.Dv NULL 133*35f8bc5fSChristian Bruefferotherwise. 134*35f8bc5fSChristian Brueffer.Sh RETURN VALUES 135*35f8bc5fSChristian BruefferThe 136*35f8bc5fSChristian Brueffer.Fn buf_ring_enqueue 137*35f8bc5fSChristian Bruefferand 138*35f8bc5fSChristian Brueffer.Fn buf_ring_enqueue_bytes 139*35f8bc5fSChristian Bruefferfunctions return 140*35f8bc5fSChristian Brueffer.Er ENOBUFS 141*35f8bc5fSChristian Bruefferif there are no available slots in the buf_ring. 142*35f8bc5fSChristian Brueffer.Sh HISTORY 143*35f8bc5fSChristian BruefferThese functions were introduced in 144*35f8bc5fSChristian Brueffer.Fx 8.0 . 145