From 05f9b02e3c0927e69f96bafaac359a53c7f92322 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Fri, 20 May 2016 07:01:03 +0200 Subject: posix: Add and use _POSIX_Get_object_body() --- cpukit/posix/include/rtems/posix/posixapi.h | 32 ++++++++++++++++++++ cpukit/posix/src/condget.c | 43 +++++---------------------- cpukit/posix/src/mutexget.c | 45 +++++------------------------ cpukit/posix/src/prwlockinit.c | 44 +++++----------------------- 4 files changed, 53 insertions(+), 111 deletions(-) (limited to 'cpukit/posix') diff --git a/cpukit/posix/include/rtems/posix/posixapi.h b/cpukit/posix/include/rtems/posix/posixapi.h index 0348e28e16..f2378c184a 100644 --- a/cpukit/posix/include/rtems/posix/posixapi.h +++ b/cpukit/posix/include/rtems/posix/posixapi.h @@ -21,6 +21,7 @@ #include #include +#include #include /** @@ -59,6 +60,37 @@ RTEMS_INLINE_ROUTINE int _POSIX_Get_by_name_error( return _POSIX_Get_by_name_error_table[ error ]; } +/** + * @brief Macro to generate a function body to get a POSIX object by + * identifier. + * + * Generates a function body to get the object for the specified indentifier. + * Performs automatic initialization if requested and necessary. This is an + * ugly macro, since C lacks support for templates. + */ +#define _POSIX_Get_object_body( \ + type, \ + id, \ + lock_context, \ + info, \ + initializer, \ + init \ +) \ + Objects_Control *the_object; \ + if ( id == NULL ) { \ + return NULL; \ + } \ + the_object = _Objects_Get_local( (Objects_Id) *id, lock_context, info ); \ + if ( the_object == NULL ) { \ + _Once_Lock(); \ + if ( *id == initializer ) { \ + init( id, NULL ); \ + } \ + _Once_Unlock(); \ + the_object = _Objects_Get_local( (Objects_Id) *id, lock_context, info ); \ + } \ + return (type *) the_object + /** @} */ #endif diff --git a/cpukit/posix/src/condget.c b/cpukit/posix/src/condget.c index b89566af03..5676de861a 100644 --- a/cpukit/posix/src/condget.c +++ b/cpukit/posix/src/condget.c @@ -12,48 +12,19 @@ #endif #include - -static bool _POSIX_Condition_variables_Check_id_and_auto_init( - pthread_cond_t *cond -) -{ - if ( cond == NULL ) { - return false; - } - - if ( *cond == PTHREAD_COND_INITIALIZER ) { - int eno; - - _Once_Lock(); - - if ( *cond == PTHREAD_COND_INITIALIZER ) { - eno = pthread_cond_init( cond, NULL ); - } else { - eno = 0; - } - - _Once_Unlock(); - - if ( eno != 0 ) { - return false; - } - } - - return true; -} +#include POSIX_Condition_variables_Control *_POSIX_Condition_variables_Get( pthread_cond_t *cond, ISR_lock_Context *lock_context ) { - if ( !_POSIX_Condition_variables_Check_id_and_auto_init( cond ) ) { - return NULL; - } - - return (POSIX_Condition_variables_Control *) _Objects_Get_local( - (Objects_Id) *cond, + _POSIX_Get_object_body( + POSIX_Condition_variables_Control, + cond, lock_context, - &_POSIX_Condition_variables_Information + &_POSIX_Condition_variables_Information, + PTHREAD_COND_INITIALIZER, + pthread_cond_init ); } diff --git a/cpukit/posix/src/mutexget.c b/cpukit/posix/src/mutexget.c index 9d34ecb583..97d6dff658 100644 --- a/cpukit/posix/src/mutexget.c +++ b/cpukit/posix/src/mutexget.c @@ -19,50 +19,19 @@ #endif #include -#include - -static bool _POSIX_Mutex_Check_id_and_auto_init( pthread_mutex_t *mutex ) -{ - if ( mutex == NULL ) { - return false; - } - - if ( *mutex == PTHREAD_MUTEX_INITIALIZER ) { - int eno; - - _Once_Lock(); - - if ( *mutex == PTHREAD_MUTEX_INITIALIZER ) { - eno = pthread_mutex_init( mutex, NULL ); - } else { - eno = 0; - } - - _Once_Unlock(); - - if ( eno != 0 ) { - return false; - } - } - - return true; -} +#include POSIX_Mutex_Control *_POSIX_Mutex_Get_interrupt_disable( pthread_mutex_t *mutex, ISR_lock_Context *lock_context ) { - Objects_Locations location; - - if ( !_POSIX_Mutex_Check_id_and_auto_init( mutex ) ) { - return NULL; - } - - return (POSIX_Mutex_Control *) _Objects_Get_isr_disable( + _POSIX_Get_object_body( + POSIX_Mutex_Control, + mutex, + lock_context, &_POSIX_Mutex_Information, - (Objects_Id) *mutex, - &location, - lock_context + PTHREAD_MUTEX_INITIALIZER, + pthread_mutex_init ); } diff --git a/cpukit/posix/src/prwlockinit.c b/cpukit/posix/src/prwlockinit.c index 8847c17c30..fcf4e29a34 100644 --- a/cpukit/posix/src/prwlockinit.c +++ b/cpukit/posix/src/prwlockinit.c @@ -21,50 +21,20 @@ #endif #include -#include - -static bool _POSIX_RWLock_Check_id_and_auto_init( - pthread_mutex_t *rwlock -) -{ - if ( rwlock == NULL ) { - return false; - } - - if ( *rwlock == PTHREAD_RWLOCK_INITIALIZER ) { - int eno; - - _Once_Lock(); - - if ( *rwlock == PTHREAD_RWLOCK_INITIALIZER ) { - eno = pthread_rwlock_init( rwlock, NULL ); - } else { - eno = 0; - } - - _Once_Unlock(); - - if ( eno != 0 ) { - return false; - } - } - - return true; -} +#include POSIX_RWLock_Control *_POSIX_RWLock_Get( pthread_rwlock_t *rwlock, ISR_lock_Context *lock_context ) { - if ( !_POSIX_RWLock_Check_id_and_auto_init( rwlock ) ) { - return NULL; - } - - return (POSIX_RWLock_Control *) _Objects_Get_local( - *rwlock, + _POSIX_Get_object_body( + POSIX_RWLock_Control, + rwlock, lock_context, - &_POSIX_RWLock_Information + &_POSIX_RWLock_Information, + PTHREAD_RWLOCK_INITIALIZER, + pthread_rwlock_init ); } -- cgit v1.2.3