[c06d8f64] | 1 | /* |
---|
| 2 | * Semaphore Manager |
---|
| 3 | * |
---|
| 4 | * DESCRIPTION: |
---|
| 5 | * |
---|
| 6 | * This package is the implementation of the Semaphore Manager. |
---|
| 7 | * This manager utilizes standard Dijkstra counting semaphores to provide |
---|
| 8 | * synchronization and mutual exclusion capabilities. |
---|
| 9 | * |
---|
| 10 | * Directives provided are: |
---|
| 11 | * |
---|
| 12 | * + create a semaphore |
---|
| 13 | * + get an ID of a semaphore |
---|
| 14 | * + delete a semaphore |
---|
| 15 | * + acquire a semaphore |
---|
| 16 | * + release a semaphore |
---|
| 17 | * |
---|
[08311cc3] | 18 | * COPYRIGHT (c) 1989-1999. |
---|
[c06d8f64] | 19 | * On-Line Applications Research Corporation (OAR). |
---|
| 20 | * |
---|
| 21 | * The license and distribution terms for this file may be |
---|
| 22 | * found in the file LICENSE in this distribution or at |
---|
[277cc95] | 23 | * http://www.rtems.com/license/LICENSE. |
---|
[c06d8f64] | 24 | * |
---|
| 25 | * $Id$ |
---|
| 26 | */ |
---|
| 27 | |
---|
[1095ec1] | 28 | #if HAVE_CONFIG_H |
---|
| 29 | #include "config.h" |
---|
| 30 | #endif |
---|
| 31 | |
---|
[c06d8f64] | 32 | #include <rtems/system.h> |
---|
| 33 | #include <rtems/rtems/status.h> |
---|
| 34 | #include <rtems/rtems/support.h> |
---|
| 35 | #include <rtems/rtems/attr.h> |
---|
| 36 | #include <rtems/score/isr.h> |
---|
| 37 | #include <rtems/score/object.h> |
---|
| 38 | #include <rtems/rtems/options.h> |
---|
| 39 | #include <rtems/rtems/sem.h> |
---|
| 40 | #include <rtems/score/coremutex.h> |
---|
| 41 | #include <rtems/score/coresem.h> |
---|
| 42 | #include <rtems/score/states.h> |
---|
| 43 | #include <rtems/score/thread.h> |
---|
| 44 | #include <rtems/score/threadq.h> |
---|
| 45 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 46 | #include <rtems/score/mpci.h> |
---|
| 47 | #endif |
---|
| 48 | #include <rtems/score/sysstate.h> |
---|
| 49 | |
---|
| 50 | #include <rtems/score/interr.h> |
---|
| 51 | |
---|
| 52 | /*PAGE |
---|
| 53 | * |
---|
| 54 | * rtems_semaphore_create |
---|
| 55 | * |
---|
| 56 | * This directive creates a semaphore and sets the initial value based |
---|
| 57 | * on the given count. A semaphore id is returned. |
---|
| 58 | * |
---|
| 59 | * Input parameters: |
---|
| 60 | * name - user defined semaphore name |
---|
| 61 | * count - initial count of semaphore |
---|
| 62 | * attribute_set - semaphore attributes |
---|
[50f32b11] | 63 | * priority_ceiling - semaphore's ceiling priority |
---|
[c06d8f64] | 64 | * id - pointer to semaphore id |
---|
| 65 | * |
---|
| 66 | * Output parameters: |
---|
| 67 | * id - semaphore id |
---|
| 68 | * RTEMS_SUCCESSFUL - if successful |
---|
| 69 | * error code - if unsuccessful |
---|
| 70 | */ |
---|
| 71 | |
---|
| 72 | rtems_status_code rtems_semaphore_create( |
---|
| 73 | rtems_name name, |
---|
[1d496f6] | 74 | uint32_t count, |
---|
[c06d8f64] | 75 | rtems_attribute attribute_set, |
---|
| 76 | rtems_task_priority priority_ceiling, |
---|
[e7541849] | 77 | rtems_id *id |
---|
[c06d8f64] | 78 | ) |
---|
| 79 | { |
---|
| 80 | register Semaphore_Control *the_semaphore; |
---|
| 81 | CORE_mutex_Attributes the_mutex_attributes; |
---|
| 82 | CORE_semaphore_Attributes the_semaphore_attributes; |
---|
[1d496f6] | 83 | uint32_t lock; |
---|
[c06d8f64] | 84 | |
---|
| 85 | if ( !rtems_is_name_valid( name ) ) |
---|
| 86 | return RTEMS_INVALID_NAME; |
---|
| 87 | |
---|
[e980b219] | 88 | if ( !id ) |
---|
| 89 | return RTEMS_INVALID_ADDRESS; |
---|
| 90 | |
---|
[c06d8f64] | 91 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 92 | if ( _Attributes_Is_global( attribute_set ) ) { |
---|
| 93 | |
---|
| 94 | if ( !_System_state_Is_multiprocessing ) |
---|
| 95 | return RTEMS_MP_NOT_CONFIGURED; |
---|
| 96 | |
---|
| 97 | if ( _Attributes_Is_inherit_priority( attribute_set ) ) |
---|
| 98 | return RTEMS_NOT_DEFINED; |
---|
| 99 | |
---|
[50f32b11] | 100 | } else |
---|
[c06d8f64] | 101 | #endif |
---|
[d80188eb] | 102 | |
---|
[50f32b11] | 103 | if ( _Attributes_Is_inherit_priority( attribute_set ) || |
---|
[c06d8f64] | 104 | _Attributes_Is_priority_ceiling( attribute_set ) ) { |
---|
| 105 | |
---|
[5870ac55] | 106 | if ( ! ( (_Attributes_Is_binary_semaphore( attribute_set ) || |
---|
| 107 | _Attributes_Is_simple_binary_semaphore( attribute_set )) && |
---|
[50f32b11] | 108 | |
---|
[c06d8f64] | 109 | _Attributes_Is_priority( attribute_set ) ) ) |
---|
| 110 | return RTEMS_NOT_DEFINED; |
---|
| 111 | |
---|
| 112 | } |
---|
| 113 | |
---|
[5870ac55] | 114 | if ( !_Attributes_Is_counting_semaphore( attribute_set ) && ( count > 1 ) ) |
---|
[c06d8f64] | 115 | return RTEMS_INVALID_NUMBER; |
---|
| 116 | |
---|
| 117 | _Thread_Disable_dispatch(); /* prevents deletion */ |
---|
| 118 | |
---|
| 119 | the_semaphore = _Semaphore_Allocate(); |
---|
| 120 | |
---|
| 121 | if ( !the_semaphore ) { |
---|
| 122 | _Thread_Enable_dispatch(); |
---|
| 123 | return RTEMS_TOO_MANY; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 127 | if ( _Attributes_Is_global( attribute_set ) && |
---|
| 128 | ! ( _Objects_MP_Allocate_and_open( &_Semaphore_Information, name, |
---|
[eaef4657] | 129 | the_semaphore->Object.id, false ) ) ) { |
---|
[c06d8f64] | 130 | _Semaphore_Free( the_semaphore ); |
---|
| 131 | _Thread_Enable_dispatch(); |
---|
| 132 | return RTEMS_TOO_MANY; |
---|
| 133 | } |
---|
| 134 | #endif |
---|
| 135 | |
---|
| 136 | the_semaphore->attribute_set = attribute_set; |
---|
| 137 | |
---|
[5870ac55] | 138 | /* |
---|
[50f32b11] | 139 | * If it is not a counting semaphore, then it is either a |
---|
[1382cea3] | 140 | * simple binary semaphore or a more powerful mutex style binary |
---|
[5870ac55] | 141 | * semaphore. |
---|
| 142 | */ |
---|
| 143 | |
---|
| 144 | if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) { |
---|
[c06d8f64] | 145 | if ( _Attributes_Is_inherit_priority( attribute_set ) ) |
---|
| 146 | the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT; |
---|
[7d91d72] | 147 | else if ( _Attributes_Is_priority_ceiling( attribute_set ) ) |
---|
[c06d8f64] | 148 | the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING; |
---|
[7d91d72] | 149 | else if ( _Attributes_Is_priority( attribute_set ) ) |
---|
[c06d8f64] | 150 | the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY; |
---|
| 151 | else |
---|
| 152 | the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_FIFO; |
---|
| 153 | |
---|
| 154 | |
---|
[5870ac55] | 155 | if ( _Attributes_Is_binary_semaphore( attribute_set ) ) { |
---|
| 156 | the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES; |
---|
| 157 | |
---|
| 158 | switch ( the_mutex_attributes.discipline ) { |
---|
| 159 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
| 160 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
[eaef4657] | 161 | the_mutex_attributes.only_owner_release = false; |
---|
[5870ac55] | 162 | break; |
---|
| 163 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
| 164 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
[eaef4657] | 165 | the_mutex_attributes.only_owner_release = true; |
---|
[5870ac55] | 166 | break; |
---|
| 167 | } |
---|
| 168 | } else { |
---|
[b452a40] | 169 | the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_BLOCKS; |
---|
[eaef4657] | 170 | the_mutex_attributes.only_owner_release = false; |
---|
[5870ac55] | 171 | } |
---|
[c06d8f64] | 172 | |
---|
| 173 | the_mutex_attributes.priority_ceiling = priority_ceiling; |
---|
| 174 | |
---|
| 175 | if ( count == 1 ) |
---|
| 176 | lock = CORE_MUTEX_UNLOCKED; |
---|
| 177 | else |
---|
| 178 | lock = CORE_MUTEX_LOCKED; |
---|
| 179 | |
---|
| 180 | _CORE_mutex_Initialize( |
---|
| 181 | &the_semaphore->Core_control.mutex, |
---|
| 182 | &the_mutex_attributes, |
---|
[90015e7f] | 183 | lock |
---|
[c06d8f64] | 184 | ); |
---|
[5870ac55] | 185 | } else { |
---|
[c06d8f64] | 186 | if ( _Attributes_Is_priority( attribute_set ) ) |
---|
| 187 | the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_PRIORITY; |
---|
| 188 | else |
---|
| 189 | the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO; |
---|
| 190 | |
---|
[d80188eb] | 191 | /* |
---|
| 192 | * This effectively disables limit checking. |
---|
| 193 | */ |
---|
| 194 | |
---|
| 195 | the_semaphore_attributes.maximum_count = 0xFFFFFFFF; |
---|
| 196 | |
---|
[c06d8f64] | 197 | /* |
---|
| 198 | * The following are just to make Purify happy. |
---|
| 199 | */ |
---|
| 200 | |
---|
[5870ac55] | 201 | the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES; |
---|
[c06d8f64] | 202 | the_mutex_attributes.priority_ceiling = PRIORITY_MINIMUM; |
---|
| 203 | |
---|
| 204 | _CORE_semaphore_Initialize( |
---|
| 205 | &the_semaphore->Core_control.semaphore, |
---|
| 206 | &the_semaphore_attributes, |
---|
[90015e7f] | 207 | count |
---|
[c06d8f64] | 208 | ); |
---|
| 209 | } |
---|
| 210 | |
---|
[6312db3] | 211 | _Objects_Open( |
---|
| 212 | &_Semaphore_Information, |
---|
| 213 | &the_semaphore->Object, |
---|
| 214 | (Objects_Name) name |
---|
| 215 | ); |
---|
[c06d8f64] | 216 | |
---|
| 217 | *id = the_semaphore->Object.id; |
---|
| 218 | |
---|
| 219 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 220 | if ( _Attributes_Is_global( attribute_set ) ) |
---|
| 221 | _Semaphore_MP_Send_process_packet( |
---|
| 222 | SEMAPHORE_MP_ANNOUNCE_CREATE, |
---|
| 223 | the_semaphore->Object.id, |
---|
| 224 | name, |
---|
| 225 | 0 /* Not used */ |
---|
| 226 | ); |
---|
| 227 | #endif |
---|
| 228 | _Thread_Enable_dispatch(); |
---|
| 229 | return RTEMS_SUCCESSFUL; |
---|
| 230 | } |
---|