[linux-audio-dev] Lock Free Ringbuffer in C ?

David Olofson david at olofson.net
Sat Apr 5 13:47:00 UTC 2003


On Friday 04 April 2003 17.15, torbenh at gmx.de wrote:
> On Fri, Apr 04, 2003 at 12:47:28PM +0200, David Olofson wrote:
> > On Thursday 03 April 2003 15.36, torbenh at gmx.de wrote:
> > > i remember i have read a statement about a lock free ringbuffer
> > > implemented in C somewhere.
> > >
> > > Can anybody remeber it ?
> >
> > Here's one in C; the 'sfifo':
> >
> > 	http://olofson.net/mixed.html
> >
> >
> > I've been using it in various environments for a few years, but
> > that doesn't guarantee anything. ;-)
>
> well... it should for such small code :)

You might think so, but I'm not NASA. ;-)

As an example, the crash bug when specifying non-power-of-two buffer 
size had been there for years before I found it, simply because it 
took that long before anyone tried to ask for something like 4000 
bytes.

Testing does not guarantee anything beyond the specific conditions you 
test, under the exact conditions of your tests.


> but how do i handle this sensibly ?
>
>
> 	res = sfifo_write(sf, &buf, j);
> 	if(res != j)
> 	{
> 		printf("Write failed!\n");
>
> // eeek... no i dont want that...
> 		sleep(1);
> 	} else if(res)
> 		printf("Wrote %d\n", res);

write() can only fail if you forgot to initialize the fifo before 
using it, or possibly if someone stomped the fifo struct. In the case 
of the latter, you have more serious things to worry about! :-)

Feel free to ignore the return value from write() - unless it's valid 
for your writer to mess with closed fifos. (I wouldn't recommend 
doing that with statically allocated fifo structs, since you'd have 
to make sure to initialize them anyway.)


//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -'
   --- http://olofson.net --- http://www.reologica.se ---




More information about the Linux-audio-dev mailing list