source: rtems/cpukit/rtems/src/ratemon.c @ e8cdda4

4.104.114.84.95
Last change on this file since e8cdda4 was e8cdda4, checked in by Joel Sherrill <joel.sherrill@…>, on 04/25/97 at 18:13:49

Corrected by adding check for NULL address passed into get_status
directive.

  • Property mode set to 100644
File size: 12.2 KB
Line 
1/*
2 *  Rate Monotonic Manager
3 *
4 *
5 *  COPYRIGHT (c) 1989-1997.
6 *  On-Line Applications Research Corporation (OAR).
7 *  Copyright assigned to U.S. Government, 1994.
8 *
9 *  The license and distribution terms for this file may in
10 *  the file LICENSE in this distribution or at
11 *  http://www.OARcorp.com/rtems/license.html.
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
40void _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
73rtems_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
119rtems_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
150rtems_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
191rtems_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_get_status
219 *
220 *  This directive allows a thread to obtain status information on a
221 *  period.
222 *
223 *  Input parameters:
224 *    id     - rate monotonic id
225 *    status - pointer to status control block
226 *
227 *  Output parameters:
228 *    RTEMS_SUCCESSFUL - if successful
229 *    error code        - if unsuccessful
230 *
231 */
232
233rtems_status_code rtems_rate_monotonic_get_status(
234  Objects_Id                           id,
235  rtems_rate_monotonic_period_status  *status
236)
237{
238  Objects_Locations              location;
239  Rate_monotonic_Control        *the_period;
240
241  if ( status == NULL )
242    return RTEMS_INVALID_ADDRESS;
243
244  the_period = _Rate_monotonic_Get( id, &location );
245  switch ( location ) {
246    case OBJECTS_ERROR:
247      return RTEMS_INVALID_ID;
248    case OBJECTS_REMOTE:            /* should never return this */
249      return RTEMS_INTERNAL_ERROR;
250    case OBJECTS_LOCAL:
251      status->state = the_period->state;
252
253      if ( status->state == RATE_MONOTONIC_INACTIVE ) {
254        status->ticks_since_last_period = 0;
255        status->ticks_executed_since_last_period = 0;
256      } else {
257        status->ticks_since_last_period =
258          _Watchdog_Ticks_since_boot - the_period->time_at_period;
259
260        status->ticks_executed_since_last_period =
261          the_period->owner->ticks_executed -
262            the_period->owner_ticks_executed_at_period;
263      }
264
265      _Thread_Enable_dispatch();
266      return RTEMS_SUCCESSFUL;
267  }
268
269  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
270}
271
272
273/*PAGE
274 *
275 *  rtems_rate_monotonic_period
276 *
277 *  This directive allows a thread to manipulate a rate monotonic timer.
278 *
279 *  Input parameters:
280 *    id     - rate monotonic id
281 *    length - length of period (in ticks)
282 *
283 *  Output parameters:
284 *    RTEMS_SUCCESSFUL - if successful
285 *    error code        - if unsuccessful
286 */
287
288rtems_status_code rtems_rate_monotonic_period(
289  Objects_Id        id,
290  rtems_interval    length
291)
292{
293  Rate_monotonic_Control              *the_period;
294  Objects_Locations                    location;
295  rtems_status_code                    return_value;
296  rtems_rate_monotonic_period_states   local_state;
297  ISR_Level                            level;
298
299  the_period = _Rate_monotonic_Get( id, &location );
300  switch ( location ) {
301    case OBJECTS_ERROR:
302      return RTEMS_INVALID_ID;
303    case OBJECTS_REMOTE:            /* should never return this */
304      return RTEMS_INTERNAL_ERROR;
305    case OBJECTS_LOCAL:
306      if ( !_Thread_Is_executing( the_period->owner ) ) {
307        _Thread_Enable_dispatch();
308        return RTEMS_NOT_OWNER_OF_RESOURCE;
309      }
310
311      if ( length == RTEMS_PERIOD_STATUS ) {
312        switch ( the_period->state ) {
313          case RATE_MONOTONIC_INACTIVE:
314            return_value = RTEMS_NOT_DEFINED;
315            break;
316          case RATE_MONOTONIC_ACTIVE:
317            return_value = RTEMS_SUCCESSFUL;
318            break;
319          case RATE_MONOTONIC_EXPIRED:
320            return_value = RTEMS_TIMEOUT;
321            break;
322          default:              /* unreached -- only to remove warnings */
323            return_value = RTEMS_INTERNAL_ERROR;
324            break;
325        }
326        _Thread_Enable_dispatch();
327        return( return_value );
328      }
329
330      _ISR_Disable( level );
331      switch ( the_period->state ) {
332        case RATE_MONOTONIC_INACTIVE:
333          _ISR_Enable( level );
334          the_period->state = RATE_MONOTONIC_ACTIVE;
335          _Watchdog_Initialize(
336            &the_period->Timer,
337            _Rate_monotonic_Timeout,
338            id,
339            NULL
340          );
341
342          the_period->owner_ticks_executed_at_period =
343            _Thread_Executing->ticks_executed;
344
345          the_period->time_at_period = _Watchdog_Ticks_since_boot;
346
347          _Watchdog_Insert_ticks( &the_period->Timer, length );
348          _Thread_Enable_dispatch();
349          return RTEMS_SUCCESSFUL;
350
351        case RATE_MONOTONIC_ACTIVE:
352          /*
353           *  This tells the _Rate_monotonic_Timeout that this task is
354           *  in the process of blocking on the period.
355           */
356
357          the_period->state = RATE_MONOTONIC_OWNER_IS_BLOCKING;
358          _ISR_Enable( level );
359
360          _Thread_Executing->Wait.id = the_period->Object.id;
361          _Thread_Set_state( _Thread_Executing, STATES_WAITING_FOR_PERIOD );
362         
363          /*
364           *  Did the watchdog timer expire while we were actually blocking
365           *  on it?
366           */
367
368          _ISR_Disable( level );
369            local_state = the_period->state;
370            the_period->state = RATE_MONOTONIC_ACTIVE;
371          _ISR_Enable( level );
372
373          /*
374           *  If it did, then we want to unblock ourself and continue as
375           *  if nothing happen.  The period was reset in the timeout routine.
376           */
377
378          if ( local_state == RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING )
379            _Thread_Clear_state( _Thread_Executing, STATES_WAITING_FOR_PERIOD );
380
381          _Thread_Enable_dispatch();
382          return RTEMS_SUCCESSFUL;
383          break;
384
385        case RATE_MONOTONIC_EXPIRED:
386          _ISR_Enable( level );
387          the_period->state = RATE_MONOTONIC_ACTIVE;
388          the_period->owner_ticks_executed_at_period =
389            _Thread_Executing->ticks_executed;
390          the_period->time_at_period = _Watchdog_Ticks_since_boot;
391
392          _Watchdog_Insert_ticks( &the_period->Timer, length );
393          _Thread_Enable_dispatch();
394          return RTEMS_TIMEOUT;
395
396        case RATE_MONOTONIC_OWNER_IS_BLOCKING:
397        case RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING:
398          /*
399           *  These should never happen.
400           */
401          break;
402      }
403  }
404
405  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
406}
407
408/*PAGE
409 *
410 *  _Rate_monotonic_Timeout
411 *
412 *  This routine processes a period ending.  If the owning thread
413 *  is waiting for the period, that thread is unblocked and the
414 *  period reinitiated.  Otherwise, the period is expired.
415 *  This routine is called by the watchdog handler.
416 *
417 *  Input parameters:
418 *    id - period id
419 *
420 *  Output parameters: NONE
421 */
422
423void _Rate_monotonic_Timeout(
424  Objects_Id  id,
425  void       *ignored
426)
427{
428  Rate_monotonic_Control *the_period;
429  Objects_Locations       location;
430  Thread_Control         *the_thread;
431
432  the_period = _Rate_monotonic_Get( id, &location );
433  switch ( location ) {
434    case OBJECTS_ERROR:
435    case OBJECTS_REMOTE:  /* impossible */
436      break;
437    case OBJECTS_LOCAL:
438      the_thread = the_period->owner;
439      if ( _States_Is_waiting_for_period( the_thread->current_state ) &&
440            the_thread->Wait.id == the_period->Object.id ) {
441        _Thread_Unblock( the_thread );
442        the_period->owner_ticks_executed_at_period =
443          the_thread->ticks_executed;
444
445        the_period->time_at_period = _Watchdog_Ticks_since_boot;
446
447        _Watchdog_Reset( &the_period->Timer );
448      } else if ( the_period->state == RATE_MONOTONIC_OWNER_IS_BLOCKING ) {
449        the_period->state = RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING;
450        the_period->owner_ticks_executed_at_period =
451          the_thread->ticks_executed;
452
453        the_period->time_at_period = _Watchdog_Ticks_since_boot;
454        _Watchdog_Reset( &the_period->Timer );
455      } else
456        the_period->state = RATE_MONOTONIC_EXPIRED;
457      _Thread_Unnest_dispatch();
458      break;
459  }
460}
461
Note: See TracBrowser for help on using the repository browser.