source: rtems/cpukit/rtems/src/ratemonperiod.c @ 4008232

4.104.114.84.95
Last change on this file since 4008232 was 1095ec1, checked in by Ralf Corsepius <ralf.corsepius@…>, on 01/18/05 at 09:03:45

Include config.h.

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 *  Rate Monotonic Manager
3 *
4 *
5 *  COPYRIGHT (c) 1989-1999.
6 *  On-Line Applications Research Corporation (OAR).
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.rtems.com/license/LICENSE.
11 *
12 *  $Id$
13 */
14
15#if HAVE_CONFIG_H
16#include "config.h"
17#endif
18
19#include <rtems/system.h>
20#include <rtems/rtems/status.h>
21#include <rtems/rtems/support.h>
22#include <rtems/score/isr.h>
23#include <rtems/score/object.h>
24#include <rtems/rtems/ratemon.h>
25#include <rtems/score/thread.h>
26
27/*PAGE
28 *
29 *  rtems_rate_monotonic_period
30 *
31 *  This directive allows a thread to manipulate a rate monotonic timer.
32 *
33 *  Input parameters:
34 *    id     - rate monotonic id
35 *    length - length of period (in ticks)
36 *
37 *  Output parameters:
38 *    RTEMS_SUCCESSFUL - if successful
39 *    error code       - if unsuccessful
40 */
41
42rtems_status_code rtems_rate_monotonic_period(
43  Objects_Id        id,
44  rtems_interval    length
45)
46{
47  Rate_monotonic_Control              *the_period;
48  Objects_Locations                    location;
49  rtems_status_code                    return_value;
50  rtems_rate_monotonic_period_states   local_state;
51  ISR_Level                            level;
52
53  the_period = _Rate_monotonic_Get( id, &location );
54  switch ( location ) {
55    case OBJECTS_REMOTE:            /* should never return this */
56      return RTEMS_INTERNAL_ERROR;
57
58    case OBJECTS_ERROR:
59      return RTEMS_INVALID_ID;
60
61    case OBJECTS_LOCAL:
62      if ( !_Thread_Is_executing( the_period->owner ) ) {
63        _Thread_Enable_dispatch();
64        return RTEMS_NOT_OWNER_OF_RESOURCE;
65      }
66
67      if ( length == RTEMS_PERIOD_STATUS ) {
68        switch ( the_period->state ) {
69          case RATE_MONOTONIC_INACTIVE:
70            return_value = RTEMS_NOT_DEFINED;
71            break;
72          case RATE_MONOTONIC_ACTIVE:
73            return_value = RTEMS_SUCCESSFUL;
74            break;
75          case RATE_MONOTONIC_EXPIRED:
76            return_value = RTEMS_TIMEOUT;
77            break;
78          default:              /* unreached -- only to remove warnings */
79            return_value = RTEMS_INTERNAL_ERROR;
80            break;
81        }
82        _Thread_Enable_dispatch();
83        return( return_value );
84      }
85
86      _ISR_Disable( level );
87      switch ( the_period->state ) {
88        case RATE_MONOTONIC_INACTIVE:
89          _ISR_Enable( level );
90          the_period->state = RATE_MONOTONIC_ACTIVE;
91          _Watchdog_Initialize(
92            &the_period->Timer,
93            _Rate_monotonic_Timeout,
94            id,
95            NULL
96          );
97
98          the_period->owner_ticks_executed_at_period =
99            _Thread_Executing->ticks_executed;
100
101          the_period->time_at_period = _Watchdog_Ticks_since_boot;
102          the_period->next_length = length;
103
104          _Watchdog_Insert_ticks( &the_period->Timer, length );
105          _Thread_Enable_dispatch();
106          return RTEMS_SUCCESSFUL;
107
108        case RATE_MONOTONIC_ACTIVE:
109          /*
110           *  This tells the _Rate_monotonic_Timeout that this task is
111           *  in the process of blocking on the period and that we
112           *  may be changing the length of the next period.
113           */
114
115          the_period->state = RATE_MONOTONIC_OWNER_IS_BLOCKING;
116          the_period->next_length = length;
117
118          _ISR_Enable( level );
119
120          _Thread_Executing->Wait.id = the_period->Object.id;
121          _Thread_Set_state( _Thread_Executing, STATES_WAITING_FOR_PERIOD );
122
123          /*
124           *  Did the watchdog timer expire while we were actually blocking
125           *  on it?
126           */
127
128          _ISR_Disable( level );
129            local_state = the_period->state;
130            the_period->state = RATE_MONOTONIC_ACTIVE;
131          _ISR_Enable( level );
132
133          /*
134           *  If it did, then we want to unblock ourself and continue as
135           *  if nothing happen.  The period was reset in the timeout routine.
136           */
137
138          if ( local_state == RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING )
139            _Thread_Clear_state( _Thread_Executing, STATES_WAITING_FOR_PERIOD );
140
141          _Thread_Enable_dispatch();
142          return RTEMS_SUCCESSFUL;
143          break;
144
145        case RATE_MONOTONIC_EXPIRED:
146          _ISR_Enable( level );
147          the_period->state = RATE_MONOTONIC_ACTIVE;
148          the_period->owner_ticks_executed_at_period =
149            _Thread_Executing->ticks_executed;
150          the_period->time_at_period = _Watchdog_Ticks_since_boot;
151          the_period->next_length = length;
152
153          _Watchdog_Insert_ticks( &the_period->Timer, length );
154          _Thread_Enable_dispatch();
155          return RTEMS_TIMEOUT;
156
157        case RATE_MONOTONIC_OWNER_IS_BLOCKING:
158        case RATE_MONOTONIC_EXPIRED_WHILE_BLOCKING:
159          /*
160           *  These should never happen.
161           */
162          break;
163      }
164  }
165
166  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
167}
Note: See TracBrowser for help on using the repository browser.