source: rtems/cpukit/rtems/src/semcreate.c @ 08311cc3

4.104.114.84.95
Last change on this file since 08311cc3 was 08311cc3, checked in by Joel Sherrill <joel.sherrill@…>, on 11/17/99 at 17:51:34

Updated copyright notice.

  • Property mode set to 100644
File size: 5.7 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_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_priority( attribute_set ) ) )
101      return RTEMS_NOT_DEFINED;
102
103  }
104
105  if ( _Attributes_Is_binary_semaphore( attribute_set ) && ( count > 1 ) )
106    return RTEMS_INVALID_NUMBER;
107
108  _Thread_Disable_dispatch();             /* prevents deletion */
109
110  the_semaphore = _Semaphore_Allocate();
111
112  if ( !the_semaphore ) {
113    _Thread_Enable_dispatch();
114    return RTEMS_TOO_MANY;
115  }
116
117#if defined(RTEMS_MULTIPROCESSING)
118  if ( _Attributes_Is_global( attribute_set ) &&
119       ! ( _Objects_MP_Allocate_and_open( &_Semaphore_Information, name,
120                            the_semaphore->Object.id, FALSE ) ) ) {
121    _Semaphore_Free( the_semaphore );
122    _Thread_Enable_dispatch();
123    return RTEMS_TOO_MANY;
124  }
125#endif
126
127  the_semaphore->attribute_set = attribute_set;
128
129  if ( _Attributes_Is_binary_semaphore( attribute_set ) ) {
130    if ( _Attributes_Is_inherit_priority( attribute_set ) )
131      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
132    else if (_Attributes_Is_priority_ceiling( attribute_set ) )
133      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
134    else if (_Attributes_Is_priority( attribute_set ) )
135      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY;
136    else
137      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_FIFO;
138
139    the_mutex_attributes.allow_nesting = TRUE;
140
141    /* Add priority ceiling code here ????? */
142
143    the_mutex_attributes.priority_ceiling = priority_ceiling;
144
145    if ( count == 1 )
146      lock = CORE_MUTEX_UNLOCKED;
147    else
148      lock = CORE_MUTEX_LOCKED;
149
150    _CORE_mutex_Initialize(
151      &the_semaphore->Core_control.mutex,
152      OBJECTS_RTEMS_SEMAPHORES,
153      &the_mutex_attributes,
154      lock,
155#if defined(RTEMS_MULTIPROCESSING)
156      _Semaphore_MP_Send_extract_proxy
157#else
158      NULL
159#endif
160    );
161  }
162  else {
163    if ( _Attributes_Is_priority( attribute_set ) )
164      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_PRIORITY;
165    else
166      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO;
167
168    /*
169     *  This effectively disables limit checking.
170     */
171
172    the_semaphore_attributes.maximum_count = 0xFFFFFFFF;
173
174    /*
175     *  The following are just to make Purify happy.
176     */
177
178    the_mutex_attributes.allow_nesting = TRUE;
179    the_mutex_attributes.priority_ceiling = PRIORITY_MINIMUM;
180
181    _CORE_semaphore_Initialize(
182      &the_semaphore->Core_control.semaphore,
183      OBJECTS_RTEMS_SEMAPHORES,
184      &the_semaphore_attributes,
185      count,
186#if defined(RTEMS_MULTIPROCESSING)
187      _Semaphore_MP_Send_extract_proxy
188#else
189      NULL
190#endif
191    );
192  }
193
194  _Objects_Open( &_Semaphore_Information, &the_semaphore->Object, &name );
195
196  *id = the_semaphore->Object.id;
197
198#if defined(RTEMS_MULTIPROCESSING)
199  if ( _Attributes_Is_global( attribute_set ) )
200    _Semaphore_MP_Send_process_packet(
201      SEMAPHORE_MP_ANNOUNCE_CREATE,
202      the_semaphore->Object.id,
203      name,
204      0                          /* Not used */
205    );
206#endif
207  _Thread_Enable_dispatch();
208  return RTEMS_SUCCESSFUL;
209}
Note: See TracBrowser for help on using the repository browser.