source: rtems/c/src/exec/rtems/src/semcreate.c @ d80188eb

4.104.114.84.95
Last change on this file since d80188eb was d80188eb, checked in by Joel Sherrill <joel.sherrill@…>, on 11/02/99 at 15:30:20

Added code to utilize the maximum_count attribute of core semaphores
and initialize it to a value that would keep it from every being
a problem.

  • Property mode set to 100644
File size: 5.8 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-1998.
19 *  On-Line Applications Research Corporation (OAR).
20 *  Copyright assigned to U.S. Government, 1994.
21 *
22 *  The license and distribution terms for this file may be
23 *  found in the file LICENSE in this distribution or at
24 *  http://www.OARcorp.com/rtems/license.html.
25 *
26 *  $Id$
27 */
28
29#include <rtems/system.h>
30#include <rtems/rtems/status.h>
31#include <rtems/rtems/support.h>
32#include <rtems/rtems/attr.h>
33#include <rtems/score/isr.h>
34#include <rtems/score/object.h>
35#include <rtems/rtems/options.h>
36#include <rtems/rtems/sem.h>
37#include <rtems/score/coremutex.h>
38#include <rtems/score/coresem.h>
39#include <rtems/score/states.h>
40#include <rtems/score/thread.h>
41#include <rtems/score/threadq.h>
42#if defined(RTEMS_MULTIPROCESSING)
43#include <rtems/score/mpci.h>
44#endif
45#include <rtems/score/sysstate.h>
46
47#include <rtems/score/interr.h>
48
49/*PAGE
50 *
51 *  rtems_semaphore_create
52 *
53 *  This directive creates a semaphore and sets the initial value based
54 *  on the given count.  A semaphore id is returned.
55 *
56 *  Input parameters:
57 *    name             - user defined semaphore name
58 *    count            - initial count of semaphore
59 *    attribute_set    - semaphore attributes
60 *    priority_ceiling - semaphore's ceiling priority
61 *    id               - pointer to semaphore id
62 *
63 *  Output parameters:
64 *    id       - semaphore id
65 *    RTEMS_SUCCESSFUL - if successful
66 *    error code - if unsuccessful
67 */
68
69rtems_status_code rtems_semaphore_create(
70  rtems_name           name,
71  unsigned32           count,
72  rtems_attribute      attribute_set,
73  rtems_task_priority  priority_ceiling,
74  Objects_Id          *id
75)
76{
77  register Semaphore_Control *the_semaphore;
78  CORE_mutex_Attributes       the_mutex_attributes;
79  CORE_semaphore_Attributes   the_semaphore_attributes;
80  unsigned32                  lock;
81
82  if ( !rtems_is_name_valid( name ) )
83    return RTEMS_INVALID_NAME;
84
85#if defined(RTEMS_MULTIPROCESSING)
86  if ( _Attributes_Is_global( attribute_set ) ) {
87
88    if ( !_System_state_Is_multiprocessing )
89      return RTEMS_MP_NOT_CONFIGURED;
90
91    if ( _Attributes_Is_inherit_priority( attribute_set ) )
92      return RTEMS_NOT_DEFINED;
93
94  } else
95#endif
96
97  if ( _Attributes_Is_inherit_priority( attribute_set ) ||
98              _Attributes_Is_priority_ceiling( attribute_set ) ) {
99
100    if ( ! ( _Attributes_Is_binary_semaphore( attribute_set ) &&
101             _Attributes_Is_priority( attribute_set ) ) )
102      return RTEMS_NOT_DEFINED;
103
104  }
105
106  if ( _Attributes_Is_binary_semaphore( attribute_set ) && ( count > 1 ) )
107    return RTEMS_INVALID_NUMBER;
108
109  _Thread_Disable_dispatch();             /* prevents deletion */
110
111  the_semaphore = _Semaphore_Allocate();
112
113  if ( !the_semaphore ) {
114    _Thread_Enable_dispatch();
115    return RTEMS_TOO_MANY;
116  }
117
118#if defined(RTEMS_MULTIPROCESSING)
119  if ( _Attributes_Is_global( attribute_set ) &&
120       ! ( _Objects_MP_Allocate_and_open( &_Semaphore_Information, name,
121                            the_semaphore->Object.id, FALSE ) ) ) {
122    _Semaphore_Free( the_semaphore );
123    _Thread_Enable_dispatch();
124    return RTEMS_TOO_MANY;
125  }
126#endif
127
128  the_semaphore->attribute_set = attribute_set;
129
130  if ( _Attributes_Is_binary_semaphore( attribute_set ) ) {
131    if ( _Attributes_Is_inherit_priority( attribute_set ) )
132      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
133    else if (_Attributes_Is_priority_ceiling( attribute_set ) )
134      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
135    else if (_Attributes_Is_priority( attribute_set ) )
136      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY;
137    else
138      the_mutex_attributes.discipline = CORE_MUTEX_DISCIPLINES_FIFO;
139
140    the_mutex_attributes.allow_nesting = TRUE;
141
142    /* Add priority ceiling code here ????? */
143
144    the_mutex_attributes.priority_ceiling = priority_ceiling;
145
146    if ( count == 1 )
147      lock = CORE_MUTEX_UNLOCKED;
148    else
149      lock = CORE_MUTEX_LOCKED;
150
151    _CORE_mutex_Initialize(
152      &the_semaphore->Core_control.mutex,
153      OBJECTS_RTEMS_SEMAPHORES,
154      &the_mutex_attributes,
155      lock,
156#if defined(RTEMS_MULTIPROCESSING)
157      _Semaphore_MP_Send_extract_proxy
158#else
159      NULL
160#endif
161    );
162  }
163  else {
164    if ( _Attributes_Is_priority( attribute_set ) )
165      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_PRIORITY;
166    else
167      the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO;
168
169    /*
170     *  This effectively disables limit checking.
171     */
172
173    the_semaphore_attributes.maximum_count = 0xFFFFFFFF;
174
175    /*
176     *  The following are just to make Purify happy.
177     */
178
179    the_mutex_attributes.allow_nesting = TRUE;
180    the_mutex_attributes.priority_ceiling = PRIORITY_MINIMUM;
181
182    _CORE_semaphore_Initialize(
183      &the_semaphore->Core_control.semaphore,
184      OBJECTS_RTEMS_SEMAPHORES,
185      &the_semaphore_attributes,
186      count,
187#if defined(RTEMS_MULTIPROCESSING)
188      _Semaphore_MP_Send_extract_proxy
189#else
190      NULL
191#endif
192    );
193  }
194
195  _Objects_Open( &_Semaphore_Information, &the_semaphore->Object, &name );
196
197  *id = the_semaphore->Object.id;
198
199#if defined(RTEMS_MULTIPROCESSING)
200  if ( _Attributes_Is_global( attribute_set ) )
201    _Semaphore_MP_Send_process_packet(
202      SEMAPHORE_MP_ANNOUNCE_CREATE,
203      the_semaphore->Object.id,
204      name,
205      0                          /* Not used */
206    );
207#endif
208  _Thread_Enable_dispatch();
209  return RTEMS_SUCCESSFUL;
210}
Note: See TracBrowser for help on using the repository browser.