[PATCH 03/15] posix: Add and use _POSIX_Get_object_body()

Sebastian Huber sebastian.huber at embedded-brains.de
Fri May 20 13:33:40 UTC 2016


---
 cpukit/posix/include/rtems/posix/posixapi.h | 29 +++++++++++++++++++
 cpukit/posix/src/condget.c                  | 43 +++++----------------------
 cpukit/posix/src/mutexget.c                 | 45 +++++------------------------
 cpukit/posix/src/prwlockinit.c              | 44 +++++-----------------------
 4 files changed, 50 insertions(+), 111 deletions(-)

diff --git a/cpukit/posix/include/rtems/posix/posixapi.h b/cpukit/posix/include/rtems/posix/posixapi.h
index 0348e28..0001d98 100644
--- a/cpukit/posix/include/rtems/posix/posixapi.h
+++ b/cpukit/posix/include/rtems/posix/posixapi.h
@@ -21,6 +21,7 @@
 
 #include <rtems/config.h>
 #include <rtems/score/assert.h>
+#include <rtems/score/apimutex.h>
 #include <rtems/score/objectimpl.h>
 
 /**
@@ -59,6 +60,34 @@ RTEMS_INLINE_ROUTINE int _POSIX_Get_by_name_error(
   return _POSIX_Get_by_name_error_table[ error ];
 }
 
+/*
+ * 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 b89566a..5676de8 100644
--- a/cpukit/posix/src/condget.c
+++ b/cpukit/posix/src/condget.c
@@ -12,48 +12,19 @@
 #endif
 
 #include <rtems/posix/condimpl.h>
-
-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 <rtems/posix/posixapi.h>
 
 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 9d34ecb..97d6dff 100644
--- a/cpukit/posix/src/mutexget.c
+++ b/cpukit/posix/src/mutexget.c
@@ -19,50 +19,19 @@
 #endif
 
 #include <rtems/posix/muteximpl.h>
-#include <rtems/score/apimutex.h>
-
-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 <rtems/posix/posixapi.h>
 
 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 8847c17..fcf4e29 100644
--- a/cpukit/posix/src/prwlockinit.c
+++ b/cpukit/posix/src/prwlockinit.c
@@ -21,50 +21,20 @@
 #endif
 
 #include <rtems/posix/rwlockimpl.h>
-#include <rtems/score/apimutex.h>
-
-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 <rtems/posix/posixapi.h>
 
 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
   );
 }
 
-- 
1.8.4.5



More information about the devel mailing list