1 | /* |
---|
2 | * Event Manager |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * All rights assigned to U.S. Government, 1994. |
---|
7 | * |
---|
8 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
9 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
10 | * notice must appear in all copies of this file and its derivatives. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #include <rtems/system.h> |
---|
16 | #include <rtems/event.h> |
---|
17 | #include <rtems/isr.h> |
---|
18 | #include <rtems/object.h> |
---|
19 | #include <rtems/options.h> |
---|
20 | #include <rtems/states.h> |
---|
21 | #include <rtems/thread.h> |
---|
22 | |
---|
23 | /*PAGE |
---|
24 | * |
---|
25 | * rtems_event_send |
---|
26 | * |
---|
27 | * This directive allows a thread send an event set to another thread. |
---|
28 | * |
---|
29 | * Input parameters: |
---|
30 | * id - thread id |
---|
31 | * event - event set |
---|
32 | * |
---|
33 | * Output parameters: |
---|
34 | * RTEMS_SUCCESSFUL - if successful |
---|
35 | * error code - if unsuccessful |
---|
36 | */ |
---|
37 | |
---|
38 | rtems_status_code rtems_event_send( |
---|
39 | Objects_Id id, |
---|
40 | rtems_event_set event_in |
---|
41 | ) |
---|
42 | { |
---|
43 | register Thread_Control *the_thread; |
---|
44 | Objects_Locations location; |
---|
45 | |
---|
46 | the_thread = _Thread_Get( id, &location ); |
---|
47 | switch ( location ) { |
---|
48 | case OBJECTS_ERROR: |
---|
49 | return( RTEMS_INVALID_ID ); |
---|
50 | case OBJECTS_REMOTE: |
---|
51 | return( |
---|
52 | _Event_MP_Send_request_packet( |
---|
53 | EVENT_MP_SEND_REQUEST, |
---|
54 | id, |
---|
55 | event_in |
---|
56 | ) |
---|
57 | ); |
---|
58 | case OBJECTS_LOCAL: |
---|
59 | _Event_sets_Post( event_in, &the_thread->pending_events ); |
---|
60 | _Event_Surrender( the_thread ); |
---|
61 | _Thread_Enable_dispatch(); |
---|
62 | return( RTEMS_SUCCESSFUL ); |
---|
63 | } |
---|
64 | |
---|
65 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
66 | } |
---|
67 | |
---|
68 | /*PAGE |
---|
69 | * |
---|
70 | * rtems_event_receive |
---|
71 | * |
---|
72 | * This directive allows a thread to receive a set of events. |
---|
73 | * |
---|
74 | * Input parameters: |
---|
75 | * event_in - input event condition |
---|
76 | * option_set - options |
---|
77 | * ticks - number of ticks to wait (0 means wait forever) |
---|
78 | * event_out - pointer to output event set |
---|
79 | * |
---|
80 | * Output parameters: |
---|
81 | * event out - event set |
---|
82 | * RTEMS_SUCCESSFUL - if successful |
---|
83 | * error code - if unsuccessful |
---|
84 | */ |
---|
85 | |
---|
86 | rtems_status_code rtems_event_receive( |
---|
87 | rtems_event_set event_in, |
---|
88 | rtems_option option_set, |
---|
89 | rtems_interval ticks, |
---|
90 | rtems_event_set *event_out |
---|
91 | ) |
---|
92 | { |
---|
93 | if ( _Event_sets_Is_empty( event_in ) ) { |
---|
94 | *event_out = _Thread_Executing->pending_events; |
---|
95 | return( RTEMS_SUCCESSFUL ); |
---|
96 | } |
---|
97 | |
---|
98 | _Thread_Disable_dispatch(); |
---|
99 | _Event_Seize( event_in, option_set, ticks ); |
---|
100 | _Thread_Enable_dispatch(); |
---|
101 | *event_out = _Thread_Executing->events_out; |
---|
102 | return( _Thread_Executing->Wait.return_code ); |
---|
103 | } |
---|
104 | |
---|
105 | /*PAGE |
---|
106 | * |
---|
107 | * _Event_Seize |
---|
108 | * |
---|
109 | * This routine attempts to satisfy the requested event condition |
---|
110 | * for the running thread. |
---|
111 | * |
---|
112 | * Input parameters: |
---|
113 | * event_in - the event condition to satisfy |
---|
114 | * option_set - acquire event options |
---|
115 | * ticks - interval to wait |
---|
116 | * |
---|
117 | * Output parameters: NONE |
---|
118 | * |
---|
119 | * INTERRUPT LATENCY: |
---|
120 | * available |
---|
121 | * wait |
---|
122 | * check sync |
---|
123 | */ |
---|
124 | |
---|
125 | void _Event_Seize( |
---|
126 | rtems_event_set event_in, |
---|
127 | rtems_option option_set, |
---|
128 | rtems_interval ticks |
---|
129 | ) |
---|
130 | { |
---|
131 | Thread_Control *executing; |
---|
132 | rtems_event_set seized_events; |
---|
133 | rtems_event_set pending_events; |
---|
134 | ISR_Level level; |
---|
135 | |
---|
136 | executing = _Thread_Executing; |
---|
137 | executing->Wait.return_code = RTEMS_SUCCESSFUL; |
---|
138 | |
---|
139 | _ISR_Disable( level ); |
---|
140 | pending_events = executing->pending_events; |
---|
141 | seized_events = _Event_sets_Get( pending_events, event_in ); |
---|
142 | |
---|
143 | if ( !_Event_sets_Is_empty( seized_events ) && |
---|
144 | (seized_events == event_in || _Options_Is_any( option_set )) ) { |
---|
145 | executing->pending_events = |
---|
146 | _Event_sets_Clear( pending_events, seized_events ); |
---|
147 | _ISR_Enable( level ); |
---|
148 | executing->events_out = seized_events; |
---|
149 | return; |
---|
150 | } |
---|
151 | |
---|
152 | if ( _Options_Is_no_wait( option_set ) ) { |
---|
153 | _ISR_Enable( level ); |
---|
154 | executing->Wait.return_code = RTEMS_UNSATISFIED; |
---|
155 | executing->events_out = seized_events; |
---|
156 | return; |
---|
157 | } |
---|
158 | |
---|
159 | _Event_Sync = TRUE; |
---|
160 | executing->Wait.option_set = option_set; |
---|
161 | executing->Wait.Extra.event_condition = event_in; |
---|
162 | |
---|
163 | _ISR_Enable( level ); |
---|
164 | _Thread_Set_state( executing, STATES_WAITING_FOR_EVENT ); |
---|
165 | |
---|
166 | if ( ticks ) { |
---|
167 | _Watchdog_Initialize( |
---|
168 | &executing->Timer, |
---|
169 | _Event_Timeout, |
---|
170 | executing->Object.id, |
---|
171 | NULL |
---|
172 | ); |
---|
173 | _Watchdog_Insert_ticks( |
---|
174 | &executing->Timer, |
---|
175 | ticks, |
---|
176 | WATCHDOG_NO_ACTIVATE |
---|
177 | ); |
---|
178 | } |
---|
179 | |
---|
180 | _ISR_Disable( level ); |
---|
181 | if ( _Event_Sync == TRUE ) { |
---|
182 | _Event_Sync = FALSE; |
---|
183 | if ( ticks ) |
---|
184 | _Watchdog_Activate( &executing->Timer ); |
---|
185 | _ISR_Enable( level ); |
---|
186 | return; |
---|
187 | } |
---|
188 | _ISR_Enable( level ); |
---|
189 | (void) _Watchdog_Remove( &executing->Timer ); |
---|
190 | _Thread_Unblock( executing ); |
---|
191 | return; |
---|
192 | } |
---|
193 | |
---|
194 | /*PAGE |
---|
195 | * |
---|
196 | * _Event_Surrender |
---|
197 | * |
---|
198 | * This routines remove a thread from the specified threadq. |
---|
199 | * |
---|
200 | * Input parameters: |
---|
201 | * the_thread - pointer to thread to be dequeued |
---|
202 | * |
---|
203 | * Output parameters: NONE |
---|
204 | * |
---|
205 | * INTERRUPT LATENCY: |
---|
206 | * before flash |
---|
207 | * after flash |
---|
208 | * check sync |
---|
209 | */ |
---|
210 | |
---|
211 | void _Event_Surrender( |
---|
212 | Thread_Control *the_thread |
---|
213 | ) |
---|
214 | { |
---|
215 | ISR_Level level; |
---|
216 | rtems_event_set pending_events; |
---|
217 | rtems_event_set event_condition; |
---|
218 | rtems_event_set seized_events; |
---|
219 | |
---|
220 | _ISR_Disable( level ); |
---|
221 | pending_events = the_thread->pending_events; |
---|
222 | event_condition = the_thread->Wait.Extra.event_condition; |
---|
223 | |
---|
224 | seized_events = _Event_sets_Get( pending_events, event_condition ); |
---|
225 | |
---|
226 | if ( !_Event_sets_Is_empty( seized_events ) ) { |
---|
227 | if ( _States_Is_waiting_for_event( the_thread->current_state ) ) { |
---|
228 | if ( seized_events == event_condition || |
---|
229 | _Options_Is_any( the_thread->Wait.option_set ) ) { |
---|
230 | the_thread->pending_events = |
---|
231 | _Event_sets_Clear( pending_events, seized_events ); |
---|
232 | (rtems_event_set *)the_thread->events_out = seized_events; |
---|
233 | |
---|
234 | _ISR_Flash( level ); |
---|
235 | |
---|
236 | if ( !_Watchdog_Is_active( &the_thread->Timer ) ) { |
---|
237 | _ISR_Enable( level ); |
---|
238 | _Thread_Unblock( the_thread ); |
---|
239 | } |
---|
240 | else { |
---|
241 | _Watchdog_Deactivate( &the_thread->Timer ); |
---|
242 | _ISR_Enable( level ); |
---|
243 | (void) _Watchdog_Remove( &the_thread->Timer ); |
---|
244 | _Thread_Unblock( the_thread ); |
---|
245 | } |
---|
246 | return; |
---|
247 | } |
---|
248 | } |
---|
249 | else if ( _Thread_Is_executing( the_thread ) && _Event_Sync == TRUE ) { |
---|
250 | if ( seized_events == event_condition || |
---|
251 | _Options_Is_any( the_thread->Wait.option_set ) ) { |
---|
252 | the_thread->pending_events = |
---|
253 | _Event_sets_Clear( pending_events,seized_events ); |
---|
254 | (rtems_event_set *)the_thread->events_out = seized_events; |
---|
255 | _Event_Sync = FALSE; |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | _ISR_Enable( level ); |
---|
260 | } |
---|
261 | |
---|
262 | /*PAGE |
---|
263 | * |
---|
264 | * _Event_Timeout |
---|
265 | * |
---|
266 | * This routine processes a thread which timeouts while waiting to |
---|
267 | * receive an event_set. It is called by the watchdog handler. |
---|
268 | * |
---|
269 | * Input parameters: |
---|
270 | * id - thread id |
---|
271 | * |
---|
272 | * Output parameters: NONE |
---|
273 | */ |
---|
274 | |
---|
275 | void _Event_Timeout( |
---|
276 | Objects_Id id, |
---|
277 | void *ignored |
---|
278 | ) |
---|
279 | { |
---|
280 | Thread_Control *the_thread; |
---|
281 | Objects_Locations location; |
---|
282 | |
---|
283 | the_thread = _Thread_Get( id, &location ); |
---|
284 | switch ( location ) { |
---|
285 | case OBJECTS_ERROR: |
---|
286 | case OBJECTS_REMOTE: /* impossible */ |
---|
287 | break; |
---|
288 | case OBJECTS_LOCAL: |
---|
289 | the_thread->Wait.return_code = RTEMS_TIMEOUT; |
---|
290 | _Thread_Unblock( the_thread ); |
---|
291 | _Thread_Unnest_dispatch(); |
---|
292 | break; |
---|
293 | } |
---|
294 | } |
---|