[LAD] a *simple* ring buffer, comments pls?

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Linux Audio Developers <linux-audio-dev@...>
Date: Thursday, July 7, 2011 - 12:10 pm

just wondered if any more-experienced-than-i devs might comment on
this. written in c for c (obviously). i realize it's not portable
outside of GNU GCC (regarding the GCC atomic builtin funcs
__sync_***). meant for a single reader thread and a single writer
thread. comments regarding thread safety very much welcome. thanks in
advance.

james.

#include "rng_buf.h" /* only prototypes the public functions and
typedefs the struct */

#include
#include
#include "debug.h"

struct _RingBuffer
{
size_t count;

void** buf;
void** bufend;

void** volatile w;
void** volatile r;
};

RngBuf* rng_buf_new(size_t count)
{
size_t sz = 1;
RngBuf* mb = malloc(sizeof(RngBuf));

if (!mb)
{
return 0;
}

for (sz = 1; sz < count; sz <<= 1)
;

mb->buf = calloc(sz, sizeof(void*));

if (!mb->buf)
{
free(mb);
return 0;
}

mb->count = sz;
mb->bufend = mb->buf + mb->count - 1;
mb->w = mb->buf;
mb->r = mb->buf;

return mb;
}

void rng_buf_free(RngBuf* mb)
{
free(mb->buf);
free(mb);
}

size_t rng_buf_write(RngBuf* mb, const void* data)
{
if (__sync_bool_compare_and_swap(mb->w, 0, data))
{
if (!(__sync_bool_compare_and_swap(&mb->w, mb->bufend, mb->buf)))
__sync_add_and_fetch(&mb->w, sizeof(void*));

return (size_t)1;
}

return (size_t)0;
}

void* rng_buf_read(RngBuf* mb)
{
void* data;

if ((data = __sync_fetch_and_and(mb->r, 0)))
{
if (!__sync_bool_compare_and_swap(&mb->r, mb->bufend, mb->buf))
__sync_add_and_fetch(&mb->r, sizeof(void*));

return data;
}

return NULL;
}

void rng_buf_reset(RngBuf* mb)
{ /* needs work */
mb->r = mb->w = mb->buf;
}
_______________________________________________
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[LAD] a *simple* ring buffer, comments pls?, James Morris, (Thu Jul 7, 12:10 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 12:51 am)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 11:24 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Fri Jul 8, 1:22 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 5:17 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Gabriel M. Beddingfield, (Fri Jul 8, 6:12 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Arnold Krille, (Fri Jul 8, 7:06 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 7:53 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Fri Jul 8, 8:41 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 9:01 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Arnold Krille, (Fri Jul 8, 8:59 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 9:11 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Fri Jul 8, 7:23 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Mon Jul 11, 6:59 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Mon Jul 11, 8:53 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Tue Jul 12, 5:41 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Kegel, (Fri Jul 8, 7:29 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Fri Jul 8, 8:49 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Sean Bolton, (Fri Jul 8, 9:57 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Fri Jul 8, 11:03 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 11:27 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Sat Jul 9, 1:25 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Sat Jul 9, 2:38 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Sun Jul 10, 3:05 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Sun Jul 10, 9:15 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Mon Jul 11, 12:03 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Mon Jul 11, 8:44 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Kegel, (Sun Jul 10, 10:34 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Sun Jul 10, 9:41 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Sean Bolton, (Mon Jul 11, 1:34 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Sean Bolton, (Mon Jul 11, 1:44 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Mon Jul 11, 7:19 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Mon Jul 11, 8:32 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Mon Jul 11, 8:50 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Tim E. Real, (Mon Jul 11, 9:38 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Robin Gareus, (Mon Jul 11, 10:27 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Arnold Krille, (Mon Jul 11, 10:12 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Robin Gareus, (Mon Jul 11, 10:28 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Mon Jul 11, 10:34 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Mills, (Mon Jul 11, 10:24 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Mon Jul 11, 10:06 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Mon Jul 11, 11:06 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Mon Jul 11, 10:17 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Mon Jul 11, 8:58 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 7:07 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Muresan, (Tue Jul 12, 12:44 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Kai Vehmanen, (Wed Jul 20, 9:25 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Emanuel Rumpf, (Sat Aug 20, 6:24 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Emanuel Rumpf, (Sat Aug 27, 7:21 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Gabriel Beddingfield, (Sat Aug 20, 7:16 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 7:46 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 8:20 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Arnold Krille, (Tue Jul 12, 11:31 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Tue Jul 12, 11:56 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Wed Jul 13, 12:27 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 9:07 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 9:26 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Paul Davis, (Tue Jul 12, 8:36 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 9:32 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Kegel, (Tue Jul 12, 10:23 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 11:50 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fred Gleason, (Wed Jul 13, 12:08 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Wed Jul 13, 12:25 am)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 9:37 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Tue Jul 12, 10:00 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Tue Jul 12, 9:16 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Emanuel Rumpf, (Tue Jul 12, 5:20 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Chris Cannam, (Mon Jul 11, 8:50 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, David Olofson, (Mon Jul 11, 8:47 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Dan Kegel, (Fri Jul 8, 9:52 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Fons Adriaensen, (Fri Jul 8, 6:39 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 12:21 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Olivier Guilyardi, (Fri Jul 8, 12:56 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, Gabriel M. Beddingfield, (Fri Jul 8, 1:04 am)
Re: [LAD] a *simple* ring buffer, comments pls?, David Robillard, (Fri Jul 8, 2:23 pm)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 8:45 am)
Re: [LAD] a *simple* ring buffer, comments pls?, James Morris, (Fri Jul 8, 8:40 am)