source: rtems/cpukit/rtems/src/semcreate.c @ 6b4518aa

4.104.114.84.95
Last change on this file since 6b4518aa was e980b219, checked in by Joel Sherrill <joel.sherrill@…>, on 05/06/04 at 19:21:40

2004-05-06 Joel Sherrill <joel@…>

PR 618/rtems

  • rtems/include/rtems/rtems/status.h, rtems/src/clockget.c, rtems/src/clockset.c, rtems/src/dpmemcreate.c, rtems/src/dpmemexternal2internal.c, rtems/src/dpmeminternal2external.c, rtems/src/eventmp.c, rtems/src/eventreceive.c, rtems/src/eventsend.c, rtems/src/msgqbroadcast.c, rtems/src/msgqcreate.c, rtems/src/msgqflush.c, rtems/src/msgqgetnumberpending.c, rtems/src/msgqreceive.c, rtems/src/msgqsubmit.c, rtems/src/partcreate.c, rtems/src/partdelete.c, rtems/src/partgetbuffer.c, rtems/src/ratemoncancel.c, rtems/src/ratemoncreate.c, rtems/src/ratemondelete.c, rtems/src/ratemongetstatus.c, rtems/src/ratemonident.c, rtems/src/ratemonperiod.c, rtems/src/regioncreate.c, rtems/src/regiondelete.c, rtems/src/regionextend.c, rtems/src/regiongetsegment.c, rtems/src/regiongetsegmentsize.c, rtems/src/regionreturnsegment.c, rtems/src/semcreate.c, rtems/src/semdelete.c, rtems/src/semflush.c, rtems/src/semident.c, rtems/src/taskcreate.c, rtems/src/taskgetnote.c, rtems/src/taskmode.c, rtems/src/taskrestart.c, rtems/src/taskresume.c, rtems/src/tasksetnote.c, rtems/src/tasksuspend.c, rtems/src/taskvariableadd.c, rtems/src/taskvariabledelete.c, rtems/src/taskvariableget.c, rtems/src/taskwakewhen.c, rtems/src/timercreate.c, rtems/src/timerdelete.c, rtems/src/timerfireafter.c, rtems/src/timerfirewhen.c, rtems/src/timerserverfireafter.c, rtems/src/timerserverfirewhen.c, score/include/rtems/score/object.h, score/src/coretodvalidate.c, score/src/objectnametoid.c: Add NULL checks.
  • 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#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
68rtems_status_code rtems_semaphore_create(
69  rtems_name           name,
70  uint32_t             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  uint32_t                    lock;
80
81  if ( !rtems_is_name_valid( name ) )
82    return RTEMS_INVALID_NAME;
83
84  if ( !id )
85    return RTEMS_INVALID_ADDRESS;
86
87#if defined(RTEMS_MULTIPROCESSING)
88  if ( _Attributes_Is_global( attribute_set ) ) {
89
90    if ( !_System_state_Is_multiprocessing )
91      return RTEMS_MP_NOT_CONFIGURED;
92
93    if ( _Attributes_Is_inherit_priority( attribute_set ) )
94      return RTEMS_NOT_DEFINED;
95
96  } else
97#endif
98
99  if ( _Attributes_Is_inherit_priority( attribute_set ) ||
100              _Attributes_Is_priority_ceiling( attribute_set ) ) {
101
102    if ( ! ( (_Attributes_Is_binary_semaphore( attribute_set ) ||
103              _Attributes_Is_simple_binary_semaphore( attribute_set )) &&
104
105             _Attributes_Is_priority( attribute_set ) ) )
106      return RTEMS_NOT_DEFINED;
107
108  }
109
110  if ( !_Attributes_Is_counting_semaphore( attribute_set ) && ( count > 1 ) )
111    return RTEMS_INVALID_NUMBER;
112
113  _Thread_Disable_dispatch();             /* prevents deletion */
114
115  the_semaphore = _Semaphore_Allocate();
116
117  if ( !the_semaphore ) {
118    _Thread_Enable_dispatch();
119    return RTEMS_TOO_MANY;
120  }
121
122#if defined(RTEMS_MULTIPROCESSING)
123  if ( _Attributes_Is_global( attribute_set ) &&
124       ! ( _Objects_MP_Allocate_and_open( &_Semaphore_Information, name,
125                            the_semaphore->Object.id, FALSE ) ) ) {
126    _Semaphore_Free( the_semaphore );
127    _Thread_Enable_dispatch();
128    return RTEMS_TOO_MANY;
129  }
130#endif
131
132  the_semaphore->attribute_set = attribute_set;
133
134  /*
135   *  If it is not a counting semaphore, then it is either a
136   *  simple binary semaphore or a more powerful mutex style binary
137   *  semaphore.
138   */
139
140  if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) {
141    if ( _Attributes_Is_inherit_priority( attribute_set ) )
142      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
143    else if ( _Attributes_Is_priority_ceiling( attribute_set ) )
144      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
145    else if ( _Attributes_Is_priority( attribute_set ) )
146      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY;
147    else
148      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_FIFO;
149
150
151    if ( _Attributes_Is_binary_semaphore( attribute_set ) ) {
152      the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
153
154      switch ( the_mutex_attributes.discipline ) {
155        case CORE_MUTEX_DISCIPLINES_FIFO:
156        case CORE_MUTEX_DISCIPLINES_PRIORITY:
157          the_mutex_attributes.only_owner_release = FALSE;
158          break;
159        case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
160        case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
161          the_mutex_attributes.only_owner_release = TRUE;
162          break;
163      }
164    } else {
165      the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_BLOCKS;
166      the_mutex_attributes.only_owner_release = FALSE;
167    }
168
169    the_mutex_attributes.priority_ceiling = priority_ceiling;
170
171    if ( count == 1 )
172      lock = CORE_MUTEX_UNLOCKED;
173    else
174      lock = CORE_MUTEX_LOCKED;
175
176    _CORE_mutex_Initialize(
177      &the_semaphore->Core_control.mutex,
178      &the_mutex_attributes,
179      lock
180    );
181  } else {
182    if ( _Attributes_Is_priority( attribute_set ) )
183      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_PRIORITY;
184    else
185      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO;
186
187    /*
188     *  This effectively disables limit checking.
189     */
190
191    the_semaphore_attributes.maximum_count = 0xFFFFFFFF;
192
193    /*
194     *  The following are just to make Purify happy.
195     */
196
197    the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
198    the_mutex_attributes.priority_ceiling = PRIORITY_MINIMUM;
199
200    _CORE_semaphore_Initialize(
201      &the_semaphore->Core_control.semaphore,
202      &the_semaphore_attributes,
203      count
204    );
205  }
206
207  _Objects_Open(
208    &_Semaphore_Information,
209    &the_semaphore->Object,
210    (Objects_Name) name
211  );
212
213  *id = the_semaphore->Object.id;
214
215#if defined(RTEMS_MULTIPROCESSING)
216  if ( _Attributes_Is_global( attribute_set ) )
217    _Semaphore_MP_Send_process_packet(
218      SEMAPHORE_MP_ANNOUNCE_CREATE,
219      the_semaphore->Object.id,
220      name,
221      0                          /* Not used */
222    );
223#endif
224  _Thread_Enable_dispatch();
225  return RTEMS_SUCCESSFUL;
226}
Note: See TracBrowser for help on using the repository browser.