From c090db7405b72ce6d0b726c0a39fb1c1aebab7ea Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Tue, 12 Sep 2017 08:09:16 +0200 Subject: posix: Implement self-contained POSIX semaphores For semaphore object pointer and object validation see POSIX_SEMAPHORE_VALIDATE_OBJECT(). Destruction or close of a busy semaphore returns an error status. The object is not flushed. POSIX semaphores are now available in all configurations and no longer depend on --enable-posix. Update #2514. Update #3116. --- cpukit/posix/include/rtems/posix/semaphore.h | 18 ++---- cpukit/posix/include/rtems/posix/semaphoreimpl.h | 82 +++++++++++++----------- 2 files changed, 48 insertions(+), 52 deletions(-) (limited to 'cpukit/posix/include') diff --git a/cpukit/posix/include/rtems/posix/semaphore.h b/cpukit/posix/include/rtems/posix/semaphore.h index 6598397052..9133db22be 100644 --- a/cpukit/posix/include/rtems/posix/semaphore.h +++ b/cpukit/posix/include/rtems/posix/semaphore.h @@ -21,7 +21,6 @@ #include #include -#include #ifdef __cplusplus extern "C" { @@ -41,19 +40,10 @@ extern "C" { */ typedef struct { - Objects_Control Object; - CORE_semaphore_Control Semaphore; - bool named; - bool linked; - uint32_t open_count; - /* - * sem_t is 32-bit. If Object_Id is 16-bit, then they are not - * interchangeable. We have to be able to return a pointer to - * a 32-bit form of the 16-bit Id. - */ - #if defined(RTEMS_USE_16_BIT_OBJECT) - sem_t Semaphore_id; - #endif + Objects_Control Object; + sem_t Semaphore; + bool linked; + uint32_t open_count; } POSIX_Semaphore_Control; /** @} */ diff --git a/cpukit/posix/include/rtems/posix/semaphoreimpl.h b/cpukit/posix/include/rtems/posix/semaphoreimpl.h index 43440298ec..fd17743699 100644 --- a/cpukit/posix/include/rtems/posix/semaphoreimpl.h +++ b/cpukit/posix/include/rtems/posix/semaphoreimpl.h @@ -21,21 +21,25 @@ #include #include -#include +#include #include #ifdef __cplusplus extern "C" { #endif +/** + * @brief This is a random number used to check if a semaphore object is + * properly initialized. + */ +#define POSIX_SEMAPHORE_MAGIC 0x5d367fe7UL + /** * This defines the information control block used to manage * this class of objects. */ extern Objects_Information _POSIX_Semaphore_Information; -#define POSIX_SEMAPHORE_TQ_OPERATIONS &_Thread_queue_Operations_FIFO - RTEMS_INLINE_ROUTINE POSIX_Semaphore_Control * _POSIX_Semaphore_Allocate_unprotected( void ) { @@ -57,53 +61,45 @@ RTEMS_INLINE_ROUTINE void _POSIX_Semaphore_Free ( } RTEMS_INLINE_ROUTINE POSIX_Semaphore_Control *_POSIX_Semaphore_Get( - const sem_t *id, - Thread_queue_Context *queue_context + sem_t *sem ) { - _Thread_queue_Context_initialize( queue_context ); - return (POSIX_Semaphore_Control *) _Objects_Get( - (Objects_Id) *id, - &queue_context->Lock_context.Lock_context, - &_POSIX_Semaphore_Information - ); + return RTEMS_CONTAINER_OF( sem, POSIX_Semaphore_Control, Semaphore ); } -/** - * @brief POSIX Semaphore Create Support - * - * This routine supports the sem_init and sem_open routines. - */ +RTEMS_INLINE_ROUTINE bool _POSIX_Semaphore_Is_named( const sem_t *sem ) +{ + return sem->_Semaphore._Queue._name != NULL; +} -int _POSIX_Semaphore_Create_support( - const char *name, - size_t name_len, - unsigned int value, - POSIX_Semaphore_Control **the_sem -); +RTEMS_INLINE_ROUTINE bool _POSIX_Semaphore_Is_busy( const sem_t *sem ) +{ + return sem->_Semaphore._Queue._heads != NULL; +} + +RTEMS_INLINE_ROUTINE void _POSIX_Semaphore_Initialize( + sem_t *sem, + const char *name, + unsigned int value +) +{ + sem->_flags = (uintptr_t) sem ^ POSIX_SEMAPHORE_MAGIC; + _Semaphore_Initialize_named( &sem->_Semaphore, name, value ); +} + +RTEMS_INLINE_ROUTINE void _POSIX_Semaphore_Destroy( sem_t *sem ) +{ + sem->_flags = 0; + _Semaphore_Destroy( &sem->_Semaphore ); +} /** * @brief POSIX Semaphore Delete * * This routine supports the sem_close and sem_unlink routines. */ -void _POSIX_Semaphore_Delete( - POSIX_Semaphore_Control *the_semaphore, - Thread_queue_Context *queue_context -); +void _POSIX_Semaphore_Delete( POSIX_Semaphore_Control *the_semaphore ); -/** - * @brief POSIX semaphore wait support. - * - * This routine supports the sem_wait, sem_trywait, and sem_timedwait - * services. - */ -int _POSIX_Semaphore_Wait_support( - sem_t *sem, - bool blocking, - Watchdog_Interval timeout -); - /** * @brief POSIX Semaphore Namespace Remove */ @@ -129,6 +125,16 @@ RTEMS_INLINE_ROUTINE POSIX_Semaphore_Control *_POSIX_Semaphore_Get_by_name( ); } +#define POSIX_SEMAPHORE_VALIDATE_OBJECT( sem ) \ + do { \ + if ( \ + ( sem ) == NULL \ + || ( (uintptr_t) ( sem ) ^ POSIX_SEMAPHORE_MAGIC ) != ( sem )->_flags \ + ) { \ + rtems_set_errno_and_return_minus_one( EINVAL ); \ + } \ + } while ( 0 ) + #ifdef __cplusplus } #endif -- cgit v1.2.3