From 82a5bc70e5f85d7d40e04e6a7dc567b086025421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 6 Jan 2010 23:45:20 +0100 Subject: ring buffer read : set read_from=-1 if read_from==write_to --- lf_ring_buffer.c | 11 +++++++++-- lf_ring_buffer_test.c | 5 +++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lf_ring_buffer.c b/lf_ring_buffer.c index 75b2f17..ad866e0 100644 --- a/lf_ring_buffer.c +++ b/lf_ring_buffer.c @@ -149,8 +149,15 @@ int lf_ring_buffer_read( lf_ring_buffer_t *r, void *data, int flags ) { if (next==r->n_buf) next=0; /* will do bad things if data dst buffer is too small !! */ memcpy( data, r->buffer[idx].data, LFRB_DATA_SIZE ); - _LOG_( "write: CAS %d %d %d\n", r->read_from, idx, next ); - if( CompareAndSwapInt( &r->read_from, idx, next ) ) break; + _LOG_( "read: CAS %d %d %d\n", r->read_from, idx, next ); + if( CompareAndSwapInt( &r->read_from, idx, next ) ) { + if(r->read_from==r->write_to) { + /* the buffer is actually empty but writers will see it as full */ + _LOG_( "read: empty CAS %d %d %d\n", r->read_from, next, -1 ); + CompareAndSwapInt( &r->read_from, next, -1 ); + } + break; + } } else { _LOG_("read: not available\n"); if(IS_NOT_BLOCKING(flags)) return -1; diff --git a/lf_ring_buffer_test.c b/lf_ring_buffer_test.c index 348d202..e9e199a 100644 --- a/lf_ring_buffer_test.c +++ b/lf_ring_buffer_test.c @@ -6,8 +6,7 @@ #include "lf_ring_buffer.h" -//#define BUFFER_LEN 5000000 -#define BUFFER_LEN 500 +#define BUFFER_LEN 5000000 static rb_data_t data[BUFFER_LEN][RB_DATA_LEN]; @@ -25,12 +24,14 @@ static void report( int n, uint64_t dt ) { fprintf(stdout,"%d operations in %d [ms] => %d [us] => %d [ns]\t >>> %d [ns/op]\n", n, (int)(dt/1000000), (int)(dt/1000), (int)dt, (int)(dt/n) ); } +/* static void feed_data( int n){ int i; for(i=0; i