X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Frmr%2Fcommon%2Fsrc%2Fring_static.c;h=8e565fabb47de9009ab3275f8e1c62fc8c138e60;hb=f8623e7dc17184b3359a5de8a81a54bb963469c7;hp=37bb4dbcf3600a9fa864e151130c92bc654297ad;hpb=c1f84f8a4a4e2b90ad9ec18aba2b5365d3e51386;p=ric-plt%2Flib%2Frmr.git diff --git a/src/rmr/common/src/ring_static.c b/src/rmr/common/src/ring_static.c index 37bb4db..8e565fa 100644 --- a/src/rmr/common/src/ring_static.c +++ b/src/rmr/common/src/ring_static.c @@ -85,12 +85,12 @@ static void* uta_mk_ring( int size ) { } r->nelements = size; // because we always have an empty element when full - if( (r->data = (void **) malloc( sizeof( void** ) * (r->nelements + 1) )) == NULL ) { + if( (r->data = (void **) malloc( sizeof( void* ) * (r->nelements + 1) )) == NULL ) { free( r ); return NULL; } - memset( r->data, 0, sizeof( void** ) * r->nelements ); + memset( r->data, 0, sizeof( void* ) * r->nelements ); r->pfd = eventfd( 0, EFD_SEMAPHORE | EFD_NONBLOCK ); // in semaphore mode counter is maintained with each insert/extract return (void *) r; } @@ -148,7 +148,15 @@ static void uta_ring_free( void* vr ) { if( (r = (ring_t*) vr) == NULL ) { return; } - + if( r->data ){ + free( r->data ); + } + if( r->rgate ){ + free( r->rgate ); + } + if( r->wgate ){ + free( r->wgate ); + } free( r ); } @@ -166,6 +174,7 @@ static inline void* uta_ring_extract( void* vr ) { ring_t* r; uint16_t ti; // real index in data int64_t ctr; // pfd counter + void* data; if( !RING_FAST ) { // compiler should drop the conditional when always false if( (r = (ring_t*) vr) == NULL ) { @@ -182,6 +191,7 @@ static inline void* uta_ring_extract( void* vr ) { if( r->rgate != NULL ) { // if lock exists we must honour it pthread_mutex_lock( r->rgate ); if( r->tail == r->head ) { // ensure ring didn't go empty while waiting + pthread_mutex_unlock( r->rgate ); return NULL; } } @@ -199,10 +209,14 @@ future -- investigate if it's possible only to set/clear when empty or going to } */ + data = r->data[ti]; // secure data and clear before letting go of the lock + r->data[ti] = NULL; + if( r->rgate != NULL ) { // if locked above... pthread_mutex_unlock( r->rgate ); } - return r->data[ti]; + + return data; } /* @@ -232,6 +246,12 @@ static inline int uta_ring_insert( void* vr, void* new_data ) { return 0; } + r->data[r->head] = new_data; + r->head++; + if( r->head >= r->nelements ) { + r->head = 0; + } + write( r->pfd, &inc, sizeof( inc ) ); /* future -- investigate if it's possible only to set/clear when empty or going to empty @@ -239,12 +259,6 @@ future -- investigate if it's possible only to set/clear when empty or going to } */ - r->data[r->head] = new_data; - r->head++; - if( r->head >= r->nelements ) { - r->head = 0; - } - if( r->wgate != NULL ) { // if lock exists we must unlock before going pthread_mutex_unlock( r->wgate ); }