source: rtems/cpukit/rtems/src/semcreate.c @ e93d791

4.104.115
Last change on this file since e93d791 was e93d791, checked in by Joel Sherrill <joel.sherrill@…>, on 03/02/09 at 16:12:41

2009-03-02 Joel Sherrill <joel.sherrill@…>

PR 1388/cpukit

  • rtems/src/semcreate.c: Classic Semaphores allow both priority inherit and ceiling attributes to be set on semaphore create. These attributes are mutually exclusive and this should be an error.
  • Property mode set to 100644
File size: 6.6 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-2009.
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_inherit_priority( attribute_set ) &&
115       _Attributes_Is_priority_ceiling( attribute_set ) )
116    return RTEMS_NOT_DEFINED;
117
118  if ( !_Attributes_Is_counting_semaphore( attribute_set ) && ( count > 1 ) )
119    return RTEMS_INVALID_NUMBER;
120
121  _Thread_Disable_dispatch();             /* prevents deletion */
122
123  the_semaphore = _Semaphore_Allocate();
124
125  if ( !the_semaphore ) {
126    _Thread_Enable_dispatch();
127    return RTEMS_TOO_MANY;
128  }
129
130#if defined(RTEMS_MULTIPROCESSING)
131  if ( _Attributes_Is_global( attribute_set ) &&
132       ! ( _Objects_MP_Allocate_and_open( &_Semaphore_Information, name,
133                            the_semaphore->Object.id, false ) ) ) {
134    _Semaphore_Free( the_semaphore );
135    _Thread_Enable_dispatch();
136    return RTEMS_TOO_MANY;
137  }
138#endif
139
140  the_semaphore->attribute_set = attribute_set;
141
142  /*
143   *  If it is not a counting semaphore, then it is either a
144   *  simple binary semaphore or a more powerful mutex style binary
145   *  semaphore.
146   */
147
148  if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) {
149    if ( _Attributes_Is_inherit_priority( attribute_set ) )
150      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
151    else if ( _Attributes_Is_priority_ceiling( attribute_set ) )
152      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
153    else if ( _Attributes_Is_priority( attribute_set ) )
154      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY;
155    else
156      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_FIFO;
157
158
159    if ( _Attributes_Is_binary_semaphore( attribute_set ) ) {
160      the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
161
162      switch ( the_mutex_attributes.discipline ) {
163        case CORE_MUTEX_DISCIPLINES_FIFO:
164        case CORE_MUTEX_DISCIPLINES_PRIORITY:
165          the_mutex_attributes.only_owner_release = false;
166          break;
167        case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
168        case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
169          the_mutex_attributes.only_owner_release = true;
170          break;
171      }
172    } else {
173      the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_BLOCKS;
174      the_mutex_attributes.only_owner_release = false;
175    }
176
177    the_mutex_attributes.priority_ceiling = priority_ceiling;
178
179    if ( count == 1 )
180      lock = CORE_MUTEX_UNLOCKED;
181    else
182      lock = CORE_MUTEX_LOCKED;
183
184    _CORE_mutex_Initialize(
185      &the_semaphore->Core_control.mutex,
186      &the_mutex_attributes,
187      lock
188    );
189  } else {
190    if ( _Attributes_Is_priority( attribute_set ) )
191      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_PRIORITY;
192    else
193      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO;
194
195    /*
196     *  This effectively disables limit checking.
197     */
198
199    the_semaphore_attributes.maximum_count = 0xFFFFFFFF;
200
201    /*
202     *  The following are just to make Purify happy.
203     */
204
205    the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
206    the_mutex_attributes.priority_ceiling = PRIORITY_MINIMUM;
207
208    _CORE_semaphore_Initialize(
209      &the_semaphore->Core_control.semaphore,
210      &the_semaphore_attributes,
211      count
212    );
213  }
214
215  _Objects_Open(
216    &_Semaphore_Information,
217    &the_semaphore->Object,
218    (Objects_Name) name
219  );
220
221  *id = the_semaphore->Object.id;
222
223#if defined(RTEMS_MULTIPROCESSING)
224  if ( _Attributes_Is_global( attribute_set ) )
225    _Semaphore_MP_Send_process_packet(
226      SEMAPHORE_MP_ANNOUNCE_CREATE,
227      the_semaphore->Object.id,
228      name,
229      0                          /* Not used */
230    );
231#endif
232  _Thread_Enable_dispatch();
233  return RTEMS_SUCCESSFUL;
234}
Note: See TracBrowser for help on using the repository browser.