X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Frmr%2Fcommon%2Fsrc%2Fring_static.c;h=37bb4dbcf3600a9fa864e151130c92bc654297ad;hb=05ce11d8c370a66c238b1db3fff1fde4790fa701;hp=93dfdb7e603c94a47f398fbc063ce980cdec888d;hpb=ec88d3c0563eeb6ae5f73427edb0b3c4d7acf299;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 93dfdb7..37bb4db 100644 --- a/src/rmr/common/src/ring_static.c +++ b/src/rmr/common/src/ring_static.c @@ -62,7 +62,10 @@ static int uta_ring_getpfd( void* vr ) { } /* - Make a new ring. + Make a new ring. The default is to NOT create a lock; if the user + wants read locking then uta_config_ring() can be used to setup the + mutex. (We use several rings internally and the assumption is that + there is no locking for these.) */ static void* uta_mk_ring( int size ) { ring_t* r; @@ -72,6 +75,8 @@ static void* uta_mk_ring( int size ) { return NULL; } + r->rgate = NULL; + r->wgate = NULL; r->head = r->tail = 0; max = (r->head - 1); @@ -90,6 +95,49 @@ static void* uta_mk_ring( int size ) { return (void *) r; } +/* + Allows for configuration of a ring after it has been allocated. + Options are RING_* options that allow for things like setting/clearing + read locking. Returns 0 for failure 1 on success. + + Options can be ORd together and all made effective at the same time, but + it will be impossible to determine a specific failure if invoked this + way. Control is returned on the first error, and no provision is made + to "undo" previously set options if an error occurs. +*/ +static int uta_ring_config( void* vr, int options ) { + ring_t* r; + + if( (r = (ring_t*) vr) == NULL ) { + errno = EINVAL; + return 0; + } + + if( options & RING_WLOCK ) { + if( r->wgate == NULL ) { // don't realloc + r->wgate = (pthread_mutex_t *) malloc( sizeof( *r->wgate ) ); + if( r->wgate == NULL ) { + return 0; + } + + pthread_mutex_init( r->wgate, NULL ); + } + } + + if( options & RING_RLOCK ) { + if( r->rgate == NULL ) { // don't realloc + r->rgate = (pthread_mutex_t *) malloc( sizeof( *r->rgate ) ); + if( r->rgate == NULL ) { + return 0; + } + + pthread_mutex_init( r->rgate, NULL ); + } + } + + return 1; +} + /* Ditch the ring. The caller is responsible for extracting any remaining pointers and freeing them as needed. @@ -108,6 +156,11 @@ static void uta_ring_free( void* vr ) { /* Pull the next data pointer from the ring; null if there isn't anything to be pulled. + + If the read lock exists for the ring, then this will BLOCK until + it gets the lock. There is always a chance that once the lock + is obtained that the ring is empty, so the caller MUST handle + a nil pointer as the return. */ static inline void* uta_ring_extract( void* vr ) { ring_t* r; @@ -122,10 +175,17 @@ static inline void* uta_ring_extract( void* vr ) { r = (ring_t*) vr; } - if( r->tail == r->head ) { // empty ring + if( r->tail == r->head ) { // empty ring we can bail out quickly return NULL; } + 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 + return NULL; + } + } + ti = r->tail; r->tail++; if( r->tail >= r->nelements ) { @@ -138,6 +198,10 @@ future -- investigate if it's possible only to set/clear when empty or going to if( r->tail == r->head ) { // if this emptied the ring, turn off ready } */ + + if( r->rgate != NULL ) { // if locked above... + pthread_mutex_unlock( r->rgate ); + } return r->data[ti]; } @@ -157,7 +221,14 @@ static inline int uta_ring_insert( void* vr, void* new_data ) { r = (ring_t*) vr; } + if( r->wgate != NULL ) { // if lock exists we must honour it + pthread_mutex_lock( r->wgate ); + } + if( r->head+1 == r->tail || (r->head+1 >= r->nelements && !r->tail) ) { // ring is full + if( r->wgate != NULL ) { // ensure released if needed + pthread_mutex_unlock( r->wgate ); + } return 0; } @@ -174,6 +245,9 @@ future -- investigate if it's possible only to set/clear when empty or going to r->head = 0; } + if( r->wgate != NULL ) { // if lock exists we must unlock before going + pthread_mutex_unlock( r->wgate ); + } return 1; }