diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2017-09-27 15:08:33 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2017-10-05 14:29:02 +0200 |
commit | de59c065c57cb8526662ee6da28a57ad16fdde66 (patch) | |
tree | 21f4a2adbd58f65f722051bca435572fbf5dcf05 /cpukit/posix/src | |
parent | posix: Implement self-contained POSIX condvar (diff) | |
download | rtems-de59c065c57cb8526662ee6da28a57ad16fdde66.tar.bz2 |
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.
Diffstat (limited to 'cpukit/posix/src')
-rw-r--r-- | cpukit/posix/src/mutex.c | 75 | ||||
-rw-r--r-- | cpukit/posix/src/mutexdestroy.c | 33 | ||||
-rw-r--r-- | cpukit/posix/src/mutexget.c | 37 | ||||
-rw-r--r-- | cpukit/posix/src/mutexgetprioceiling.c | 17 | ||||
-rw-r--r-- | cpukit/posix/src/mutexinit.c | 83 | ||||
-rw-r--r-- | cpukit/posix/src/mutexlocksupp.c | 79 | ||||
-rw-r--r-- | cpukit/posix/src/mutexsetprioceiling.c | 16 | ||||
-rw-r--r-- | cpukit/posix/src/mutexunlock.c | 75 |
8 files changed, 189 insertions, 226 deletions
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 <limits.h> - -#include <rtems/config.h> -#include <rtems/sysinit.h> -#include <rtems/posix/muteximpl.h> -#include <rtems/score/objectimpl.h> - -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 <rtems/posix/muteximpl.h> -#include <rtems/posix/posixapi.h> - -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 <rtems/posix/priorityimpl.h> #include <rtems/score/schedulerimpl.h> +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 <rtems/posix/muteximpl.h> #include <rtems/posix/posixapi.h> -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 <rtems/posix/muteximpl.h> #include <rtems/posix/posixapi.h> +#include <string.h> + +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 ); |