source: rtems/cpukit/rtems/src/semcreate.c @ 277cc95

4.104.114.84.95
Last change on this file since 277cc95 was 277cc95, checked in by Joel Sherrill <joel.sherrill@…>, on 09/04/03 at 18:54:32

2003-09-04 Joel Sherrill <joel@…>

  • include/rtems.h, include/rtems/rtems/asr.h, include/rtems/rtems/attr.h, include/rtems/rtems/cache.h, include/rtems/rtems/clock.h, include/rtems/rtems/config.h, include/rtems/rtems/dpmem.h, include/rtems/rtems/event.h, include/rtems/rtems/eventmp.h, include/rtems/rtems/eventset.h, include/rtems/rtems/intr.h, include/rtems/rtems/message.h, include/rtems/rtems/modes.h, include/rtems/rtems/mp.h, include/rtems/rtems/msgmp.h, include/rtems/rtems/options.h, include/rtems/rtems/part.h, include/rtems/rtems/partmp.h, include/rtems/rtems/ratemon.h, include/rtems/rtems/region.h, include/rtems/rtems/regionmp.h, include/rtems/rtems/rtemsapi.h, include/rtems/rtems/sem.h, include/rtems/rtems/semmp.h, include/rtems/rtems/signal.h, include/rtems/rtems/signalmp.h, include/rtems/rtems/status.h, include/rtems/rtems/support.h, include/rtems/rtems/taskmp.h, include/rtems/rtems/tasks.h, include/rtems/rtems/timer.h, include/rtems/rtems/types.h, inline/rtems/rtems/asr.inl, inline/rtems/rtems/attr.inl, inline/rtems/rtems/dpmem.inl, inline/rtems/rtems/event.inl, inline/rtems/rtems/eventset.inl, inline/rtems/rtems/message.inl, inline/rtems/rtems/modes.inl, inline/rtems/rtems/options.inl, inline/rtems/rtems/part.inl, inline/rtems/rtems/ratemon.inl, inline/rtems/rtems/region.inl, inline/rtems/rtems/sem.inl, inline/rtems/rtems/status.inl, inline/rtems/rtems/support.inl, inline/rtems/rtems/tasks.inl, inline/rtems/rtems/timer.inl, macros/rtems/rtems/asr.inl, macros/rtems/rtems/attr.inl, macros/rtems/rtems/dpmem.inl, macros/rtems/rtems/event.inl, macros/rtems/rtems/eventset.inl, macros/rtems/rtems/message.inl, macros/rtems/rtems/modes.inl, macros/rtems/rtems/options.inl, macros/rtems/rtems/part.inl, macros/rtems/rtems/ratemon.inl, macros/rtems/rtems/region.inl, macros/rtems/rtems/sem.inl, macros/rtems/rtems/status.inl, macros/rtems/rtems/support.inl, macros/rtems/rtems/tasks.inl, macros/rtems/rtems/timer.inl, src/attr.c, src/clockget.c, src/clockset.c, src/clocktick.c, src/dpmem.c, src/dpmemcreate.c, src/dpmemdelete.c, src/dpmemexternal2internal.c, src/dpmemident.c, src/dpmeminternal2external.c, src/event.c, src/eventmp.c, src/eventreceive.c, src/eventseize.c, src/eventsend.c, src/eventsurrender.c, src/eventtimeout.c, src/intr.c, src/intrbody.c, src/intrcatch.c, src/mp.c, src/msg.c, src/msgmp.c, src/msgqallocate.c, src/msgqbroadcast.c, src/msgqcreate.c, src/msgqdelete.c, src/msgqflush.c, src/msgqgetnumberpending.c, src/msgqident.c, src/msgqreceive.c, src/msgqsend.c, src/msgqsubmit.c, src/msgqtranslatereturncode.c, src/msgqurgent.c, src/part.c, src/partcreate.c, src/partdelete.c, src/partgetbuffer.c, src/partident.c, src/partmp.c, src/partreturnbuffer.c, src/ratemon.c, src/ratemoncancel.c, src/ratemoncreate.c, src/ratemondelete.c, src/ratemongetstatus.c, src/ratemonident.c, src/ratemonperiod.c, src/ratemontimeout.c, src/region.c, src/regioncreate.c, src/regiondelete.c, src/regionextend.c, src/regiongetinfo.c, src/regiongetsegment.c, src/regiongetsegmentsize.c, src/regionident.c, src/regionmp.c, src/regionreturnsegment.c, src/rtclock.c, src/rtemsidtoname.c, src/rtemstimer.c, src/sem.c, src/semcreate.c, src/semdelete.c, src/semflush.c, src/semident.c, src/semmp.c, src/semobtain.c, src/semrelease.c, src/semtranslatereturncode.c, src/signal.c, src/signalcatch.c, src/signalmp.c, src/signalsend.c, src/taskcreate.c, src/taskdelete.c, src/taskgetnote.c, src/taskident.c, src/taskinitusers.c, src/taskissuspended.c, src/taskmode.c, src/taskmp.c, src/taskrestart.c, src/taskresume.c, src/tasks.c, src/tasksetnote.c, src/tasksetpriority.c, src/taskstart.c, src/tasksuspend.c, src/taskvariableadd.c, src/taskvariabledelete.c, src/taskvariableget.c, src/taskwakeafter.c, src/taskwakewhen.c, src/timercancel.c, src/timercreate.c, src/timerdelete.c, src/timerfireafter.c, src/timerfirewhen.c, src/timergetinfo.c, src/timerident.c, src/timerreset.c, src/timerserver.c, src/timerserverfireafter.c, src/timerserverfirewhen.c: URL for license changed.
  • Property mode set to 100644
