[d86308b] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @brief Initialize a Mutex |
---|
[5cb175bb] | 5 | * @ingroup POSIXAPI |
---|
[d86308b] | 6 | */ |
---|
| 7 | |
---|
[96c041c] | 8 | /* |
---|
[81551a2] | 9 | * COPYRIGHT (c) 1989-2009. |
---|
[fbfb5926] | 10 | * On-Line Applications Research Corporation (OAR). |
---|
| 11 | * |
---|
| 12 | * The license and distribution terms for this file may be |
---|
| 13 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 14 | * http://www.rtems.org/license/LICENSE. |
---|
[96c041c] | 15 | */ |
---|
| 16 | |
---|
[f42b726] | 17 | #if HAVE_CONFIG_H |
---|
| 18 | #include "config.h" |
---|
| 19 | #endif |
---|
| 20 | |
---|
[f9d533a5] | 21 | #include <rtems/posix/muteximpl.h> |
---|
[97552c98] | 22 | #include <rtems/posix/priorityimpl.h> |
---|
[5a32c48] | 23 | #include <rtems/score/schedulerimpl.h> |
---|
[96c041c] | 24 | |
---|
[d86308b] | 25 | /** |
---|
| 26 | * 11.3.2 Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87 |
---|
[96c041c] | 27 | * |
---|
[d86308b] | 28 | * NOTE: XXX Could be optimized so all the attribute error checking |
---|
| 29 | * is not performed when attr is NULL. |
---|
[96c041c] | 30 | */ |
---|
| 31 | |
---|
| 32 | int pthread_mutex_init( |
---|
| 33 | pthread_mutex_t *mutex, |
---|
| 34 | const pthread_mutexattr_t *attr |
---|
| 35 | ) |
---|
| 36 | { |
---|
[5a598ac] | 37 | POSIX_Mutex_Control *the_mutex; |
---|
| 38 | const pthread_mutexattr_t *the_attr; |
---|
| 39 | POSIX_Mutex_Protocol protocol; |
---|
[2df7fcf] | 40 | const Scheduler_Control *scheduler; |
---|
[6601684f] | 41 | |
---|
[96c041c] | 42 | if ( attr ) the_attr = attr; |
---|
| 43 | else the_attr = &_POSIX_Mutex_Default_attributes; |
---|
| 44 | |
---|
| 45 | /* Check for NULL mutex */ |
---|
| 46 | if ( !mutex ) |
---|
| 47 | return EINVAL; |
---|
| 48 | |
---|
| 49 | /* |
---|
[28ba7e2] | 50 | * The POSIX specification says: |
---|
[96c041c] | 51 | * |
---|
| 52 | * "Attempting to initialize an already initialized mutex results |
---|
| 53 | * in undefined behavior." |
---|
| 54 | * |
---|
| 55 | * Trying to keep the caller from doing the create when *mutex |
---|
| 56 | * is actually a valid ID causes grief. All it takes is the wrong |
---|
[28ba7e2] | 57 | * value in an uninitialized variable to make this fail. |
---|
[96c041c] | 58 | * |
---|
[28ba7e2] | 59 | * Thus, we do not look at *mutex. |
---|
[96c041c] | 60 | */ |
---|
[874297f3] | 61 | |
---|
| 62 | if ( !the_attr->is_initialized ) |
---|
[96c041c] | 63 | return EINVAL; |
---|
| 64 | |
---|
| 65 | /* |
---|
[290d2b79] | 66 | * We only support process private mutexes. |
---|
[96c041c] | 67 | */ |
---|
| 68 | if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED ) |
---|
[b8596d8] | 69 | return ENOSYS; |
---|
[874297f3] | 70 | |
---|
[290d2b79] | 71 | if ( the_attr->process_shared != PTHREAD_PROCESS_PRIVATE ) |
---|
| 72 | return EINVAL; |
---|
| 73 | |
---|
[96c041c] | 74 | /* |
---|
| 75 | * Determine the discipline of the mutex |
---|
| 76 | */ |
---|
| 77 | switch ( the_attr->protocol ) { |
---|
| 78 | case PTHREAD_PRIO_NONE: |
---|
[5a598ac] | 79 | protocol = POSIX_MUTEX_NO_PROTOCOL; |
---|
[96c041c] | 80 | break; |
---|
| 81 | case PTHREAD_PRIO_INHERIT: |
---|
[5a598ac] | 82 | protocol = POSIX_MUTEX_PRIORITY_INHERIT; |
---|
[96c041c] | 83 | break; |
---|
| 84 | case PTHREAD_PRIO_PROTECT: |
---|
[5a598ac] | 85 | protocol = POSIX_MUTEX_PRIORITY_CEILING; |
---|
[96c041c] | 86 | break; |
---|
| 87 | default: |
---|
| 88 | return EINVAL; |
---|
| 89 | } |
---|
| 90 | |
---|
[8cdf733] | 91 | #if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES) |
---|
| 92 | /* |
---|
| 93 | * Validate the mutex type and set appropriate SuperCore mutex |
---|
| 94 | * attributes. |
---|
| 95 | */ |
---|
| 96 | switch ( the_attr->type ) { |
---|
| 97 | case PTHREAD_MUTEX_NORMAL: |
---|
| 98 | case PTHREAD_MUTEX_RECURSIVE: |
---|
| 99 | case PTHREAD_MUTEX_ERRORCHECK: |
---|
| 100 | case PTHREAD_MUTEX_DEFAULT: |
---|
| 101 | break; |
---|
| 102 | |
---|
| 103 | default: |
---|
| 104 | return EINVAL; |
---|
| 105 | } |
---|
| 106 | #endif |
---|
| 107 | |
---|
[96c041c] | 108 | the_mutex = _POSIX_Mutex_Allocate(); |
---|
[874297f3] | 109 | |
---|
[96c041c] | 110 | if ( !the_mutex ) { |
---|
[23fec9f0] | 111 | _Objects_Allocator_unlock(); |
---|
[96c041c] | 112 | return EAGAIN; |
---|
| 113 | } |
---|
| 114 | |
---|
[5a598ac] | 115 | the_mutex->protocol = protocol; |
---|
| 116 | the_mutex->is_recursive = ( the_attr->type == PTHREAD_MUTEX_RECURSIVE ); |
---|
| 117 | |
---|
[12f9c5b7] | 118 | if ( protocol == POSIX_MUTEX_PRIORITY_CEILING ) { |
---|
| 119 | int prio_ceiling; |
---|
| 120 | bool valid; |
---|
| 121 | Priority_Control priority; |
---|
| 122 | |
---|
| 123 | scheduler = _Thread_Scheduler_get_home( _Thread_Get_executing() ); |
---|
| 124 | prio_ceiling = the_attr->prio_ceiling; |
---|
| 125 | |
---|
| 126 | if ( prio_ceiling == INT_MAX ) { |
---|
| 127 | prio_ceiling = _POSIX_Priority_Get_maximum( scheduler ); |
---|
[c16d45a5] | 128 | } |
---|
[12f9c5b7] | 129 | |
---|
| 130 | priority = _POSIX_Priority_To_core( scheduler, prio_ceiling, &valid ); |
---|
| 131 | if ( !valid ) { |
---|
| 132 | _POSIX_Mutex_Free(the_mutex); |
---|
| 133 | _Objects_Allocator_unlock(); |
---|
| 134 | return EINVAL; |
---|
| 135 | } |
---|
| 136 | _CORE_ceiling_mutex_Initialize( &the_mutex->Mutex, scheduler, priority ); |
---|
| 137 | } else { |
---|
| 138 | _Assert( |
---|
| 139 | the_mutex->protocol == POSIX_MUTEX_NO_PROTOCOL |
---|
| 140 | || the_mutex->protocol == POSIX_MUTEX_PRIORITY_INHERIT |
---|
| 141 | ); |
---|
| 142 | _CORE_recursive_mutex_Initialize( &the_mutex->Mutex.Recursive ); |
---|
[5a598ac] | 143 | } |
---|
[96c041c] | 144 | |
---|
[ce19f1fa] | 145 | _Objects_Open_u32( &_POSIX_Mutex_Information, &the_mutex->Object, 0 ); |
---|
[96c041c] | 146 | |
---|
| 147 | *mutex = the_mutex->Object.id; |
---|
| 148 | |
---|
[23fec9f0] | 149 | _Objects_Allocator_unlock(); |
---|
[96c041c] | 150 | return 0; |
---|
| 151 | } |
---|