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/mutexinit.c | |
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/mutexinit.c')
-rw-r--r-- | cpukit/posix/src/mutexinit.c | 83 |
1 files changed, 61 insertions, 22 deletions
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; } |