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 | * |
---|
18 | * COPYRIGHT (c) 1989-1999. |
---|
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 |
---|
23 | * http://www.OARcorp.com/rtems/license.html. |
---|
24 | * |
---|
25 | * $Id$ |
---|
26 | */ |
---|
27 | |
---|
28 | #include <rtems/system.h> |
---|
29 | #include <rtems/rtems/status.h> |
---|
30 | #include <rtems/rtems/support.h> |
---|
31 | #include <rtems/rtems/attr.h> |
---|
32 | #include <rtems/score/isr.h> |
---|
33 | #include <rtems/score/object.h> |
---|
34 | #include <rtems/rtems/options.h> |
---|
35 | #include <rtems/rtems/sem.h> |
---|
36 | #include <rtems/score/coremutex.h> |
---|
37 | #include <rtems/score/coresem.h> |
---|
38 | #include <rtems/score/states.h> |
---|
39 | #include <rtems/score/thread.h> |
---|
40 | #include <rtems/score/threadq.h> |
---|
41 | #if defined(RTEMS_MULTIPROCESSING) |
---|
42 | #include <rtems/score/mpci.h> |
---|
43 | #endif |
---|
44 | #include <rtems/score/sysstate.h> |
---|
45 | |
---|
46 | #include <rtems/score/interr.h> |
---|
47 | |
---|
48 | /*PAGE |
---|
49 | * |
---|
50 | * rtems_semaphore_create |
---|
51 | * |
---|
52 | * This directive creates a semaphore and sets the initial value based |
---|
53 | * on the given count. A semaphore id is returned. |
---|
54 | * |
---|
55 | * Input parameters: |
---|
56 | * name - user defined semaphore name |
---|
57 | * count - initial count of semaphore |
---|
58 | * attribute_set - semaphore attributes |
---|
59 | * priority_ceiling - semaphore's ceiling priority |
---|
60 | * id - pointer to semaphore id |
---|
61 | * |
---|
62 | * Output parameters: |
---|
63 | * id - semaphore id |
---|
64 | * RTEMS_SUCCESSFUL - if successful |
---|
65 | * error code - if unsuccessful |
---|
66 | */ |
---|
67 | |
---|
68 | rtems_status_code rtems_semaphore_create( |
---|
69 | rtems_name name, |
---|
70 | unsigned32 count, |
---|
71 | rtems_attribute attribute_set, |
---|
72 | rtems_task_priority priority_ceiling, |
---|
73 | Objects_Id *id |
---|
74 | ) |
---|
75 | { |
---|
76 | register Semaphore_Control *the_semaphore; |
---|
77 | CORE_mutex_Attributes the_mutex_attributes; |
---|
78 | CORE_semaphore_Attributes the_semaphore_attributes; |
---|
79 | unsigned32 lock; |
---|
80 | |
---|
81 | if ( !rtems_is_name_valid( name ) ) |
---|
82 | return RTEMS_INVALID_NAME; |
---|
83 | |
---|
84 | #if defined(RTEMS_MULTIPROCESSING) |
---|
85 | if ( _Attributes_Is_global( attribute_set ) ) { |
---|
86 | |
---|
87 | if ( !_System_state_Is_multiprocessing ) |
---|
88 | return RTEMS_MP_NOT_CONFIGURED; |
---|
89 | |
---|
90 | if ( _Attributes_Is_inherit_priority( attribute_set ) ) |
---|
91 | return RTEMS_NOT_DEFINED; |
---|
92 | |
---|
93 | } else |
---|
94 | #endif |
---|
95 | |
---|
96 | if ( _Attributes_Is_inherit_priority( attribute_set ) || |
---|
97 | _Attributes_Is_priority_ceiling( attribute_set ) ) { |
---|
98 | |
---|
99 | if ( ! ( (_Attributes_Is_binary_semaphore( attribute_set ) || |
---|
100 | _Attributes_Is_simple_binary_semaphore( attribute_set )) && |
---|
101 | |
---|
102 | _Attributes_Is_priority( attribute_set ) ) ) |
---|
103 | return RTEMS_NOT_DEFINED; |
---|
104 | |
---|
105 | } |
---|
106 | |
---|
107 | if ( !_Attributes_Is_counting_semaphore( attribute_set ) && ( count > 1 ) ) |
---|
108 | return RTEMS_INVALID_NUMBER; |
---|
109 | |
---|
110 | _Thread_Disable_dispatch(); /* prevents deletion */ |
---|
111 | |
---|
112 | the_semaphore = _Semaphore_Allocate(); |
---|
113 | |
---|
114 | if ( !the_semaphore ) { |
---|
115 | _Thread_Enable_dispatch(); |
---|
116 | return RTEMS_TOO_MANY; |
---|
117 | } |
---|
118 | |
---|
119 | #if defined(RTEMS_MULTIPROCESSING) |
---|
120 | if ( _Attributes_Is_global( attribute_set ) && |
---|
121 | ! ( _Objects_MP_Allocate_and_open( &_Semaphore_Information, name, |
---|
122 | the_semaphore->Object.id, FALSE ) ) ) { |
---|
123 | _Semaphore_Free( the_semaphore ); |
---|
124 | _Thread_Enable_dispatch(); |
---|
125 | return RTEMS_TOO_MANY; |
---|
126 | } |
---|
127 | #endif |
---|
128 | |
---|
129 | the_semaphore->attribute_set = attribute_set; |
---|
130 | |
---|
131 | /* |
---|
132 | * If it is not a counting semaphore, then it is either a |
---|
133 | * simple binary semaphore or a more powerful mutex style binary |
---|
134 | * semaphore. |
---|
135 | */ |
---|
136 | |
---|
137 | if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) { |
---|
138 | if ( _Attributes_Is_inherit_priority( attribute_set ) ) |
---|
139 | the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT; |
---|
140 | else if ( _Attributes_Is_priority_ceiling( attribute_set ) ) |
---|
141 | the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING; |
---|
142 | else if ( _Attributes_Is_priority( attribute_set ) ) |
---|
143 | the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY; |
---|
144 | else |
---|
145 | the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_FIFO; |
---|
146 | |
---|
147 | |
---|
148 | if ( _Attributes_Is_binary_semaphore( attribute_set ) ) { |
---|
149 | the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES; |
---|
150 | |
---|
151 | switch ( the_mutex_attributes.discipline ) { |
---|
152 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
153 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
154 | the_mutex_attributes.only_owner_release = FALSE; |
---|
155 | break; |
---|
156 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
157 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
158 | the_mutex_attributes.only_owner_release = TRUE; |
---|
159 | break; |
---|
160 | } |
---|
161 | } else { |
---|
162 | the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_BLOCKS; |
---|
163 | the_mutex_attributes.only_owner_release = FALSE; |
---|
164 | } |
---|
165 | |
---|
166 | the_mutex_attributes.priority_ceiling = priority_ceiling; |
---|
167 | |
---|
168 | if ( count == 1 ) |
---|
169 | lock = CORE_MUTEX_UNLOCKED; |
---|
170 | else |
---|
171 | lock = CORE_MUTEX_LOCKED; |
---|
172 | |
---|
173 | _CORE_mutex_Initialize( |
---|
174 | &the_semaphore->Core_control.mutex, |
---|
175 | OBJECTS_RTEMS_SEMAPHORES, |
---|
176 | &the_mutex_attributes, |
---|
177 | lock, |
---|
178 | #if defined(RTEMS_MULTIPROCESSING) |
---|
179 | _Semaphore_MP_Send_extract_proxy |
---|
180 | #else |
---|
181 | NULL |
---|
182 | #endif |
---|
183 | ); |
---|
184 | } else { |
---|
185 | if ( _Attributes_Is_priority( attribute_set ) ) |
---|
186 | the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_PRIORITY; |
---|
187 | else |
---|
188 | the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO; |
---|
189 | |
---|
190 | /* |
---|
191 | * This effectively disables limit checking. |
---|
192 | */ |
---|
193 | |
---|
194 | the_semaphore_attributes.maximum_count = 0xFFFFFFFF; |
---|
195 | |
---|
196 | /* |
---|
197 | * The following are just to make Purify happy. |
---|
198 | */ |
---|
199 | |
---|
200 | the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES; |
---|
201 | the_mutex_attributes.priority_ceiling = PRIORITY_MINIMUM; |
---|
202 | |
---|
203 | _CORE_semaphore_Initialize( |
---|
204 | &the_semaphore->Core_control.semaphore, |
---|
205 | OBJECTS_RTEMS_SEMAPHORES, |
---|
206 | &the_semaphore_attributes, |
---|
207 | count, |
---|
208 | #if defined(RTEMS_MULTIPROCESSING) |
---|
209 | _Semaphore_MP_Send_extract_proxy |
---|
210 | #else |
---|
211 | NULL |
---|
212 | #endif |
---|
213 | ); |
---|
214 | } |
---|
215 | |
---|
216 | _Objects_Open( &_Semaphore_Information, &the_semaphore->Object, &name ); |
---|
217 | |
---|
218 | *id = the_semaphore->Object.id; |
---|
219 | |
---|
220 | #if defined(RTEMS_MULTIPROCESSING) |
---|
221 | if ( _Attributes_Is_global( attribute_set ) ) |
---|
222 | _Semaphore_MP_Send_process_packet( |
---|
223 | SEMAPHORE_MP_ANNOUNCE_CREATE, |
---|
224 | the_semaphore->Object.id, |
---|
225 | name, |
---|
226 | 0 /* Not used */ |
---|
227 | ); |
---|
228 | #endif |
---|
229 | _Thread_Enable_dispatch(); |
---|
230 | return RTEMS_SUCCESSFUL; |
---|
231 | } |
---|