From de59c065c57cb8526662ee6da28a57ad16fdde66 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 27 Sep 2017 15:08:33 +0200 Subject: posix: Implement self-contained POSIX mutex POSIX mutexes are now available in all configurations and no longer depend on --enable-posix. Update #2514. Update #3112. --- cpukit/posix/Makefile.am | 23 +- cpukit/posix/include/rtems/posix/config.h | 6 - cpukit/posix/include/rtems/posix/mutex.h | 75 ----- cpukit/posix/include/rtems/posix/muteximpl.h | 454 ++++++++++++++++++++++++--- cpukit/posix/preinstall.am | 12 +- cpukit/posix/src/mutex.c | 75 ----- cpukit/posix/src/mutexdestroy.c | 33 +- cpukit/posix/src/mutexget.c | 37 --- cpukit/posix/src/mutexgetprioceiling.c | 17 +- cpukit/posix/src/mutexinit.c | 83 +++-- cpukit/posix/src/mutexlocksupp.c | 79 ++--- cpukit/posix/src/mutexsetprioceiling.c | 16 +- cpukit/posix/src/mutexunlock.c | 75 ++++- 13 files changed, 607 insertions(+), 378 deletions(-) delete mode 100644 cpukit/posix/include/rtems/posix/mutex.h delete mode 100644 cpukit/posix/src/mutex.c delete mode 100644 cpukit/posix/src/mutexget.c (limited to 'cpukit/posix') diff --git a/cpukit/posix/Makefile.am b/cpukit/posix/Makefile.am index 99d1e7d08e..b8618e1689 100644 --- a/cpukit/posix/Makefile.am +++ b/cpukit/posix/Makefile.am @@ -21,6 +21,7 @@ include_rtems_posix_HEADERS += include/rtems/posix/condimpl.h include_rtems_posix_HEADERS += include/rtems/posix/key.h include_rtems_posix_HEADERS += include/rtems/posix/keyimpl.h include_rtems_posix_HEADERS += include/rtems/posix/config.h +include_rtems_posix_HEADERS += include/rtems/posix/muteximpl.h include_rtems_posix_HEADERS += include/rtems/posix/posixapi.h include_rtems_posix_HEADERS += include/rtems/posix/priorityimpl.h include_rtems_posix_HEADERS += include/rtems/posix/rwlockimpl.h @@ -38,8 +39,6 @@ include_rtems_posix_HEADERS += include/rtems/posix/aio_misc.h include_rtems_posix_HEADERS += include/rtems/posix/mmanimpl.h include_rtems_posix_HEADERS += include/rtems/posix/mqueue.h include_rtems_posix_HEADERS += include/rtems/posix/mqueueimpl.h -include_rtems_posix_HEADERS += include/rtems/posix/mutex.h -include_rtems_posix_HEADERS += include/rtems/posix/muteximpl.h include_rtems_posix_HEADERS += include/rtems/posix/psignal.h include_rtems_posix_HEADERS += include/rtems/posix/psignalimpl.h include_rtems_posix_HEADERS += include/rtems/posix/pthread.h @@ -80,6 +79,16 @@ libposix_a_SOURCES += src/condattrdestroy.c \ src/condinit.c src/condsignal.c src/condsignalsupp.c \ src/condtimedwait.c src/condwait.c src/condwaitsupp.c +## MUTEX_C_FILES +libposix_a_SOURCES += src/mutexattrdestroy.c src/mutexattrgetprioceiling.c \ + src/mutexattrgetprotocol.c src/mutexattrgetpshared.c \ + src/mutexattrgettype.c src/mutexattrinit.c src/mutexattrsetprioceiling.c \ + src/mutexattrsetprotocol.c src/mutexattrsetpshared.c \ + src/mutexattrsettype.c src/mutexdestroy.c \ + src/mutexgetprioceiling.c src/mutexinit.c src/mutexlock.c \ + src/mutexlocksupp.c src/mutexsetprioceiling.c src/mutextimedlock.c \ + src/mutextrylock.c src/mutexunlock.c + if HAS_PTHREADS libposix_a_SOURCES += src/pthreadatfork.c @@ -113,16 +122,6 @@ libposix_a_SOURCES += src/mqueue.c src/mqueueclose.c \ src/mqueuetimedsend.c \ src/mqueueunlink.c -## MUTEX_C_FILES -libposix_a_SOURCES += src/mutexattrdestroy.c src/mutexattrgetprioceiling.c \ - src/mutexattrgetprotocol.c src/mutexattrgetpshared.c \ - src/mutexattrgettype.c src/mutexattrinit.c src/mutexattrsetprioceiling.c \ - src/mutexattrsetprotocol.c src/mutexattrsetpshared.c \ - src/mutexattrsettype.c src/mutex.c src/mutexdestroy.c src/mutexget.c \ - src/mutexgetprioceiling.c src/mutexinit.c src/mutexlock.c \ - src/mutexlocksupp.c src/mutexsetprioceiling.c src/mutextimedlock.c \ - src/mutextrylock.c src/mutexunlock.c - ## PTHREAD_C_FILES libposix_a_SOURCES += src/pthreadatfork.c src/pthreadattrdestroy.c \ src/pthreadattrgetdetachstate.c src/pthreadattrgetinheritsched.c \ diff --git a/cpukit/posix/include/rtems/posix/config.h b/cpukit/posix/include/rtems/posix/config.h index 2f5a5d7600..e90dc27011 100644 --- a/cpukit/posix/include/rtems/posix/config.h +++ b/cpukit/posix/include/rtems/posix/config.h @@ -62,12 +62,6 @@ typedef struct { */ uint32_t maximum_threads; - /** - * This field contains the maximum number of POSIX API - * mutexes which are configured for this application. - */ - uint32_t maximum_mutexes; - /** * This field contains the maximum number of POSIX API * timers which are configured for this application. diff --git a/cpukit/posix/include/rtems/posix/mutex.h b/cpukit/posix/include/rtems/posix/mutex.h deleted file mode 100644 index 97ab138652..0000000000 --- a/cpukit/posix/include/rtems/posix/mutex.h +++ /dev/null @@ -1,75 +0,0 @@ -/** - * @file - * - * @brief POSIX MUTEX Support - * - * This include file contains all the private support information for - * POSIX mutex's. - */ - -/* - * COPYRIGHT (c) 1989-2011. - * On-Line Applications Research Corporation (OAR). - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rtems.org/license/LICENSE. - */ - -#ifndef _RTEMS_POSIX_MUTEX_H -#define _RTEMS_POSIX_MUTEX_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @defgroup POSIX_MUTEX POSIX Mutex Support - * - * @ingroup POSIXAPI - * - * @brief Private Support Information for POSIX Mutex - * - */ -/**@{**/ - -/** - * @brief The POSIX mutex control. - */ -typedef struct { - /** - * @brief The object control. - */ - Objects_Control Object; - - /** - * The most general mutex variant supported by a POSIX mutex. - * - * The priority inheritance or no protocol variants will use only parts of - * this structure. - */ - CORE_ceiling_mutex_Control Mutex; - - /** - * @brief The protocol variant. - * - * @see POSIX_Mutex_Protocol. - */ - unsigned int protocol : 2; - - /** - * @brief Indicates if this is a non-recursive or recursive mutex. - */ - unsigned int is_recursive : 1; -} POSIX_Mutex_Control; - -/** @} */ - -#ifdef __cplusplus -} -#endif - -#endif -/* end of include file */ diff --git a/cpukit/posix/include/rtems/posix/muteximpl.h b/cpukit/posix/include/rtems/posix/muteximpl.h index 7d19d8c40b..833938bb2d 100644 --- a/cpukit/posix/include/rtems/posix/muteximpl.h +++ b/cpukit/posix/include/rtems/posix/muteximpl.h @@ -18,18 +18,40 @@ #ifndef _RTEMS_POSIX_MUTEXIMPL_H #define _RTEMS_POSIX_MUTEXIMPL_H -#include -#include - #include #include +#include +#include +#include + #ifdef __cplusplus extern "C" { #endif +typedef struct { + unsigned long flags; + Mutex_recursive_Control Recursive; + Priority_Node Priority_ceiling; + const Scheduler_Control *scheduler; +} POSIX_Mutex_Control; + +#define POSIX_MUTEX_PROTOCOL_MASK 0x3UL + +#define POSIX_MUTEX_RECURSIVE 0x4UL + +#define POSIX_MUTEX_FLAGS_MASK 0x7UL + +#define POSIX_MUTEX_MAGIC 0x961c13b8UL + #define POSIX_MUTEX_NO_PROTOCOL_TQ_OPERATIONS &_Thread_queue_Operations_FIFO +#define POSIX_MUTEX_PRIORITY_INHERIT_TQ_OPERATIONS \ + &_Thread_queue_Operations_priority_inherit + +#define POSIX_MUTEX_PRIORITY_CEILING_TQ_OPERATIONS \ + &_Thread_queue_Operations_priority + /** * @brief Supported POSIX mutex protocols. * @@ -41,26 +63,30 @@ typedef enum { POSIX_MUTEX_PRIORITY_CEILING } POSIX_Mutex_Protocol; -/** - * The following defines the information control block used to manage - * this class of objects. - */ -extern Objects_Information _POSIX_Mutex_Information; - /** * The default mutex attributes structure. */ extern const pthread_mutexattr_t _POSIX_Mutex_Default_attributes; -RTEMS_INLINE_ROUTINE void _POSIX_Mutex_Acquire_critical( +RTEMS_INLINE_ROUTINE Thread_Control *_POSIX_Mutex_Acquire( POSIX_Mutex_Control *the_mutex, Thread_queue_Context *queue_context ) { - _CORE_mutex_Acquire_critical( - &the_mutex->Mutex.Recursive.Mutex, - queue_context + ISR_Level level; + Thread_Control *executing; + + _Thread_queue_Context_initialize( queue_context ); + _Thread_queue_Context_ISR_disable( queue_context, level ); + _Thread_queue_Context_set_ISR_level( queue_context, level ); + executing = _Thread_Executing; + _Thread_queue_Queue_acquire_critical( + &the_mutex->Recursive.Mutex.Queue.Queue, + &executing->Potpourri_stats, + &queue_context->Lock_context.Lock_context ); + + return executing; } RTEMS_INLINE_ROUTINE void _POSIX_Mutex_Release( @@ -68,36 +94,359 @@ RTEMS_INLINE_ROUTINE void _POSIX_Mutex_Release( Thread_queue_Context *queue_context ) { - _CORE_mutex_Release( - &the_mutex->Mutex.Recursive.Mutex, + _Thread_queue_Queue_release( + &the_mutex->Recursive.Mutex.Queue.Queue, + &queue_context->Lock_context.Lock_context + ); +} + +RTEMS_INLINE_ROUTINE POSIX_Mutex_Protocol _POSIX_Mutex_Get_protocol( + unsigned long flags +) +{ + return flags & POSIX_MUTEX_PROTOCOL_MASK; +} + +RTEMS_INLINE_ROUTINE bool _POSIX_Mutex_Is_recursive( + unsigned long flags +) +{ + return ( flags & POSIX_MUTEX_RECURSIVE ) != 0; +} + +RTEMS_INLINE_ROUTINE Thread_Control *_POSIX_Mutex_Get_owner( + const POSIX_Mutex_Control *the_mutex +) +{ + return the_mutex->Recursive.Mutex.Queue.Queue.owner; +} + +RTEMS_INLINE_ROUTINE bool _POSIX_Mutex_Is_locked( + const POSIX_Mutex_Control *the_mutex +) +{ + return _POSIX_Mutex_Get_owner( the_mutex ) != NULL; +} + +Status_Control _POSIX_Mutex_Seize_slow( + POSIX_Mutex_Control *the_mutex, + const Thread_queue_Operations *operations, + Thread_Control *executing, + bool wait, + Thread_queue_Context *queue_context +); + +RTEMS_INLINE_ROUTINE void _POSIX_Mutex_Set_owner( + POSIX_Mutex_Control *the_mutex, + Thread_Control *owner +) +{ + the_mutex->Recursive.Mutex.Queue.Queue.owner = owner; +} + +RTEMS_INLINE_ROUTINE bool _POSIX_Mutex_Is_owner( + const POSIX_Mutex_Control *the_mutex, + const Thread_Control *the_thread +) +{ + return _POSIX_Mutex_Get_owner( the_mutex ) == the_thread; +} + +static Status_Control _POSIX_Mutex_Lock_nested( + POSIX_Mutex_Control *the_mutex, + unsigned long flags +) +{ + + if ( _POSIX_Mutex_Is_recursive( flags ) ) { + ++the_mutex->Recursive.nest_level; + return STATUS_SUCCESSFUL; + } else { + return STATUS_NESTING_NOT_ALLOWED; + } +} + +RTEMS_INLINE_ROUTINE Status_Control _POSIX_Mutex_Seize( + POSIX_Mutex_Control *the_mutex, + unsigned long flags, + const Thread_queue_Operations *operations, + Thread_Control *executing, + bool wait, + Thread_queue_Context *queue_context +) +{ + Thread_Control *owner; + + owner = _POSIX_Mutex_Get_owner( the_mutex ); + + if ( owner == NULL ) { + _POSIX_Mutex_Set_owner( the_mutex, executing ); + _Thread_Resource_count_increment( executing ); + _POSIX_Mutex_Release( the_mutex, queue_context ); + return STATUS_SUCCESSFUL; + } + + if ( owner == executing ) { + Status_Control status; + + status = _POSIX_Mutex_Lock_nested( the_mutex, flags ); + _POSIX_Mutex_Release( the_mutex, queue_context ); + return status; + } + + return _POSIX_Mutex_Seize_slow( + the_mutex, + operations, + executing, + wait, queue_context ); } -/** - * @brief POSIX Mutex Allocate - * - * This function allocates a mutexes control block from - * the inactive chain of free mutexes control blocks. - */ -RTEMS_INLINE_ROUTINE POSIX_Mutex_Control *_POSIX_Mutex_Allocate( void ) +RTEMS_INLINE_ROUTINE Status_Control _POSIX_Mutex_Surrender( + POSIX_Mutex_Control *the_mutex, + const Thread_queue_Operations *operations, + Thread_Control *executing, + Thread_queue_Context *queue_context +) { - return (POSIX_Mutex_Control *) _Objects_Allocate( &_POSIX_Mutex_Information ); + unsigned int nest_level; + Thread_queue_Heads *heads; + + if ( !_POSIX_Mutex_Is_owner( the_mutex, executing ) ) { + _POSIX_Mutex_Release( the_mutex, queue_context ); + return STATUS_NOT_OWNER; + } + + nest_level = the_mutex->Recursive.nest_level; + + if ( nest_level > 0 ) { + the_mutex->Recursive.nest_level = nest_level - 1; + _POSIX_Mutex_Release( the_mutex, queue_context ); + return STATUS_SUCCESSFUL; + } + + _Thread_Resource_count_decrement( executing ); + _POSIX_Mutex_Set_owner( the_mutex, NULL ); + + heads = the_mutex->Recursive.Mutex.Queue.Queue.heads; + + if ( heads == NULL ) { + _POSIX_Mutex_Release( the_mutex, queue_context ); + return STATUS_SUCCESSFUL; + } + + _Thread_queue_Surrender( + &the_mutex->Recursive.Mutex.Queue.Queue, + heads, + executing, + queue_context, + operations + ); + return STATUS_SUCCESSFUL; } -/** - * @brief POSIX Mutex Free - * - * This routine frees a mutexes control block to the - * inactive chain of free mutexes control blocks. - */ -RTEMS_INLINE_ROUTINE void _POSIX_Mutex_Free( - POSIX_Mutex_Control *the_mutex +RTEMS_INLINE_ROUTINE const Scheduler_Control *_POSIX_Mutex_Get_scheduler( + const POSIX_Mutex_Control *the_mutex ) { - _Objects_Free( &_POSIX_Mutex_Information, &the_mutex->Object ); +#if defined(RTEMS_SMP) + return the_mutex->scheduler; +#else + return &_Scheduler_Table[ 0 ]; +#endif +} + +RTEMS_INLINE_ROUTINE void _POSIX_Mutex_Set_priority( + POSIX_Mutex_Control *the_mutex, + Priority_Control priority_ceiling, + Thread_queue_Context *queue_context +) +{ + Thread_Control *owner; + + owner = _POSIX_Mutex_Get_owner( the_mutex ); + + if ( owner != NULL ) { + _Thread_Wait_acquire( owner, queue_context ); + _Thread_Priority_change( + owner, + &the_mutex->Priority_ceiling, + priority_ceiling, + false, + queue_context + ); + _Thread_Wait_release( owner, queue_context ); + } else { + the_mutex->Priority_ceiling.priority = priority_ceiling; + } +} + +RTEMS_INLINE_ROUTINE Priority_Control _POSIX_Mutex_Get_priority( + const POSIX_Mutex_Control *the_mutex +) +{ + return the_mutex->Priority_ceiling.priority; +} + +RTEMS_INLINE_ROUTINE Status_Control _POSIX_Mutex_Ceiling_set_owner( + POSIX_Mutex_Control *the_mutex, + Thread_Control *owner, + Thread_queue_Context *queue_context +) +{ + ISR_lock_Context lock_context; + Scheduler_Node *scheduler_node; + Per_CPU_Control *cpu_self; + + _Thread_Wait_acquire_default_critical( owner, &lock_context ); + + scheduler_node = _Thread_Scheduler_get_home_node( owner ); + + if ( + _Priority_Get_priority( &scheduler_node->Wait.Priority ) + < the_mutex->Priority_ceiling.priority + ) { + _Thread_Wait_release_default_critical( owner, &lock_context ); + _POSIX_Mutex_Release( the_mutex, queue_context ); + return STATUS_MUTEX_CEILING_VIOLATED; + } + + _POSIX_Mutex_Set_owner( the_mutex, owner ); + _Thread_Resource_count_increment( owner ); + _Thread_Priority_add( + owner, + &the_mutex->Priority_ceiling, + queue_context + ); + _Thread_Wait_release_default_critical( owner, &lock_context ); + + cpu_self = _Thread_Dispatch_disable_critical( + &queue_context->Lock_context.Lock_context + ); + _POSIX_Mutex_Release( the_mutex, queue_context ); + _Thread_Priority_update( queue_context ); + _Thread_Dispatch_enable( cpu_self ); + return STATUS_SUCCESSFUL; +} + +RTEMS_INLINE_ROUTINE Status_Control _POSIX_Mutex_Ceiling_seize( + POSIX_Mutex_Control *the_mutex, + unsigned long flags, + Thread_Control *executing, + bool wait, + Thread_queue_Context *queue_context +) +{ + Thread_Control *owner; + + owner = _POSIX_Mutex_Get_owner( the_mutex ); + + if ( owner == NULL ) { +#if defined(RTEMS_SMP) + if ( + _Thread_Scheduler_get_home( executing ) + != _POSIX_Mutex_Get_scheduler( the_mutex ) + ) { + _POSIX_Mutex_Release( the_mutex, queue_context ); + return STATUS_NOT_DEFINED; + } +#endif + + _Thread_queue_Context_clear_priority_updates( queue_context ); + return _POSIX_Mutex_Ceiling_set_owner( + the_mutex, + executing, + queue_context + ); + } + + if ( owner == executing ) { + Status_Control status; + + status = _POSIX_Mutex_Lock_nested( the_mutex, flags ); + _POSIX_Mutex_Release( the_mutex, queue_context ); + return status; + } + + return _POSIX_Mutex_Seize_slow( + the_mutex, + POSIX_MUTEX_PRIORITY_CEILING_TQ_OPERATIONS, + executing, + wait, + queue_context + ); } +RTEMS_INLINE_ROUTINE Status_Control _POSIX_Mutex_Ceiling_surrender( + POSIX_Mutex_Control *the_mutex, + Thread_Control *executing, + Thread_queue_Context *queue_context +) +{ + unsigned int nest_level; + ISR_lock_Context lock_context; + Per_CPU_Control *cpu_self; + Thread_queue_Heads *heads; + + if ( !_POSIX_Mutex_Is_owner( the_mutex, executing ) ) { + _POSIX_Mutex_Release( the_mutex, queue_context ); + return STATUS_NOT_OWNER; + } + + nest_level = the_mutex->Recursive.nest_level; + + if ( nest_level > 0 ) { + the_mutex->Recursive.nest_level = nest_level - 1; + _POSIX_Mutex_Release( the_mutex, queue_context ); + return STATUS_SUCCESSFUL; + } + + _Thread_Resource_count_decrement( executing ); + + _Thread_queue_Context_clear_priority_updates( queue_context ); + _Thread_Wait_acquire_default_critical( executing, &lock_context ); + _Thread_Priority_remove( + executing, + &the_mutex->Priority_ceiling, + queue_context + ); + _Thread_Wait_release_default_critical( executing, &lock_context ); + + cpu_self = _Thread_Dispatch_disable_critical( + &queue_context->Lock_context.Lock_context + ); + + heads = the_mutex->Recursive.Mutex.Queue.Queue.heads; + + if ( heads != NULL ) { + const Thread_queue_Operations *operations; + Thread_Control *new_owner; + + operations = POSIX_MUTEX_PRIORITY_CEILING_TQ_OPERATIONS; + new_owner = ( *operations->first )( heads ); + _POSIX_Mutex_Set_owner( the_mutex, new_owner ); + _Thread_Resource_count_increment( new_owner ); + _Thread_Priority_add( + new_owner, + &the_mutex->Priority_ceiling, + queue_context + ); + _Thread_queue_Extract_critical( + &the_mutex->Recursive.Mutex.Queue.Queue, + operations, + new_owner, + queue_context + ); + } else { + _POSIX_Mutex_Set_owner( the_mutex, NULL ); + _POSIX_Mutex_Release( the_mutex, queue_context ); + } + + _Thread_Priority_update( queue_context ); + _Thread_Dispatch_enable( cpu_self ); + return STATUS_SUCCESSFUL; +} /** * @brief POSIX Mutex Lock Support Method @@ -111,29 +460,32 @@ int _POSIX_Mutex_Lock_support( Watchdog_Interval timeout ); -/** - * @brief POSIX Mutex Get (Interrupt Disable) - * - * A support routine which translates the mutex id into a local pointer. - * As a side-effect, it may create the mutex. - * - * @note: This version of the method uses an interrupt critical section. - */ -POSIX_Mutex_Control *_POSIX_Mutex_Get( - pthread_mutex_t *mutex, - Thread_queue_Context *queue_context -); - -RTEMS_INLINE_ROUTINE POSIX_Mutex_Control *_POSIX_Mutex_Get_no_protection( - const pthread_mutex_t *mutex +static inline POSIX_Mutex_Control *_POSIX_Mutex_Get( + pthread_mutex_t *mutex ) { - return (POSIX_Mutex_Control *) _Objects_Get_no_protection( - (Objects_Id) *mutex, - &_POSIX_Mutex_Information - ); + return (POSIX_Mutex_Control *) mutex; } +bool _POSIX_Mutex_Auto_initialization( POSIX_Mutex_Control *the_mutex ); + +#define POSIX_MUTEX_VALIDATE_OBJECT( the_mutex, flags ) \ + do { \ + if ( ( the_mutex ) == NULL ) { \ + return EINVAL; \ + } \ + flags = ( the_mutex )->flags; \ + if ( \ + ( ( (uintptr_t) ( the_mutex ) ^ POSIX_MUTEX_MAGIC ) \ + & ~POSIX_MUTEX_FLAGS_MASK ) \ + != ( flags & ~POSIX_MUTEX_FLAGS_MASK ) \ + ) { \ + if ( !_POSIX_Mutex_Auto_initialization( the_mutex ) ) { \ + return EINVAL; \ + } \ + } \ + } while ( 0 ) + #ifdef __cplusplus } #endif diff --git a/cpukit/posix/preinstall.am b/cpukit/posix/preinstall.am index 285999360f..c3ce5e5aa9 100644 --- a/cpukit/posix/preinstall.am +++ b/cpukit/posix/preinstall.am @@ -47,6 +47,10 @@ $(PROJECT_INCLUDE)/rtems/posix/config.h: include/rtems/posix/config.h $(PROJECT_ $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/config.h PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/config.h +$(PROJECT_INCLUDE)/rtems/posix/muteximpl.h: include/rtems/posix/muteximpl.h $(PROJECT_INCLUDE)/rtems/posix/$(dirstamp) + $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/muteximpl.h +PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/muteximpl.h + $(PROJECT_INCLUDE)/rtems/posix/posixapi.h: include/rtems/posix/posixapi.h $(PROJECT_INCLUDE)/rtems/posix/$(dirstamp) $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/posixapi.h PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/posixapi.h @@ -96,14 +100,6 @@ $(PROJECT_INCLUDE)/rtems/posix/mqueueimpl.h: include/rtems/posix/mqueueimpl.h $( $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/mqueueimpl.h PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/mqueueimpl.h -$(PROJECT_INCLUDE)/rtems/posix/mutex.h: include/rtems/posix/mutex.h $(PROJECT_INCLUDE)/rtems/posix/$(dirstamp) - $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/mutex.h -PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/mutex.h - -$(PROJECT_INCLUDE)/rtems/posix/muteximpl.h: include/rtems/posix/muteximpl.h $(PROJECT_INCLUDE)/rtems/posix/$(dirstamp) - $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/muteximpl.h -PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/muteximpl.h - $(PROJECT_INCLUDE)/rtems/posix/psignal.h: include/rtems/posix/psignal.h $(PROJECT_INCLUDE)/rtems/posix/$(dirstamp) $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/psignal.h PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/psignal.h diff --git a/cpukit/posix/src/mutex.c b/cpukit/posix/src/mutex.c deleted file mode 100644 index 3e34dffb3d..0000000000 --- a/cpukit/posix/src/mutex.c +++ /dev/null @@ -1,75 +0,0 @@ -/** - * @file - * - * @brief POSIX Mutex Manager Initialization - * @ingroup POSIX_MUTEX POSIX Mutex Support - */ - -/* - * COPYRIGHT (c) 1989-2008. - * On-Line Applications Research Corporation (OAR). - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rtems.org/license/LICENSE. - */ - -#if HAVE_CONFIG_H -#include "config.h" -#endif - -#include - -#include -#include -#include -#include - -Objects_Information _POSIX_Mutex_Information; - -const pthread_mutexattr_t _POSIX_Mutex_Default_attributes = { -#if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES) - .type = PTHREAD_MUTEX_DEFAULT, -#endif - .is_initialized = true, - .process_shared = PTHREAD_PROCESS_PRIVATE, - .prio_ceiling = INT_MAX, - .protocol = PTHREAD_PRIO_NONE, - .recursive = false -}; - -/* - * _POSIX_Mutex_Manager_initialization - * - * This routine initializes all mutex manager related data structures. - * - * Input parameters: - * maximum_mutexes - maximum configured mutexes - * - * Output parameters: NONE - */ - -static void _POSIX_Mutex_Manager_initialization(void) -{ - /* - * Initialize the POSIX mutex object class information structure. - */ - _Objects_Initialize_information( - &_POSIX_Mutex_Information, /* object information table */ - OBJECTS_POSIX_API, /* object API */ - OBJECTS_POSIX_MUTEXES, /* object class */ - Configuration_POSIX_API.maximum_mutexes, - /* maximum objects of this class */ - sizeof( POSIX_Mutex_Control ), - /* size of this object's control block */ - true, /* true if names for this object are strings */ - _POSIX_PATH_MAX, /* maximum length of each object's name */ - NULL /* Proxy extraction support callout */ - ); -} - -RTEMS_SYSINIT_ITEM( - _POSIX_Mutex_Manager_initialization, - RTEMS_SYSINIT_POSIX_MUTEX, - RTEMS_SYSINIT_ORDER_MIDDLE -); diff --git a/cpukit/posix/src/mutexdestroy.c b/cpukit/posix/src/mutexdestroy.c index 57e63937a9..193f3391bc 100644 --- a/cpukit/posix/src/mutexdestroy.c +++ b/cpukit/posix/src/mutexdestroy.c @@ -29,37 +29,22 @@ int pthread_mutex_destroy( ) { POSIX_Mutex_Control *the_mutex; + unsigned long flags; Thread_queue_Context queue_context; int eno; - _Objects_Allocator_lock(); + the_mutex = _POSIX_Mutex_Get( mutex ); + POSIX_MUTEX_VALIDATE_OBJECT( the_mutex, flags ); - the_mutex = _POSIX_Mutex_Get( mutex, &queue_context ); + _POSIX_Mutex_Acquire( the_mutex, &queue_context ); - if ( the_mutex != NULL ) { - _POSIX_Mutex_Acquire_critical( the_mutex, &queue_context ); - - /* - * XXX: There is an error for the mutex being locked - * or being in use by a condition variable. - */ - - if ( - !_CORE_mutex_Is_locked( &the_mutex->Mutex.Recursive.Mutex ) - ) { - _Objects_Close( &_POSIX_Mutex_Information, &the_mutex->Object ); - _POSIX_Mutex_Release( the_mutex, &queue_context ); - _CORE_mutex_Destroy( &the_mutex->Mutex.Recursive.Mutex ); - _POSIX_Mutex_Free( the_mutex ); - eno = 0; - } else { - _POSIX_Mutex_Release( the_mutex, &queue_context ); - eno = EBUSY; - } + if ( _POSIX_Mutex_Get_owner( the_mutex ) == NULL ) { + the_mutex->flags = ~the_mutex->flags; + eno = 0; } else { - eno = EINVAL; + eno = EBUSY; } - _Objects_Allocator_unlock(); + _POSIX_Mutex_Release( the_mutex, &queue_context ); return eno; } diff --git a/cpukit/posix/src/mutexget.c b/cpukit/posix/src/mutexget.c deleted file mode 100644 index e90c41c3a2..0000000000 --- a/cpukit/posix/src/mutexget.c +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @file - * - * @brief Convert POSIX Mutex ID to local object pointer - * @ingroup POSIXAPI - */ - -/* - * COPYRIGHT (c) 1989-2007. - * On-Line Applications Research Corporation (OAR). - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rtems.org/license/LICENSE. - */ - -#if HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include - -POSIX_Mutex_Control *_POSIX_Mutex_Get( - pthread_mutex_t *mutex, - Thread_queue_Context *queue_context -) -{ - _POSIX_Get_object_body( - POSIX_Mutex_Control, - mutex, - queue_context, - &_POSIX_Mutex_Information, - PTHREAD_MUTEX_INITIALIZER, - pthread_mutex_init - ); -} diff --git a/cpukit/posix/src/mutexgetprioceiling.c b/cpukit/posix/src/mutexgetprioceiling.c index dfff98f9b7..544c8fbf33 100644 --- a/cpukit/posix/src/mutexgetprioceiling.c +++ b/cpukit/posix/src/mutexgetprioceiling.c @@ -31,30 +31,27 @@ int pthread_mutex_getprioceiling( ) { POSIX_Mutex_Control *the_mutex; + unsigned long flags; Thread_queue_Context queue_context; if ( prioceiling == NULL ) { return EINVAL; } - the_mutex = _POSIX_Mutex_Get( mutex, &queue_context ); + the_mutex = _POSIX_Mutex_Get( mutex ); + POSIX_MUTEX_VALIDATE_OBJECT( the_mutex, flags ); - if ( the_mutex == NULL ) { - return EINVAL; - } + _POSIX_Mutex_Acquire( the_mutex, &queue_context ); - _POSIX_Mutex_Acquire_critical( the_mutex, &queue_context ); - - if ( the_mutex->protocol == POSIX_MUTEX_PRIORITY_CEILING ) { + if ( _POSIX_Mutex_Get_protocol( flags ) == POSIX_MUTEX_PRIORITY_CEILING ) { *prioceiling = _POSIX_Priority_From_core( - _CORE_ceiling_mutex_Get_scheduler( &the_mutex->Mutex ), - _CORE_ceiling_mutex_Get_priority( &the_mutex->Mutex ) + _POSIX_Mutex_Get_scheduler( the_mutex ), + _POSIX_Mutex_Get_priority( the_mutex ) ); } else { *prioceiling = 0; } _POSIX_Mutex_Release( the_mutex, &queue_context ); - return 0; } diff --git a/cpukit/posix/src/mutexinit.c b/cpukit/posix/src/mutexinit.c index c7161b0daf..86b3bd0e96 100644 --- a/cpukit/posix/src/mutexinit.c +++ b/cpukit/posix/src/mutexinit.c @@ -23,6 +23,46 @@ #include #include +RTEMS_STATIC_ASSERT( + offsetof( POSIX_Mutex_Control, flags ) + == offsetof( pthread_mutex_t, _flags ), + POSIX_MUTEX_CONTROL_FLAGS +); + +RTEMS_STATIC_ASSERT( + offsetof( POSIX_Mutex_Control, Recursive ) + == offsetof( pthread_mutex_t, _Recursive ), + POSIX_MUTEX_CONTROL_RECURSIVE +); + +RTEMS_STATIC_ASSERT( + offsetof( POSIX_Mutex_Control, Priority_ceiling ) + == offsetof( pthread_mutex_t, _Priority_ceiling ), + POSIX_MUTEX_CONTROL_SCHEDULER +); + +RTEMS_STATIC_ASSERT( + offsetof( POSIX_Mutex_Control, scheduler ) + == offsetof( pthread_mutex_t, _scheduler ), + POSIX_MUTEX_CONTROL_SCHEDULER +); + +RTEMS_STATIC_ASSERT( + sizeof( POSIX_Mutex_Control ) == sizeof( pthread_mutex_t ), + POSIX_MUTEX_CONTROL_SIZE +); + +const pthread_mutexattr_t _POSIX_Mutex_Default_attributes = { +#if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES) + .type = PTHREAD_MUTEX_DEFAULT, +#endif + .is_initialized = true, + .process_shared = PTHREAD_PROCESS_PRIVATE, + .prio_ceiling = INT_MAX, + .protocol = PTHREAD_PRIO_NONE, + .recursive = false +}; + /** * 11.3.2 Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87 * @@ -38,6 +78,8 @@ int pthread_mutex_init( POSIX_Mutex_Control *the_mutex; const pthread_mutexattr_t *the_attr; POSIX_Mutex_Protocol protocol; + unsigned long flags; + Priority_Control priority; const Scheduler_Control *scheduler; if ( attr ) the_attr = attr; @@ -101,20 +143,21 @@ int pthread_mutex_init( } #endif - the_mutex = _POSIX_Mutex_Allocate(); + the_mutex = _POSIX_Mutex_Get( mutex ); - if ( !the_mutex ) { - _Objects_Allocator_unlock(); - return EAGAIN; + flags = (uintptr_t) the_mutex ^ POSIX_MUTEX_MAGIC; + flags &= ~POSIX_MUTEX_FLAGS_MASK; + flags |= protocol; + + if ( the_attr->type == PTHREAD_MUTEX_RECURSIVE ) { + flags |= POSIX_MUTEX_RECURSIVE; } - the_mutex->protocol = protocol; - the_mutex->is_recursive = ( the_attr->type == PTHREAD_MUTEX_RECURSIVE ); + the_mutex->flags = flags; if ( protocol == POSIX_MUTEX_PRIORITY_CEILING ) { - int prio_ceiling; - bool valid; - Priority_Control priority; + int prio_ceiling; + bool valid; scheduler = _Thread_Scheduler_get_home( _Thread_Get_executing() ); prio_ceiling = the_attr->prio_ceiling; @@ -125,23 +168,19 @@ int pthread_mutex_init( priority = _POSIX_Priority_To_core( scheduler, prio_ceiling, &valid ); if ( !valid ) { - _POSIX_Mutex_Free(the_mutex); - _Objects_Allocator_unlock(); return EINVAL; } - _CORE_ceiling_mutex_Initialize( &the_mutex->Mutex, scheduler, priority ); } else { - _Assert( - the_mutex->protocol == POSIX_MUTEX_NO_PROTOCOL - || the_mutex->protocol == POSIX_MUTEX_PRIORITY_INHERIT - ); - _CORE_recursive_mutex_Initialize( &the_mutex->Mutex.Recursive ); + priority = 0; + scheduler = NULL; } - _Objects_Open_u32( &_POSIX_Mutex_Information, &the_mutex->Object, 0 ); - - *mutex = the_mutex->Object.id; - - _Objects_Allocator_unlock(); + _Thread_queue_Queue_initialize( + &the_mutex->Recursive.Mutex.Queue.Queue, + NULL + ); + the_mutex->Recursive.nest_level = 0; + _Priority_Node_initialize( &the_mutex->Priority_ceiling, priority ); + the_mutex->scheduler = scheduler; return 0; } diff --git a/cpukit/posix/src/mutexlocksupp.c b/cpukit/posix/src/mutexlocksupp.c index ea4c4e3eab..507d667164 100644 --- a/cpukit/posix/src/mutexlocksupp.c +++ b/cpukit/posix/src/mutexlocksupp.c @@ -21,27 +21,34 @@ #include #include -THREAD_QUEUE_OBJECT_ASSERT( - POSIX_Mutex_Control, - Mutex.Recursive.Mutex.Wait_queue -); - -static Status_Control _POSIX_Mutex_Lock_nested( - CORE_recursive_mutex_Control *the_recursive_mutex +Status_Control _POSIX_Mutex_Seize_slow( + POSIX_Mutex_Control *the_mutex, + const Thread_queue_Operations *operations, + Thread_Control *executing, + bool wait, + Thread_queue_Context *queue_context ) { - POSIX_Mutex_Control *the_mutex; - - the_mutex = RTEMS_CONTAINER_OF( - the_recursive_mutex, - POSIX_Mutex_Control, - Mutex.Recursive - ); - - if ( the_mutex->is_recursive ) { - return _CORE_recursive_mutex_Seize_nested( the_recursive_mutex ); + if ( wait ) { + _Thread_queue_Context_set_thread_state( + queue_context, + STATES_WAITING_FOR_MUTEX + ); + _Thread_queue_Context_set_do_nothing_enqueue_callout( queue_context ); + _Thread_queue_Context_set_deadlock_callout( + queue_context, + _Thread_queue_Deadlock_status + ); + _Thread_queue_Enqueue( + &the_mutex->Recursive.Mutex.Queue.Queue, + operations, + executing, + queue_context + ); + return _Thread_Wait_get_status( executing ); } else { - return STATUS_NESTING_NOT_ALLOWED; + _POSIX_Mutex_Release( the_mutex, queue_context ); + return STATUS_UNAVAILABLE; } } @@ -52,47 +59,47 @@ int _POSIX_Mutex_Lock_support( ) { POSIX_Mutex_Control *the_mutex; + unsigned long flags; Thread_queue_Context queue_context; Thread_Control *executing; Status_Control status; - the_mutex = _POSIX_Mutex_Get( mutex, &queue_context ); + the_mutex = _POSIX_Mutex_Get( mutex ); + POSIX_MUTEX_VALIDATE_OBJECT( the_mutex, flags ); - if ( the_mutex == NULL ) { - return EINVAL; - } - - executing = _Thread_Executing; + executing = _POSIX_Mutex_Acquire( the_mutex, &queue_context ); _Thread_queue_Context_set_relative_timeout( &queue_context, timeout ); - switch ( the_mutex->protocol ) { + switch ( _POSIX_Mutex_Get_protocol( flags ) ) { case POSIX_MUTEX_PRIORITY_CEILING: - status = _CORE_ceiling_mutex_Seize( - &the_mutex->Mutex, + status = _POSIX_Mutex_Ceiling_seize( + the_mutex, + flags, executing, wait, - _POSIX_Mutex_Lock_nested, &queue_context ); break; case POSIX_MUTEX_NO_PROTOCOL: - status = _CORE_recursive_mutex_Seize( - &the_mutex->Mutex.Recursive, + status = _POSIX_Mutex_Seize( + the_mutex, + flags, POSIX_MUTEX_NO_PROTOCOL_TQ_OPERATIONS, executing, wait, - _POSIX_Mutex_Lock_nested, &queue_context ); break; default: - _Assert( the_mutex->protocol == POSIX_MUTEX_PRIORITY_INHERIT ); - status = _CORE_recursive_mutex_Seize( - &the_mutex->Mutex.Recursive, - CORE_MUTEX_TQ_PRIORITY_INHERIT_OPERATIONS, + _Assert( + _POSIX_Mutex_Get_protocol( flags ) == POSIX_MUTEX_PRIORITY_INHERIT + ); + status = _POSIX_Mutex_Seize( + the_mutex, + flags, + POSIX_MUTEX_PRIORITY_INHERIT_TQ_OPERATIONS, executing, wait, - _POSIX_Mutex_Lock_nested, &queue_context ); break; diff --git a/cpukit/posix/src/mutexsetprioceiling.c b/cpukit/posix/src/mutexsetprioceiling.c index 9288ffd828..87d9ba46f3 100644 --- a/cpukit/posix/src/mutexsetprioceiling.c +++ b/cpukit/posix/src/mutexsetprioceiling.c @@ -49,17 +49,19 @@ int pthread_mutex_setprioceiling( return EINVAL; } - the_mutex = _POSIX_Mutex_Get_no_protection( mutex ); - _Assert( the_mutex != NULL ); + the_mutex = _POSIX_Mutex_Get( mutex ); - if ( the_mutex->protocol == POSIX_MUTEX_PRIORITY_CEILING ) { + if ( + _POSIX_Mutex_Get_protocol( the_mutex->flags ) + == POSIX_MUTEX_PRIORITY_CEILING + ) { const Scheduler_Control *scheduler; bool valid; Priority_Control new_priority; Priority_Control old_priority; - scheduler = _CORE_ceiling_mutex_Get_scheduler( &the_mutex->Mutex ); - old_priority = _CORE_ceiling_mutex_Get_priority( &the_mutex->Mutex ); + scheduler = _POSIX_Mutex_Get_scheduler( the_mutex ); + old_priority = _POSIX_Mutex_Get_priority( the_mutex ); *old_ceiling = _POSIX_Priority_From_core( scheduler, old_priority ); new_priority = _POSIX_Priority_To_core( scheduler, prioceiling, &valid ); @@ -69,8 +71,8 @@ int pthread_mutex_setprioceiling( _Thread_queue_Context_initialize( &queue_context ); _Thread_queue_Context_clear_priority_updates( &queue_context ); - _CORE_ceiling_mutex_Set_priority( - &the_mutex->Mutex, + _POSIX_Mutex_Set_priority( + the_mutex, new_priority, &queue_context ); diff --git a/cpukit/posix/src/mutexunlock.c b/cpukit/posix/src/mutexunlock.c index c15f7e6ad2..ead3109812 100644 --- a/cpukit/posix/src/mutexunlock.c +++ b/cpukit/posix/src/mutexunlock.c @@ -21,6 +21,51 @@ #include #include +#include + +bool _POSIX_Mutex_Auto_initialization( POSIX_Mutex_Control *the_mutex ) +{ + unsigned long zero; + unsigned long flags; + + /* We cannot use memset() and memcmp() due to structure internal padding */ + zero = 0; + zero |= the_mutex->flags; +#if defined(RTEMS_SMP) + zero |= _Atomic_Load_uint( + &the_mutex->Recursive.Mutex.Queue.Queue.Lock.next_ticket, + ATOMIC_ORDER_RELAXED + ); + zero |= _Atomic_Load_uint( + &the_mutex->Recursive.Mutex.Queue.Queue.Lock.now_serving, + ATOMIC_ORDER_RELAXED + ); +#else + zero |= the_mutex->Recursive.Mutex.Queue.reserved[ 0 ]; + zero |= the_mutex->Recursive.Mutex.Queue.reserved[ 1 ]; +#endif + zero |= (unsigned long) the_mutex->Recursive.Mutex.Queue.Queue.heads; + zero |= (unsigned long) the_mutex->Recursive.Mutex.Queue.Queue.owner; + zero |= (unsigned long) the_mutex->Recursive.Mutex.Queue.Queue.name; + zero |= the_mutex->Recursive.nest_level; + zero |= (unsigned long) the_mutex->Priority_ceiling.Node.RBTree.Node.rbe_left; + zero |= (unsigned long) the_mutex->Priority_ceiling.Node.RBTree.Node.rbe_right; + zero |= (unsigned long) the_mutex->Priority_ceiling.Node.RBTree.Node.rbe_parent; + zero |= (unsigned long) the_mutex->Priority_ceiling.Node.RBTree.Node.rbe_color; + zero |= (unsigned long) the_mutex->Priority_ceiling.priority; + zero |= (unsigned long) (the_mutex->Priority_ceiling.priority >> 32); + zero |= (unsigned long) the_mutex->scheduler; + + if ( zero != 0 ) { + return false; + } + + flags = (uintptr_t) the_mutex ^ POSIX_MUTEX_MAGIC; + flags &= ~POSIX_MUTEX_FLAGS_MASK; + the_mutex->flags = flags; + return true; +} + /* * 11.3.3 Locking and Unlocking a Mutex, P1003.1c/Draft 10, p. 93 * @@ -32,39 +77,39 @@ int pthread_mutex_unlock( ) { POSIX_Mutex_Control *the_mutex; + unsigned long flags; Thread_queue_Context queue_context; Thread_Control *executing; Status_Control status; - the_mutex = _POSIX_Mutex_Get( mutex, &queue_context ); + the_mutex = _POSIX_Mutex_Get( mutex ); + POSIX_MUTEX_VALIDATE_OBJECT( the_mutex, flags ); - if ( the_mutex == NULL ) { - return EINVAL; - } - - executing = _Thread_Executing; + executing = _POSIX_Mutex_Acquire( the_mutex, &queue_context ); - switch ( the_mutex->protocol ) { + switch ( _POSIX_Mutex_Get_protocol( flags ) ) { case POSIX_MUTEX_PRIORITY_CEILING: - status = _CORE_ceiling_mutex_Surrender( - &the_mutex->Mutex, + status = _POSIX_Mutex_Ceiling_surrender( + the_mutex, executing, &queue_context ); break; case POSIX_MUTEX_NO_PROTOCOL: - status = _CORE_recursive_mutex_Surrender( - &the_mutex->Mutex.Recursive, + status = _POSIX_Mutex_Surrender( + the_mutex, POSIX_MUTEX_NO_PROTOCOL_TQ_OPERATIONS, executing, &queue_context ); break; default: - _Assert( the_mutex->protocol == POSIX_MUTEX_PRIORITY_INHERIT ); - status = _CORE_recursive_mutex_Surrender( - &the_mutex->Mutex.Recursive, - CORE_MUTEX_TQ_PRIORITY_INHERIT_OPERATIONS, + _Assert( + _POSIX_Mutex_Get_protocol( flags ) == POSIX_MUTEX_PRIORITY_INHERIT + ); + status = _POSIX_Mutex_Surrender( + the_mutex, + POSIX_MUTEX_PRIORITY_INHERIT_TQ_OPERATIONS, executing, &queue_context ); -- cgit v1.2.3