source: rtems/cpukit/score/src/coresem.c @ 8f0529f

4.104.114.84.9
Last change on this file since 8f0529f was 8f0529f, checked in by Joel Sherrill <joel.sherrill@…>, on Nov 2, 1999 at 3:58:09 PM

Added maximum count detection logic.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 *  CORE Semaphore Handler
3 *
4 *  DESCRIPTION:
5 *
6 *  This package is the implementation of the CORE Semaphore Handler.
7 *  This core object utilizes standard Dijkstra counting semaphores to provide
8 *  synchronization and mutual exclusion capabilities.
9 *
10 *  COPYRIGHT (c) 1989-1998.
11 *  On-Line Applications Research Corporation (OAR).
12 *  Copyright assigned to U.S. Government, 1994.
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.OARcorp.com/rtems/license.html.
17 *
18 *  $Id$
19 */
20
21#include <rtems/system.h>
22#include <rtems/score/isr.h>
23#include <rtems/score/coresem.h>
24#include <rtems/score/states.h>
25#include <rtems/score/thread.h>
26#include <rtems/score/threadq.h>
27#if defined(RTEMS_MULTIPROCESSING)
28#include <rtems/score/mpci.h>
29#endif
30
31/*PAGE
32 *
33 *  CORE_semaphore_Initialize
34 *
35 *  This function initialize a semaphore and sets the initial value based
36 *  on the given count.
37 *
38 *  Input parameters:
39 *    the_semaphore            - the semaphore control block to initialize
40 *    the_class                - the API class of the object
41 *    the_semaphore_attributes - the attributes specified at create time
42 *    initial_value            - semaphore's initial value
43 *    proxy_extract_callout    - MP specific extract callout
44 *
45 *  Output parameters:  NONE
46 */
47
48void _CORE_semaphore_Initialize(
49  CORE_semaphore_Control       *the_semaphore,
50  Objects_Classes               the_class,
51  CORE_semaphore_Attributes    *the_semaphore_attributes,
52  unsigned32                    initial_value,
53  Thread_queue_Extract_callout  proxy_extract_callout
54)
55{
56
57  the_semaphore->Attributes = *the_semaphore_attributes;
58  the_semaphore->count      = initial_value;
59
60  _Thread_queue_Initialize(
61    &the_semaphore->Wait_queue,
62    the_class,
63    _CORE_semaphore_Is_priority( the_semaphore_attributes ) ?
64              THREAD_QUEUE_DISCIPLINE_PRIORITY : THREAD_QUEUE_DISCIPLINE_FIFO,
65    STATES_WAITING_FOR_SEMAPHORE,
66    proxy_extract_callout,
67    CORE_SEMAPHORE_TIMEOUT
68  );
69}
70
71/*PAGE
72 *
73 *  _CORE_semaphore_Surrender
74 *
75 *  Input parameters:
76 *    the_semaphore            - the semaphore to be flushed
77 *    id                       - id of parent semaphore
78 *    api_semaphore_mp_support - api dependent MP support actions
79 *
80 *  Output parameters:
81 *    CORE_SEMAPHORE_STATUS_SUCCESSFUL - if successful
82 *    core error code                  - if unsuccessful
83 *
84 *  Output parameters:
85 */
86
87CORE_semaphore_Status _CORE_semaphore_Surrender(
88  CORE_semaphore_Control                *the_semaphore,
89  Objects_Id                             id,
90  CORE_semaphore_API_mp_support_callout  api_semaphore_mp_support
91)
92{
93  Thread_Control *the_thread;
94  ISR_Level       level;
95  CORE_semaphore_Status status;
96
97  status = CORE_SEMAPHORE_STATUS_SUCCESSFUL;
98
99  if ( (the_thread = _Thread_queue_Dequeue(&the_semaphore->Wait_queue)) ) {
100
101    if ( !_Objects_Is_local_id( the_thread->Object.id ) )
102      (*api_semaphore_mp_support) ( the_thread, id );
103
104  } else {
105    _ISR_Disable( level );
106      if ( the_semaphore->count <= the_semaphore->Attributes.maximum_count )
107        the_semaphore->count += 1;
108      else
109        status = CORE_SEMAPHORE_MAXIMUM_COUNT_EXCEEDED;
110    _ISR_Enable( level );
111  }
112
113  return status;
114}
115
116/*PAGE
117 *
118 *  _CORE_semaphore_Seize
119 *
120 *  This routine attempts to allocate a core semaphore to the calling thread.
121 *
122 *  Input parameters:
123 *    the_semaphore - pointer to semaphore control block
124 *    id            - id of object to wait on
125 *    wait          - TRUE if wait is allowed, FALSE otherwise
126 *    timeout       - number of ticks to wait (0 means forever)
127 *
128 *  Output parameters:  NONE
129 *
130 *  INTERRUPT LATENCY:
131 *    available
132 *    wait
133 */
134
135void _CORE_semaphore_Seize(
136  CORE_semaphore_Control  *the_semaphore,
137  Objects_Id               id,
138  boolean                  wait,
139  Watchdog_Interval        timeout
140)
141{
142  Thread_Control *executing;
143  ISR_Level       level;
144
145  executing = _Thread_Executing;
146  executing->Wait.return_code = CORE_SEMAPHORE_STATUS_SUCCESSFUL;
147  _ISR_Disable( level );
148  if ( the_semaphore->count != 0 ) {
149    the_semaphore->count -= 1;
150    _ISR_Enable( level );
151    return;
152  }
153
154  if ( !wait ) {
155    _ISR_Enable( level );
156    executing->Wait.return_code = CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT;
157    return;
158  }
159
160  _Thread_queue_Enter_critical_section( &the_semaphore->Wait_queue );
161  executing->Wait.queue          = &the_semaphore->Wait_queue;
162  executing->Wait.id             = id;
163  _ISR_Enable( level );
164
165  _Thread_queue_Enqueue( &the_semaphore->Wait_queue, timeout );
166}
167
168
169/*PAGE
170 *
171 *  _CORE_semaphore_Flush
172 *
173 *  This function a flushes the semaphore's task wait queue.
174 *
175 *  Input parameters:
176 *    the_semaphore          - the semaphore to be flushed
177 *    remote_extract_callout - function to invoke remotely
178 *    status                 - status to pass to thread
179 *
180 *  Output parameters:  NONE
181 */
182 
183void _CORE_semaphore_Flush(
184  CORE_semaphore_Control     *the_semaphore,
185  Thread_queue_Flush_callout  remote_extract_callout,
186  unsigned32                  status
187)
188{
189 
190  _Thread_queue_Flush(
191    &the_semaphore->Wait_queue,
192    remote_extract_callout,
193    status
194  );
195 
196}
Note: See TracBrowser for help on using the repository browser.