File size: 6.3 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  unsigned32           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  unsigned32                  lock;
80
81  if ( !rtems_is_name_valid( name ) )
82    return RTEMS_INVALID_NAME;
83
84#if defined(RTEMS_MULTIPROCESSING)
85  if ( _Attributes_Is_global( attribute_set ) ) {
86
87    if ( !_System_state_Is_multiprocessing )
88      return RTEMS_MP_NOT_CONFIGURED;
89
90    if ( _Attributes_Is_inherit_priority( attribute_set ) )
91      return RTEMS_NOT_DEFINED;
92
93  } else
94#endif
95
96  if ( _Attributes_Is_inherit_priority( attribute_set ) ||
97              _Attributes_Is_priority_ceiling( attribute_set ) ) {
98
99    if ( ! ( (_Attributes_Is_binary_semaphore( attribute_set ) ||
100              _Attributes_Is_simple_binary_semaphore( attribute_set )) &&
101             
102             _Attributes_Is_priority( attribute_set ) ) )
103      return RTEMS_NOT_DEFINED;
104
105  }
106
107  if ( !_Attributes_Is_counting_semaphore( attribute_set ) && ( count > 1 ) )
108    return RTEMS_INVALID_NUMBER;
109
110  _Thread_Disable_dispatch();             /* prevents deletion */
111
112  the_semaphore = _Semaphore_Allocate();
113
114  if ( !the_semaphore ) {
115    _Thread_Enable_dispatch();
116    return RTEMS_TOO_MANY;
117  }
118
119#if defined(RTEMS_MULTIPROCESSING)
120  if ( _Attributes_Is_global( attribute_set ) &&
121       ! ( _Objects_MP_Allocate_and_open( &_Semaphore_Information, name,
122                            the_semaphore->Object.id, FALSE ) ) ) {
123    _Semaphore_Free( the_semaphore );
124    _Thread_Enable_dispatch();
125    return RTEMS_TOO_MANY;
126  }
127#endif
128
129  the_semaphore->attribute_set = attribute_set;
130
131  /*
132   *  If it is not a counting semaphore, then it is either a
133   *  simple binary semaphore or a more powerful mutex style binary
134   *  semaphore.
135   */
136
137  if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) {
138    if ( _Attributes_Is_inherit_priority( attribute_set ) )
139      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
140    else if ( _Attributes_Is_priority_ceiling( attribute_set ) )
141      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
142    else if ( _Attributes_Is_priority( attribute_set ) )
143      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY;
144    else
145      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_FIFO;
146
147
148    if ( _Attributes_Is_binary_semaphore( attribute_set ) ) {
149      the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
150
151      switch ( the_mutex_attributes.discipline ) {
152        case CORE_MUTEX_DISCIPLINES_FIFO:
153        case CORE_MUTEX_DISCIPLINES_PRIORITY:
154          the_mutex_attributes.only_owner_release = FALSE;
155          break;
156        case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
157        case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
158          the_mutex_attributes.only_owner_release = TRUE;
159          break;
160      }
161    } else {
162      the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_BLOCKS;
163      the_mutex_attributes.only_owner_release = FALSE;
164    }
165
166    the_mutex_attributes.priority_ceiling = priority_ceiling;
167
168    if ( count == 1 )
169      lock = CORE_MUTEX_UNLOCKED;
170    else
171      lock = CORE_MUTEX_LOCKED;
172
173    _CORE_mutex_Initialize(
174      &the_semaphore->Core_control.mutex,
175      &the_mutex_attributes,
176      lock
177    );
178  } else {
179    if ( _Attributes_Is_priority( attribute_set ) )
180      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_PRIORITY;
181    else
182      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO;
183
184    /*
185     *  This effectively disables limit checking.
186     */
187
188    the_semaphore_attributes.maximum_count = 0xFFFFFFFF;
189
190    /*
191     *  The following are just to make Purify happy.
192     */
193
194    the_mutex_attributes.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
195    the_mutex_attributes.priority_ceiling = PRIORITY_MINIMUM;
196
197    _CORE_semaphore_Initialize(
198      &the_semaphore->Core_control.semaphore,
199      &the_semaphore_attributes,
200      count
201    );
202  }
203
204  _Objects_Open(
205    &_Semaphore_Information,
206    &the_semaphore->Object,
207    (Objects_Name) name
208  );
209
210  *id = the_semaphore->Object.id;
211
212#if defined(RTEMS_MULTIPROCESSING)
213  if ( _Attributes_Is_global( attribute_set ) )
214    _Semaphore_MP_Send_process_packet(
215      SEMAPHORE_MP_ANNOUNCE_CREATE,
216      the_semaphore->Object.id,
217      name,
218      0                          /* Not used */
219    );
220#endif
221  _Thread_Enable_dispatch();
222  return RTEMS_SUCCESSFUL;
223}
Note: See TracBrowser for help on using the repository browser.