1 | /* |
---|
2 | * Rate Monotonic Manager |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * All rights assigned to U.S. Government, 1994. |
---|
8 | * |
---|
9 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
10 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
11 | * notice must appear in all copies of this file and its derivatives. |
---|
12 | * |
---|
13 | * $Id$ |
---|
14 | */ |
---|
15 | |
---|
16 | #include <rtems/system.h> |
---|
17 | #include <rtems/rtems/status.h> |
---|
18 | #include <rtems/rtems/support.h> |
---|
19 | #include <rtems/score/isr.h> |
---|
20 | #include <rtems/score/object.h> |
---|
21 | #include <rtems/rtems/ratemon.h> |
---|
22 | #include <rtems/score/thread.h> |
---|
23 | |
---|
24 | /*PAGE |
---|
25 | * |
---|
26 | * _Rate_monotonic_Manager_initialization |
---|
27 | * |
---|
28 | * This routine initializes all Rate Monotonic Manager related |
---|
29 | * data structures. |
---|
30 | * |
---|
31 | * Input parameters: |
---|
32 | * maximum_periods - number of periods timers to initialize |
---|
33 | * |
---|
34 | * Output parameters: NONE |
---|
35 | * |
---|
36 | * NOTE: The Rate Monotonic Manager is built on top of the Watchdog |
---|
37 | * Handler. |
---|
38 | */ |
---|
39 | |
---|
40 | void _Rate_monotonic_Manager_initialization( |
---|
41 | unsigned32 maximum_periods |
---|
42 | ) |
---|
43 | { |
---|
44 | _Objects_Initialize_information( |
---|
45 | &_Rate_monotonic_Information, |
---|
46 | OBJECTS_RTEMS_PERIODS, |
---|
47 | FALSE, |
---|
48 | maximum_periods, |
---|
49 | sizeof( Rate_monotonic_Control ), |
---|
50 | FALSE, |
---|
51 | RTEMS_MAXIMUM_NAME_LENGTH, |
---|
52 | FALSE |
---|
53 | ); |
---|
54 | } |
---|
55 | |
---|
56 | /*PAGE |
---|
57 | * |
---|
58 | * rtems_rate_monotonic_create |
---|
59 | * |
---|
60 | * This directive creates a rate monotonic timer and performs |
---|
61 | * some initialization. |
---|
62 | * |
---|
63 | * Input parameters: |
---|
64 | * name - name of period |
---|
65 | * id - pointer to rate monotonic id |
---|
66 | * |
---|
67 | * Output parameters: |
---|
68 | * id - rate monotonic id |
---|
69 | * RTEMS_SUCCESSFUL - if successful |
---|
70 | * error code - if unsuccessful |
---|
71 | */ |
---|
72 | |
---|
73 | rtems_status_code rtems_rate_monotonic_create( |
---|
74 | rtems_name name, |
---|
75 | Objects_Id *id |
---|
76 | ) |
---|
77 | { |
---|
78 | Rate_monotonic_Control *the_period; |
---|
79 | |
---|
80 | if ( !rtems_is_name_valid( name ) ) |
---|
81 | return RTEMS_INVALID_NAME; |
---|
82 | |
---|
83 | _Thread_Disable_dispatch(); /* to prevent deletion */ |
---|
84 | |
---|
85 | the_period = _Rate_monotonic_Allocate(); |
---|
86 | |
---|
87 | if ( !the_period ) { |
---|
88 | _Thread_Enable_dispatch(); |
---|
89 | return RTEMS_TOO_MANY; |
---|
90 | } |
---|
91 | |
---|
92 | the_period->owner = _Thread_Executing; |
---|
93 | the_period->state = RATE_MONOTONIC_INACTIVE; |
---|
94 | |
---|
95 | _Objects_Open( &_Rate_monotonic_Information, &the_period->Object, &name ); |
---|
96 | |
---|
97 | *id = the_period->Object.id; |
---|
98 | _Thread_Enable_dispatch(); |
---|
99 | return RTEMS_SUCCESSFUL; |
---|
100 | } |
---|
101 | |
---|
102 | /*PAGE |
---|
103 | * |
---|
104 | * rtems_rate_monotonic_ident |
---|
105 | * |
---|
106 | * This directive returns the system ID associated with |
---|
107 | * the rate monotonic period name. |
---|
108 | * |
---|
109 | * Input parameters: |
---|
110 | * name - user defined period name |
---|
111 | * id - pointer to period id |
---|
112 | * |
---|
113 | * Output parameters: |
---|
114 | * *id - region id |
---|
115 | * RTEMS_SUCCESSFUL - if successful |
---|
116 | * error code - if unsuccessful |
---|
117 | */ |
---|
118 | |
---|
119 | rtems_status_code rtems_rate_monotonic_ident( |
---|
120 | rtems_name name, |
---|
121 | Objects_Id *id |
---|
122 | ) |
---|
123 | { |
---|
124 | Objects_Name_to_id_errors status; |
---|
125 | |
---|
126 | status = _Objects_Name_to_id( |
---|
127 | &_Rate_monotonic_Information, |
---|
128 | &name, |
---|
129 | OBJECTS_SEARCH_LOCAL_NODE, |
---|
130 | id |
---|
131 | ); |
---|
132 | |
---|
133 | return _Status_Object_name_errors_to_status[ status ]; |
---|
134 | } |
---|
135 | |
---|
136 | /*PAGE |
---|
137 | * |
---|
138 | * rtems_rate_monotonic_cancel |
---|
139 | * |
---|
140 | * This directive allows a thread to cancel a rate monotonic timer. |
---|
141 | * |
---|
142 | * Input parameters: |
---|
143 | * id - rate monotonic id |
---|
144 | * |
---|
145 | * Output parameters: |
---|
146 | * RTEMS_SUCCESSFUL - if successful and caller is not the owning thread |
---|
147 | * error code - if unsuccessful |
---|
148 | */ |
---|
149 | |
---|
150 | rtems_status_code rtems_rate_monotonic_cancel( |
---|
151 | Objects_Id id |
---|
152 | ) |
---|
153 | { |
---|
154 | Rate_monotonic_Control *the_period; |
---|
155 | Objects_Locations location; |
---|
156 | |
---|
157 | the_period = _Rate_monotonic_Get( id, &location ); |
---|
158 | switch ( location ) { |
---|
159 | case OBJECTS_ERROR: |
---|
160 | return RTEMS_INVALID_ID; |
---|
161 | case OBJECTS_REMOTE: |
---|
162 | return RTEMS_INTERNAL_ERROR; /* should never return this */ |
---|
163 | case OBJECTS_LOCAL: |
---|
164 | if ( !_Thread_Is_executing( the_period->owner ) ) { |
---|
165 | _Thread_Enable_dispatch(); |
---|
166 | return RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
167 | } |
---|
168 | (void) _Watchdog_Remove( &the_period->Timer ); |
---|
169 | the_period->state = RATE_MONOTONIC_INACTIVE; |
---|
170 | _Thread_Enable_dispatch(); |
---|
171 | return RTEMS_SUCCESSFUL; |
---|
172 | } |
---|
173 | |
---|
174 | return RTEMS_INTERNAL_ERROR; /* unreached - only to remove warnings */ |
---|
175 | } |
---|
176 | |
---|
177 | /*PAGE |
---|
178 | * |
---|
179 | * rtems_rate_monotonic_delete |
---|
180 | * |
---|
181 | * This directive allows a thread to delete a rate monotonic timer. |
---|
182 | * |
---|
183 | * Input parameters: |
---|
184 | * id - rate monotonic id |
---|
185 | * |
---|
186 | * Output parameters: |
---|
187 | * RTEMS_SUCCESSFUL - if successful |
---|
188 | * error code - if unsuccessful |
---|
189 | */ |
---|
190 | |
---|
191 | rtems_status_code rtems_rate_monotonic_delete( |
---|
192 | Objects_Id id |
---|
193 | ) |
---|
194 | { |
---|
195 | Rate_monotonic_Control *the_period; |
---|
196 | Objects_Locations location; |
---|
197 | |
---|
198 | the_period = _Rate_monotonic_Get( id, &location ); |
---|
199 | switch ( location ) { |
---|
200 | case OBJECTS_ERROR: |
---|
201 | return RTEMS_INVALID_ID; |
---|
202 | case OBJECTS_REMOTE: /* should never return this */ |
---|
203 | return RTEMS_INTERNAL_ERROR; |
---|
204 | case OBJECTS_LOCAL: |
---|
205 | _Objects_Close( &_Rate_monotonic_Information, &the_period->Object ); |
---|
206 | (void) _Watchdog_Remove( &the_period->Timer ); |
---|
207 | the_period->state = RATE_MONOTONIC_INACTIVE; |
---|
208 | _Rate_monotonic_Free( the_period ); |
---|
209 | _Thread_Enable_dispatch(); |
---|
210 | return RTEMS_SUCCESSFUL; |
---|
211 | } |
---|
212 | |
---|
213 | return RTEMS_INTERNAL_ERROR; /* unreached - only to remove warnings */ |
---|
214 | } |
---|
215 | |
---|
216 | /*PAGE |
---|
217 | * |
---|
218 | * rtems_rate_monotonic_period |
---|
219 | * |
---|
220 | * This directive allows a thread to manipulate a rate monotonic timer. |
---|
221 | * |
---|
222 | * Input parameters: |
---|
223 | * id - rate monotonic id |
---|
224 | * length - length of period (in ticks) |
---|
225 | * |
---|
226 | * Output parameters: |
---|
227 | * RTEMS_SUCCESSFUL - if successful |
---|
228 | * error code - if unsuccessful |
---|
229 | */ |
---|
230 | |
---|
231 | rtems_status_code rtems_rate_monotonic_period( |
---|
232 | Objects_Id id, |
---|
233 | rtems_interval length |
---|
234 | ) |
---|
235 | { |
---|
236 | Rate_monotonic_Control *the_period; |
---|
237 | Objects_Locations location; |
---|
238 | rtems_status_code return_value; |
---|
239 | Rate_Monotonic_Period_states local_state; |
---|
240 | ISR_Level level; |
---|
241 | |
---|
242 | the_period = _Rate_monotonic_Get( id, &location ); |
---|
243 | switch ( location ) { |
---|
244 | case OBJECTS_ERROR: |
---|
245 | return RTEMS_INVALID_ID; |
---|
246 | case OBJECTS_REMOTE: /* should never return this */ |
---|
247 | return RTEMS_INTERNAL_ERROR; |
---|
248 | case OBJECTS_LOCAL: |
---|
249 | if ( !_Thread_Is_executing( the_period->owner ) ) { |
---|
250 | _Thread_Enable_dispatch(); |
---|
251 | return RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
252 | } |
---|
253 | |
---|
254 | if ( length == RTEMS_PERIOD_STATUS ) { |
---|
255 | switch ( the_period->state ) { |
---|
256 | case RATE_MONOTONIC_INACTIVE: |
---|
257 | return_value = RTEMS_NOT_DEFINED; |
---|
258 | break; |
---|
259 | case RATE_MONOTONIC_ACTIVE: |
---|
260 | return_value = RTEMS_SUCCESSFUL; |
---|
261 | break; |
---|
262 | case RATE_MONOTONIC_EXPIRED: |
---|
263 | return_value = RTEMS_TIMEOUT; |
---|
264 | break; |
---|
265 | default: /* unreached -- only to remove warnings */ |
---|
266 | return_value = RTEMS_INTERNAL_ERROR; |
---|
267 | break; |
---|
268 | } |
---|
269 | _Thread_Enable_dispatch(); |
---|
270 | return( return_value ); |
---|
271 | } |
---|
272 | |
---|
273 | _ISR_Disable( level ); |
---|
274 | switch ( the_period->state ) { |
---|
275 | case RATE_MONOTONIC_INACTIVE: |
---|
276 | _ISR_Enable( level ); |
---|
277 | the_period->state = RATE_MONOTONIC_ACTIVE; |
---|
278 | _Watchdog_Initialize( |
---|
279 | &the_period->Timer, |
---|
280 | _Rate_monotonic_Timeout, |
---|
281 | id, |
---|
282 | NULL |
---|
283 | ); |
---|
284 | _Watchdog_Insert_ticks( &the_period->Timer, length ); |
---|
285 | _Thread_Enable_dispatch(); |
---|
286 | return RTEMS_SUCCESSFUL; |
---|
287 | |
---|
288 | case RATE_MONOTONIC_ACTIVE: |
---|
289 | /* |
---|
290 | * This tells the _Rate_monotonic_Timeout that this task is |
---|
291 | * in the process of blocking on the period. |
---|
292 | */ |
---|
293 | |
---|
294 | the_period->state = RATE_MONOTONIC_OWNER_IS_BLOCKING; |
---|
295 | _ISR_Enable( level ); |
---|
296 | |
---|
297 | _Thread_Executing->Wait.id = the_period->Object.id; |
---|
298 | _Thread_Set_state( _Thread_Executing, STATES_WAITING_FOR_PERIOD ); |
---|
299 | |
---|
300 | /* |
---|
301 | * Did the watchdog timer expire while we were actually blocking |
---|
302 | * on it? |
---|
303 | */ |
---|
304 | |
---|
305 | _ISR_Disable( level ); |
---|
306 | local_state = the_period->state; |
---|
307 | the_period->state = RATE_MONOTONIC_ACTIVE; |
---|
308 | _ISR_Enable( level ); |
---|
309 | |
---|
310 | /* |
---|
311 | * If it did, then we want to unblock ourself and continue as |
---|
312 | * if nothing happen. The period was reset in the timeout routine. |
---|
313 | */ |
---|
314 | |
---|
315 | if ( local_state == RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING ) |
---|
316 | _Thread_Clear_state( _Thread_Executing, STATES_WAITING_FOR_PERIOD ); |
---|
317 | |
---|
318 | _Thread_Enable_dispatch(); |
---|
319 | return RTEMS_SUCCESSFUL; |
---|
320 | break; |
---|
321 | |
---|
322 | case RATE_MONOTONIC_EXPIRED: |
---|
323 | _ISR_Enable( level ); |
---|
324 | the_period->state = RATE_MONOTONIC_ACTIVE; |
---|
325 | _Watchdog_Insert_ticks( &the_period->Timer, length ); |
---|
326 | _Thread_Enable_dispatch(); |
---|
327 | return RTEMS_TIMEOUT; |
---|
328 | |
---|
329 | case RATE_MONOTONIC_OWNER_IS_BLOCKING: |
---|
330 | case RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING: |
---|
331 | /* |
---|
332 | * These should never happen. |
---|
333 | */ |
---|
334 | break; |
---|
335 | } |
---|
336 | } |
---|
337 | |
---|
338 | return RTEMS_INTERNAL_ERROR; /* unreached - only to remove warnings */ |
---|
339 | } |
---|
340 | |
---|
341 | /*PAGE |
---|
342 | * |
---|
343 | * _Rate_monotonic_Timeout |
---|
344 | * |
---|
345 | * This routine processes a period ending. If the owning thread |
---|
346 | * is waiting for the period, that thread is unblocked and the |
---|
347 | * period reinitiated. Otherwise, the period is expired. |
---|
348 | * This routine is called by the watchdog handler. |
---|
349 | * |
---|
350 | * Input parameters: |
---|
351 | * id - period id |
---|
352 | * |
---|
353 | * Output parameters: NONE |
---|
354 | */ |
---|
355 | |
---|
356 | void _Rate_monotonic_Timeout( |
---|
357 | Objects_Id id, |
---|
358 | void *ignored |
---|
359 | ) |
---|
360 | { |
---|
361 | Rate_monotonic_Control *the_period; |
---|
362 | Objects_Locations location; |
---|
363 | Thread_Control *the_thread; |
---|
364 | |
---|
365 | the_period = _Rate_monotonic_Get( id, &location ); |
---|
366 | switch ( location ) { |
---|
367 | case OBJECTS_ERROR: |
---|
368 | case OBJECTS_REMOTE: /* impossible */ |
---|
369 | break; |
---|
370 | case OBJECTS_LOCAL: |
---|
371 | the_thread = the_period->owner; |
---|
372 | if ( _States_Is_waiting_for_period( the_thread->current_state ) && |
---|
373 | the_thread->Wait.id == the_period->Object.id ) { |
---|
374 | _Thread_Unblock( the_thread ); |
---|
375 | _Watchdog_Reset( &the_period->Timer ); |
---|
376 | } else if ( the_period->state == RATE_MONOTONIC_OWNER_IS_BLOCKING ) { |
---|
377 | the_period->state = RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING; |
---|
378 | _Watchdog_Reset( &the_period->Timer ); |
---|
379 | } else |
---|
380 | the_period->state = RATE_MONOTONIC_EXPIRED; |
---|
381 | _Thread_Unnest_dispatch(); |
---|
382 | break; |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|