source: rtems/cpukit/rtems/src/sem.c @ 52a0641

4.104.114.84.95
Last change on this file since 52a0641 was debe919, checked in by Joel Sherrill <joel.sherrill@…>, on 04/19/96 at 21:10:58

event.c: _Event_Manager_initialization no longer a static inline

sem.c: modified to eliminate Purify warnings

  • Property mode set to 100644
File size: 15.0 KB
Line 
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, 1990, 1991, 1992, 1993, 1994.
19 *  On-Line Applications Research Corporation (OAR).
20 *  All rights assigned to U.S. Government, 1994.
21 *
22 *  This material may be reproduced by or for the U.S. Government pursuant
23 *  to the copyright license under the clause at DFARS 252.227-7013.  This
24 *  notice must appear in all copies of this file and its derivatives.
25 *
26 *  $Id$
27 */
28
29#include <rtems/system.h>
30#include <rtems/rtems/status.h>
31#include <rtems/rtems/support.h>
32#include <rtems/rtems/attr.h>
33#include <rtems/score/isr.h>
34#include <rtems/score/object.h>
35#include <rtems/rtems/options.h>
36#include <rtems/rtems/sem.h>
37#include <rtems/score/coremutex.h>
38#include <rtems/score/coresem.h>
39#include <rtems/score/states.h>
40#include <rtems/score/thread.h>
41#include <rtems/score/threadq.h>
42#include <rtems/score/mpci.h>
43#include <rtems/score/sysstate.h>
44
45#include <rtems/score/interr.h>
46
47/*PAGE
48 *
49 *  _Semaphore_Manager_initialization
50 *
51 *  This routine initializes all semaphore manager related data structures.
52 *
53 *  Input parameters:
54 *    maximum_semaphores - maximum configured semaphores
55 *
56 *  Output parameters:  NONE
57 */
58
59void _Semaphore_Manager_initialization(
60  unsigned32 maximum_semaphores
61)
62{
63  _Objects_Initialize_information(
64    &_Semaphore_Information,
65    OBJECTS_RTEMS_SEMAPHORES,
66    TRUE,
67    maximum_semaphores,
68    sizeof( Semaphore_Control ),
69    FALSE,
70    RTEMS_MAXIMUM_NAME_LENGTH,
71    FALSE
72  );
73 
74  /*
75   *  Register the MP Process Packet routine.
76   */
77 
78  _MPCI_Register_packet_processor(
79    MP_PACKET_SEMAPHORE,
80    _Semaphore_MP_Process_packet
81  );
82
83}
84
85/*PAGE
86 *
87 *  rtems_semaphore_create
88 *
89 *  This directive creates a semaphore and sets the initial value based
90 *  on the given count.  A semaphore id is returned.
91 *
92 *  Input parameters:
93 *    name             - user defined semaphore name
94 *    count            - initial count of semaphore
95 *    attribute_set    - semaphore attributes
96 *    priority_ceiling - semaphore's ceiling priority
97 *    id               - pointer to semaphore id
98 *
99 *  Output parameters:
100 *    id       - semaphore id
101 *    RTEMS_SUCCESSFUL - if successful
102 *    error code - if unsuccessful
103 */
104
105rtems_status_code rtems_semaphore_create(
106  rtems_name           name,
107  unsigned32           count,
108  rtems_attribute      attribute_set,
109  rtems_task_priority  priority_ceiling,
110  Objects_Id          *id
111)
112{
113  register Semaphore_Control *the_semaphore;
114  CORE_mutex_Attributes       the_mutex_attributes;
115  CORE_semaphore_Attributes   the_semaphore_attributes;
116  unsigned32                  lock;
117
118  if ( !rtems_is_name_valid( name ) )
119    return RTEMS_INVALID_NAME;
120
121  if ( _Attributes_Is_global( attribute_set ) ) {
122
123    if ( !_System_state_Is_multiprocessing )
124      return RTEMS_MP_NOT_CONFIGURED;
125
126    if ( _Attributes_Is_inherit_priority( attribute_set ) )
127      return RTEMS_NOT_DEFINED;
128
129  } else if ( _Attributes_Is_inherit_priority( attribute_set ) ) {
130
131    if ( ! ( _Attributes_Is_binary_semaphore( attribute_set ) &&
132             _Attributes_Is_priority( attribute_set ) ) )
133      return RTEMS_NOT_DEFINED;
134
135  }
136
137  if ( _Attributes_Is_binary_semaphore( attribute_set ) && ( count > 1 ) )
138    return RTEMS_INVALID_NUMBER;
139
140  _Thread_Disable_dispatch();             /* prevents deletion */
141
142  the_semaphore = _Semaphore_Allocate();
143
144  if ( !the_semaphore ) {
145    _Thread_Enable_dispatch();
146    return RTEMS_TOO_MANY;
147  }
148
149  if ( _Attributes_Is_global( attribute_set ) &&
150       ! ( _Objects_MP_Allocate_and_open( &_Semaphore_Information, name,
151                            the_semaphore->Object.id, FALSE ) ) ) {
152    _Semaphore_Free( the_semaphore );
153    _Thread_Enable_dispatch();
154    return RTEMS_TOO_MANY;
155  }
156
157  the_semaphore->attribute_set = attribute_set;
158
159  if ( _Attributes_Is_binary_semaphore( attribute_set ) ) {
160    if ( _Attributes_Is_inherit_priority( attribute_set ) )
161      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
162    else if (_Attributes_Is_priority_ceiling( attribute_set ) )
163      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
164    else if (_Attributes_Is_priority( attribute_set ) )
165      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY;
166    else
167      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_FIFO;
168
169    the_mutex_attributes.allow_nesting = TRUE;
170
171    /* Add priority ceiling code here ????? */
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      OBJECTS_RTEMS_SEMAPHORES,
183      &the_mutex_attributes,
184      lock,
185      _Semaphore_MP_Send_extract_proxy
186    );
187  }
188  else {
189    if ( _Attributes_Is_priority( attribute_set ) )
190      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_PRIORITY;
191    else
192      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO;
193
194    /*
195     *  The following are just to make Purify happy.
196     */
197
198    the_mutex_attributes.allow_nesting = TRUE;
199    the_mutex_attributes.priority_ceiling = PRIORITY_MINIMUM;
200
201    _CORE_semaphore_Initialize(
202      &the_semaphore->Core_control.semaphore,
203      OBJECTS_RTEMS_SEMAPHORES,
204      &the_semaphore_attributes,
205      count,
206      _Semaphore_MP_Send_extract_proxy
207    );
208  }
209
210  _Objects_Open( &_Semaphore_Information, &the_semaphore->Object, &name );
211
212  *id = the_semaphore->Object.id;
213
214  if ( _Attributes_Is_global( attribute_set ) )
215    _Semaphore_MP_Send_process_packet(
216      SEMAPHORE_MP_ANNOUNCE_CREATE,
217      the_semaphore->Object.id,
218      name,
219      0                          /* Not used */
220    );
221  _Thread_Enable_dispatch();
222  return RTEMS_SUCCESSFUL;
223}
224
225/*PAGE
226 *
227 *  rtems_semaphore_ident
228 *
229 *  This directive returns the system ID associated with
230 *  the semaphore name.
231 *
232 *  Input parameters:
233 *    name - user defined semaphore name
234 *    node - node(s) to be searched
235 *    id   - pointer to semaphore id
236 *
237 *  Output parameters:
238 *    *id      - semaphore id
239 *    RTEMS_SUCCESSFUL - if successful
240 *    error code - if unsuccessful
241 */
242
243rtems_status_code rtems_semaphore_ident(
244  rtems_name  name,
245  unsigned32  node,
246  Objects_Id *id
247)
248{
249  Objects_Name_to_id_errors  status;
250 
251  status = _Objects_Name_to_id( &_Semaphore_Information, &name, node, id );
252 
253  return _Status_Object_name_errors_to_status[ status ];
254}
255
256/*PAGE
257 *
258 *  rtems_semaphore_delete
259 *
260 *  This directive allows a thread to delete a semaphore specified by
261 *  the semaphore id.  The semaphore is freed back to the inactive
262 *  semaphore chain.
263 *
264 *  Input parameters:
265 *    id - semaphore id
266 *
267 *  Output parameters:
268 *    RTEMS_SUCCESSFUL - if successful
269 *    error code        - if unsuccessful
270 */
271
272rtems_status_code rtems_semaphore_delete(
273  Objects_Id id
274)
275{
276  register Semaphore_Control *the_semaphore;
277  Objects_Locations           location;
278
279  the_semaphore = _Semaphore_Get( id, &location );
280  switch ( location ) {
281    case OBJECTS_ERROR:
282      return RTEMS_INVALID_ID;
283    case OBJECTS_REMOTE:
284      _Thread_Dispatch();
285      return RTEMS_ILLEGAL_ON_REMOTE_OBJECT;
286    case OBJECTS_LOCAL:
287      if ( _Attributes_Is_binary_semaphore( the_semaphore->attribute_set) ) {
288        if ( _CORE_mutex_Is_locked( &the_semaphore->Core_control.mutex ) ) {
289          _Thread_Enable_dispatch();
290          return RTEMS_RESOURCE_IN_USE;
291        }
292        else
293          _CORE_mutex_Flush(
294            &the_semaphore->Core_control.mutex,
295            _Semaphore_MP_Send_object_was_deleted,
296            CORE_MUTEX_WAS_DELETED
297          );
298      }
299      else
300        _CORE_semaphore_Flush(
301          &the_semaphore->Core_control.semaphore,
302          _Semaphore_MP_Send_object_was_deleted,
303          CORE_SEMAPHORE_WAS_DELETED
304        );
305
306      _Objects_Close( &_Semaphore_Information, &the_semaphore->Object );
307
308      _Semaphore_Free( the_semaphore );
309
310      if ( _Attributes_Is_global( the_semaphore->attribute_set ) ) {
311
312        _Objects_MP_Close( &_Semaphore_Information, the_semaphore->Object.id );
313
314        _Semaphore_MP_Send_process_packet(
315          SEMAPHORE_MP_ANNOUNCE_DELETE,
316          the_semaphore->Object.id,
317          0,                         /* Not used */
318          0                          /* Not used */
319        );
320      }
321      _Thread_Enable_dispatch();
322      return RTEMS_SUCCESSFUL;
323  }
324
325  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
326}
327
328/*PAGE
329 *
330 *  rtems_semaphore_obtain
331 *
332 *  This directive allows a thread to acquire a semaphore.
333 *
334 *  Input parameters:
335 *    id         - semaphore id
336 *    option_set - wait option
337 *    timeout    - number of ticks to wait (0 means wait forever)
338 *
339 *  Output parameters:
340 *    RTEMS_SUCCESSFUL - if successful
341 *    error code        - if unsuccessful
342 */
343
344rtems_status_code rtems_semaphore_obtain(
345  Objects_Id      id,
346  unsigned32      option_set,
347  rtems_interval  timeout
348)
349{
350  register Semaphore_Control *the_semaphore;
351  Objects_Locations           location;
352  boolean                     wait;
353
354  the_semaphore = _Semaphore_Get( id, &location );
355  switch ( location ) {
356    case OBJECTS_ERROR:
357      return RTEMS_INVALID_ID;
358    case OBJECTS_REMOTE:
359      return _Semaphore_MP_Send_request_packet(
360          SEMAPHORE_MP_OBTAIN_REQUEST,
361          id,
362          option_set,
363          timeout
364      );
365    case OBJECTS_LOCAL:
366      if ( _Options_Is_no_wait( option_set ) )
367        wait = FALSE;
368      else
369        wait = TRUE;
370
371      if ( _Attributes_Is_binary_semaphore( the_semaphore->attribute_set ) ) {
372        _CORE_mutex_Seize(
373          &the_semaphore->Core_control.mutex,
374          id,
375          wait,
376          timeout
377        );
378        _Thread_Enable_dispatch();
379        return _Semaphore_Translate_core_mutex_return_code(
380                  _Thread_Executing->Wait.return_code );
381      } else {
382        _CORE_semaphore_Seize(
383          &the_semaphore->Core_control.semaphore,
384          id,
385          wait,
386          timeout
387        );
388        _Thread_Enable_dispatch();
389        return _Semaphore_Translate_core_semaphore_return_code(
390                  _Thread_Executing->Wait.return_code );
391      }
392  }
393
394  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
395}
396
397/*PAGE
398 *
399 *  rtems_semaphore_release
400 *
401 *  This directive allows a thread to release a semaphore.
402 *
403 *  Input parameters:
404 *    id - semaphore id
405 *
406 *  Output parameters:
407 *    RTEMS_SUCCESSFUL - if successful
408 *    error code        - if unsuccessful
409 */
410
411rtems_status_code rtems_semaphore_release(
412  Objects_Id id
413)
414{
415  register Semaphore_Control *the_semaphore;
416  Objects_Locations           location;
417  CORE_mutex_Status           mutex_status;
418  CORE_semaphore_Status       semaphore_status;
419
420  the_semaphore = _Semaphore_Get( id, &location );
421  switch ( location ) {
422    case OBJECTS_ERROR:
423      return RTEMS_INVALID_ID;
424    case OBJECTS_REMOTE:
425      return _Semaphore_MP_Send_request_packet(
426        SEMAPHORE_MP_RELEASE_REQUEST,
427        id,
428        0,                               /* Not used */
429        MPCI_DEFAULT_TIMEOUT
430      );
431    case OBJECTS_LOCAL:
432      if ( _Attributes_Is_binary_semaphore( the_semaphore->attribute_set ) ) {
433        mutex_status = _CORE_mutex_Surrender(
434                         &the_semaphore->Core_control.mutex,
435                         id,
436                         _Semaphore_Core_mutex_mp_support
437                       );
438        _Thread_Enable_dispatch();
439        return _Semaphore_Translate_core_mutex_return_code( mutex_status );
440      }
441      else
442        semaphore_status = _CORE_semaphore_Surrender(
443                             &the_semaphore->Core_control.semaphore,
444                             id,
445                             _Semaphore_Core_semaphore_mp_support
446                           );
447        _Thread_Enable_dispatch();
448        return
449          _Semaphore_Translate_core_semaphore_return_code( semaphore_status );
450  }
451
452  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
453}
454
455/*PAGE
456 *
457 *  _Semaphore_Translate_core_mutex_return_code
458 *
459 *  Input parameters:
460 *    the_mutex_status - mutex status code to translate
461 *
462 *  Output parameters:
463 *    rtems status code - translated RTEMS status code
464 *
465 */
466 
467rtems_status_code _Semaphore_Translate_core_mutex_return_code (
468  unsigned32 the_mutex_status
469)
470{
471  switch ( the_mutex_status ) {
472    case  CORE_MUTEX_STATUS_SUCCESSFUL:
473      return RTEMS_SUCCESSFUL;
474    case CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT:
475      return RTEMS_UNSATISFIED;
476    case CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED:
477      return RTEMS_INTERNAL_ERROR;
478    case CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE:
479      return RTEMS_NOT_OWNER_OF_RESOURCE;
480    case CORE_MUTEX_WAS_DELETED:
481      return RTEMS_OBJECT_WAS_DELETED;
482    case CORE_MUTEX_TIMEOUT:
483      return RTEMS_TIMEOUT;
484    case THREAD_STATUS_PROXY_BLOCKING:
485      return THREAD_STATUS_PROXY_BLOCKING;
486  }
487  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
488}
489
490/*PAGE
491 *
492 *  _Semaphore_Translate_core_semaphore_return_code
493 *
494 *  Input parameters:
495 *    the_semaphore_status - semaphore status code to translate
496 *
497 *  Output parameters:
498 *    rtems status code - translated RTEMS status code
499 *
500 */
501 
502rtems_status_code _Semaphore_Translate_core_semaphore_return_code (
503  unsigned32 the_semaphore_status
504)
505{
506  switch ( the_semaphore_status ) {
507    case  CORE_SEMAPHORE_STATUS_SUCCESSFUL:
508      return RTEMS_SUCCESSFUL;
509    case CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT:
510      return RTEMS_UNSATISFIED;
511    case CORE_SEMAPHORE_WAS_DELETED:
512      return RTEMS_OBJECT_WAS_DELETED;
513    case CORE_SEMAPHORE_TIMEOUT:
514      return RTEMS_TIMEOUT;
515    case THREAD_STATUS_PROXY_BLOCKING:
516      return THREAD_STATUS_PROXY_BLOCKING;
517  }
518  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
519}
520
521/*PAGE
522 *
523 *  _Semaphore_Core_mutex_mp_support
524 *
525 *  Input parameters:
526 *    the_thread - the remote thread the semaphore was surrendered to
527 *    id         - id of the surrendered semaphore
528 *
529 *  Output parameters: NONE
530 */
531 
532void  _Semaphore_Core_mutex_mp_support (
533  Thread_Control *the_thread,
534  Objects_Id      id
535)
536{
537  the_thread->receive_packet->return_code = RTEMS_SUCCESSFUL;
538 
539  _Semaphore_MP_Send_response_packet(
540     SEMAPHORE_MP_OBTAIN_RESPONSE,
541     id,
542     the_thread
543   );
544}
545
546
547/*PAGE
548 *
549 *  _Semaphore_Core_semaphore_mp_support
550 *
551 *  Input parameters:
552 *    the_thread - the remote thread the semaphore was surrendered to
553 *    id         - id of the surrendered semaphore
554 *
555 *  Output parameters: NONE
556 */
557 
558void  _Semaphore_Core_semaphore_mp_support (
559  Thread_Control *the_thread,
560  Objects_Id      id
561)
562{
563  the_thread->receive_packet->return_code = RTEMS_SUCCESSFUL;
564 
565  _Semaphore_MP_Send_response_packet(
566     SEMAPHORE_MP_OBTAIN_RESPONSE,
567     id,
568     the_thread
569   );
570}
Note: See TracBrowser for help on using the repository browser.