source: rtems/c/src/exec/score/src/coresem.c @ 03f2154e

4.104.114.84.95
Last change on this file since 03f2154e was 03f2154e, checked in by Joel Sherrill <joel.sherrill@…>, on 04/22/97 at 17:20:27

headers updated to reflect new style copyright notice as part
of switching to the modified GNU GPL.

  • Property mode set to 100644
File size: 4.8 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-1997.
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 in
15 *  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#include <rtems/score/mpci.h>
28
29/*PAGE
30 *
31 *  CORE_semaphore_Initialize
32 *
33 *  This function initialize a semaphore and sets the initial value based
34 *  on the given count.
35 *
36 *  Input parameters:
37 *    the_semaphore            - the semaphore control block to initialize
38 *    the_class                - the API class of the object
39 *    the_semaphore_attributes - the attributes specified at create time
40 *    initial_value            - semaphore's initial value
41 *    proxy_extract_callout    - MP specific extract callout
42 *
43 *  Output parameters:  NONE
44 */
45
46void _CORE_semaphore_Initialize(
47  CORE_semaphore_Control       *the_semaphore,
48  Objects_Classes               the_class,
49  CORE_semaphore_Attributes    *the_semaphore_attributes,
50  unsigned32                    initial_value,
51  Thread_queue_Extract_callout  proxy_extract_callout
52)
53{
54
55  the_semaphore->Attributes = *the_semaphore_attributes;
56  the_semaphore->count      = initial_value;
57
58  _Thread_queue_Initialize(
59    &the_semaphore->Wait_queue,
60    the_class,
61    _CORE_semaphore_Is_priority( the_semaphore_attributes ) ?
62              THREAD_QUEUE_DISCIPLINE_PRIORITY : THREAD_QUEUE_DISCIPLINE_FIFO,
63    STATES_WAITING_FOR_SEMAPHORE,
64    proxy_extract_callout,
65    CORE_SEMAPHORE_TIMEOUT
66  );
67}
68
69/*PAGE
70 *
71 *  _CORE_semaphore_Surrender
72 *
73 *  Input parameters:
74 *    the_semaphore            - the semaphore to be flushed
75 *    id                       - id of parent semaphore
76 *    api_semaphore_mp_support - api dependent MP support actions
77 *
78 *  Output parameters:
79 *    CORE_SEMAPHORE_STATUS_SUCCESSFUL - if successful
80 *    core error code                  - if unsuccessful
81 *
82 *  Output parameters:
83 */
84
85CORE_semaphore_Status _CORE_semaphore_Surrender(
86  CORE_semaphore_Control                *the_semaphore,
87  Objects_Id                             id,
88  CORE_semaphore_API_mp_support_callout  api_semaphore_mp_support
89)
90{
91  Thread_Control *the_thread;
92
93  if ( (the_thread = _Thread_queue_Dequeue(&the_semaphore->Wait_queue)) ) {
94
95    if ( !_Objects_Is_local_id( the_thread->Object.id ) )
96      (*api_semaphore_mp_support) ( the_thread, id );
97
98  } else
99    the_semaphore->count += 1;
100
101  return( CORE_SEMAPHORE_STATUS_SUCCESSFUL );
102}
103
104/*PAGE
105 *
106 *  _CORE_semaphore_Seize
107 *
108 *  This routine attempts to allocate a core semaphore to the calling thread.
109 *
110 *  Input parameters:
111 *    the_semaphore - pointer to semaphore control block
112 *    id            - id of object to wait on
113 *    wait          - TRUE if wait is allowed, FALSE otherwise
114 *    timeout       - number of ticks to wait (0 means forever)
115 *
116 *  Output parameters:  NONE
117 *
118 *  INTERRUPT LATENCY:
119 *    available
120 *    wait
121 */
122
123void _CORE_semaphore_Seize(
124  CORE_semaphore_Control  *the_semaphore,
125  Objects_Id               id,
126  boolean                  wait,
127  Watchdog_Interval        timeout
128)
129{
130  Thread_Control *executing;
131  ISR_Level       level;
132
133  executing = _Thread_Executing;
134  executing->Wait.return_code = CORE_SEMAPHORE_STATUS_SUCCESSFUL;
135  _ISR_Disable( level );
136  if ( the_semaphore->count != 0 ) {
137    the_semaphore->count -= 1;
138    _ISR_Enable( level );
139    return;
140  }
141
142  if ( !wait ) {
143    _ISR_Enable( level );
144    executing->Wait.return_code = CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT;
145    return;
146  }
147
148  _Thread_queue_Enter_critical_section( &the_semaphore->Wait_queue );
149  executing->Wait.queue          = &the_semaphore->Wait_queue;
150  executing->Wait.id             = id;
151  _ISR_Enable( level );
152
153  _Thread_queue_Enqueue( &the_semaphore->Wait_queue, timeout );
154}
155
156
157/*PAGE
158 *
159 *  _CORE_semaphore_Flush
160 *
161 *  This function a flushes the semaphore's task wait queue.
162 *
163 *  Input parameters:
164 *    the_semaphore          - the semaphore to be flushed
165 *    remote_extract_callout - function to invoke remotely
166 *    status                 - status to pass to thread
167 *
168 *  Output parameters:  NONE
169 */
170 
171void _CORE_semaphore_Flush(
172  CORE_semaphore_Control     *the_semaphore,
173  Thread_queue_Flush_callout  remote_extract_callout,
174  unsigned32                  status
175)
176{
177 
178  _Thread_queue_Flush(
179    &the_semaphore->Wait_queue,
180    remote_extract_callout,
181    status
182  );
183 
184}
Note: See TracBrowser for help on using the repository browser.