source: rtems/cpukit/rtems/src/semobtain.c @ 8d37343

4.104.114.84.95
Last change on this file since 8d37343 was 5870ac55, checked in by Joel Sherrill <joel.sherrill@…>, on 01/05/00 at 22:19:21

Added support for simple binary semaphores in addition to the high
power binary/mutex style semaphores already supported by RTEMS. This
was done at the request of Eric Norum <eric@…> in support
of his effort to port EPICS to RTEMS. This change consisted of
changing the nesting_allowed boolean into a lock_nesting_behavior
enumerated value as well as allowing the core mutex object to optionally
support ensuring that the holder of a binary semaphore released it.
Finally, a more subtle enhancement was to allow the non-holder to release
a priority inheritance/ceiling mutex and still allow the holding task
to return to its original priority.

  • Property mode set to 100644
File size: 3.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-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_obtain
51 *
52 *  This directive allows a thread to acquire a semaphore.
53 *
54 *  Input parameters:
55 *    id         - semaphore id
56 *    option_set - wait option
57 *    timeout    - number of ticks to wait (0 means wait forever)
58 *
59 *  Output parameters:
60 *    RTEMS_SUCCESSFUL - if successful
61 *    error code        - if unsuccessful
62 */
63
64rtems_status_code rtems_semaphore_obtain(
65  Objects_Id      id,
66  unsigned32      option_set,
67  rtems_interval  timeout
68)
69{
70  register Semaphore_Control *the_semaphore;
71  Objects_Locations           location;
72  boolean                     wait;
73
74  the_semaphore = _Semaphore_Get( id, &location );
75  switch ( location ) {
76    case OBJECTS_REMOTE:
77#if defined(RTEMS_MULTIPROCESSING)
78      return _Semaphore_MP_Send_request_packet(
79          SEMAPHORE_MP_OBTAIN_REQUEST,
80          id,
81          option_set,
82          timeout
83      );
84#endif
85
86    case OBJECTS_ERROR:
87      return RTEMS_INVALID_ID;
88
89    case OBJECTS_LOCAL:
90      if ( _Options_Is_no_wait( option_set ) )
91        wait = FALSE;
92      else
93        wait = TRUE;
94
95      if ( !_Attributes_Is_counting_semaphore(the_semaphore->attribute_set) ) {
96        _CORE_mutex_Seize(
97          &the_semaphore->Core_control.mutex,
98          id,
99          wait,
100          timeout
101        );
102        _Thread_Enable_dispatch();
103        return _Semaphore_Translate_core_mutex_return_code(
104                  _Thread_Executing->Wait.return_code );
105      } else {
106        _CORE_semaphore_Seize(
107          &the_semaphore->Core_control.semaphore,
108          id,
109          wait,
110          timeout
111        );
112        _Thread_Enable_dispatch();
113        return _Semaphore_Translate_core_semaphore_return_code(
114                  _Thread_Executing->Wait.return_code );
115      }
116  }
117
118  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
119}
Note: See TracBrowser for help on using the repository browser.