posix: Add and use _POSIX_Get_object_body()

This commit is contained in:
Sebastian Huber
2016-05-20 07:01:03 +02:00
parent 8a040fe4ee
commit 05f9b02e3c
4 changed files with 53 additions and 111 deletions

View File

@@ -21,6 +21,7 @@
#include <rtems/config.h> #include <rtems/config.h>
#include <rtems/score/assert.h> #include <rtems/score/assert.h>
#include <rtems/score/apimutex.h>
#include <rtems/score/objectimpl.h> #include <rtems/score/objectimpl.h>
/** /**
@@ -59,6 +60,37 @@ RTEMS_INLINE_ROUTINE int _POSIX_Get_by_name_error(
return _POSIX_Get_by_name_error_table[ 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 #endif

View File

@@ -12,48 +12,19 @@
#endif #endif
#include <rtems/posix/condimpl.h> #include <rtems/posix/condimpl.h>
#include <rtems/posix/posixapi.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;
}
POSIX_Condition_variables_Control *_POSIX_Condition_variables_Get( POSIX_Condition_variables_Control *_POSIX_Condition_variables_Get(
pthread_cond_t *cond, pthread_cond_t *cond,
ISR_lock_Context *lock_context ISR_lock_Context *lock_context
) )
{ {
if ( !_POSIX_Condition_variables_Check_id_and_auto_init( cond ) ) { _POSIX_Get_object_body(
return NULL; POSIX_Condition_variables_Control,
} cond,
return (POSIX_Condition_variables_Control *) _Objects_Get_local(
(Objects_Id) *cond,
lock_context, lock_context,
&_POSIX_Condition_variables_Information &_POSIX_Condition_variables_Information,
PTHREAD_COND_INITIALIZER,
pthread_cond_init
); );
} }

View File

@@ -19,50 +19,19 @@
#endif #endif
#include <rtems/posix/muteximpl.h> #include <rtems/posix/muteximpl.h>
#include <rtems/score/apimutex.h> #include <rtems/posix/posixapi.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;
}
POSIX_Mutex_Control *_POSIX_Mutex_Get_interrupt_disable( POSIX_Mutex_Control *_POSIX_Mutex_Get_interrupt_disable(
pthread_mutex_t *mutex, pthread_mutex_t *mutex,
ISR_lock_Context *lock_context ISR_lock_Context *lock_context
) )
{ {
Objects_Locations location; _POSIX_Get_object_body(
POSIX_Mutex_Control,
if ( !_POSIX_Mutex_Check_id_and_auto_init( mutex ) ) { mutex,
return NULL; lock_context,
}
return (POSIX_Mutex_Control *) _Objects_Get_isr_disable(
&_POSIX_Mutex_Information, &_POSIX_Mutex_Information,
(Objects_Id) *mutex, PTHREAD_MUTEX_INITIALIZER,
&location, pthread_mutex_init
lock_context
); );
} }

View File

@@ -21,50 +21,20 @@
#endif #endif
#include <rtems/posix/rwlockimpl.h> #include <rtems/posix/rwlockimpl.h>
#include <rtems/score/apimutex.h> #include <rtems/posix/posixapi.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;
}
POSIX_RWLock_Control *_POSIX_RWLock_Get( POSIX_RWLock_Control *_POSIX_RWLock_Get(
pthread_rwlock_t *rwlock, pthread_rwlock_t *rwlock,
ISR_lock_Context *lock_context ISR_lock_Context *lock_context
) )
{ {
if ( !_POSIX_RWLock_Check_id_and_auto_init( rwlock ) ) { _POSIX_Get_object_body(
return NULL; POSIX_RWLock_Control,
} rwlock,
return (POSIX_RWLock_Control *) _Objects_Get_local(
*rwlock,
lock_context, lock_context,
&_POSIX_RWLock_Information &_POSIX_RWLock_Information,
PTHREAD_RWLOCK_INITIALIZER,
pthread_rwlock_init
); );
} }