source: rtems/cpukit/score/src/coresem.c @ 5e9b32b

4.104.114.84.9
Last change on this file since 5e9b32b was 5e9b32b, checked in by Joel Sherrill <joel.sherrill@…>, on Sep 26, 1995 at 7:27:15 PM

posix support initially added

  • Property mode set to 100644
File size: 4.9 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, 1990, 1991, 1992, 1993, 1994.
11 *  On-Line Applications Research Corporation (OAR).
12 *  All rights assigned to U.S. Government, 1994.
13 *
14 *  This material may be reproduced by or for the U.S. Government pursuant
15 *  to the copyright license under the clause at DFARS 252.227-7013.  This
16 *  notice must appear in all copies of this file and its derivatives.
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    executing->resource_count++;
139    _ISR_Enable( level );
140    return;
141  }
142
143  if ( !wait ) {
144    _ISR_Enable( level );
145    executing->Wait.return_code = CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT;
146    return;
147  }
148
149  the_semaphore->Wait_queue.sync = TRUE;
150  executing->Wait.queue          = &the_semaphore->Wait_queue;
151  executing->Wait.id             = id;
152  _ISR_Enable( level );
153
154  _Thread_queue_Enqueue( &the_semaphore->Wait_queue, timeout );
155}
156
157
158/*PAGE
159 *
160 *  _CORE_semaphore_Flush
161 *
162 *  This function a flushes the semaphore's task wait queue.
163 *
164 *  Input parameters:
165 *    the_semaphore          - the semaphore to be flushed
166 *    remote_extract_callout - function to invoke remotely
167 *    status                 - status to pass to thread
168 *
169 *  Output parameters:  NONE
170 */
171 
172void _CORE_semaphore_Flush(
173  CORE_semaphore_Control     *the_semaphore,
174  Thread_queue_Flush_callout  remote_extract_callout,
175  unsigned32                  status
176)
177{
178 
179  _Thread_queue_Flush(
180    &the_semaphore->Wait_queue,
181    remote_extract_callout,
182    status
183  );
184 
185}
Note: See TracBrowser for help on using the repository browser.