source: rtems/cpukit/rtems/src/semcreate.c @ 53f3084

4.104.115
Last change on this file since 53f3084 was eaef4657, checked in by Ralf Corsepius <ralf.corsepius@…>, on 01/06/09 at 05:05:03

Eliminate TRUE/FALSE.

  • Property mode set to 100644
File size: 6.4 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.rtems.com/license/LICENSE.
24 *
25 *  $Id$
26 */
27
28#if HAVE_CONFIG_H
29#include "config.h"
30#endif
31
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
63 *    priority_ceiling - semaphore's ceiling priority
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
72rtems_status_code rtems_semaphore_create(
73  rtems_name           name,
74  uint32_t             count,
75  rtems_attribute      attribute_set,
76  rtems_task_priority  priority_ceiling,
77  rtems_id            *id
78)
79{
80  register Semaphore_Control *the_semaphore;
81  CORE_mutex_Attributes       the_mutex_attributes;
82  CORE_semaphore_Attributes   the_semaphore_attributes;
83  uint32_t                    lock;
84
85  if ( !rtems_is_name_valid( name ) )
86    return RTEMS_INVALID_NAME;
87
88  if ( !id )
89    return RTEMS_INVALID_ADDRESS;
90
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
100  } else
101#endif
102
103  if ( _Attributes_Is_inherit_priority( attribute_set ) ||
104              _Attributes_Is_priority_ceiling( attribute_set ) ) {
105
106    if ( ! ( (_Attributes_Is_binary_semaphore( attribute_set ) ||
107              _Attributes_Is_simple_binary_semaphore( attribute_set )) &&
108
109             _Attributes_Is_priority( attribute_set ) ) )
110      return RTEMS_NOT_DEFINED;
111
112  }
113
114  if ( !_Attributes_Is_counting_semaphore( attribute_set ) && ( count > 1 ) )
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,
129                            the_semaphore->Object.id, false ) ) ) {
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
138  /*
139   *  If it is not a counting semaphore, then it is either a
140   *  simple binary semaphore or a more powerful mutex style binary
141   *  semaphore.
142   */
143
144  if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) {
145    if ( _Attributes_Is_inherit_priority( attribute_set ) )
146      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
147    else if ( _Attributes_Is_priority_ceiling( attribute_set ) )
148      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
149    else if ( _Attributes_Is_priority( attribute_set ) )
150      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY;
151    else
152      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_FIFO;
153
154
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:
161          the_mutex_attributes.only_owner_release = false;
162          break;
163        case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
164        case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
165          the_mutex_attributes.only_owner_release = true;
166          break;
167      }
168    } else {
169      the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_BLOCKS;
170      the_mutex_attributes.only_owner_release = false;
171    }
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,
183      lock
184    );
185  } else {
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
191    /*
192     *  This effectively disables limit checking.
193     */
194
195    the_semaphore_attributes.maximum_count = 0xFFFFFFFF;
196
197    /*
198     *  The following are just to make Purify happy.
199     */
200
201    the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
202    the_mutex_attributes.priority_ceiling = PRIORITY_MINIMUM;
203
204    _CORE_semaphore_Initialize(
205      &the_semaphore->Core_control.semaphore,
206      &the_semaphore_attributes,
207      count
208    );
209  }
210
211  _Objects_Open(
212    &_Semaphore_Information,
213    &the_semaphore->Object,
214    (Objects_Name) name
215  );
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}
Note: See TracBrowser for help on using the repository browser.