source: rtems/cpukit/rtems/src/ratemon.c @ 9863dbf

4.104.114.84.95
Last change on this file since 9863dbf was 9863dbf, checked in by Joel Sherrill <joel.sherrill@…>, on Aug 18, 1995 at 9:42:58 PM

+ Added object type field to object id.

+ Added name pointer to Object_Control.

+ Modified Object Open and Close to address name field.

+ Removed name as separate element from Thread and Proxy Control.

+ Added parameter "object class" to calls to Initialize Information

  • Property mode set to 100644
File size: 10.2 KB
Line 
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/isr.h>
18#include <rtems/object.h>
19#include <rtems/ratemon.h>
20#include <rtems/thread.h>
21
22/*PAGE
23 *
24 *  _Rate_monotonic_Manager_initialization
25 *
26 *  This routine initializes all Rate Monotonic Manager related
27 *  data structures.
28 *
29 *  Input parameters:
30 *    maximum_periods - number of periods timers to initialize
31 *
32 *  Output parameters:  NONE
33 *
34 *  NOTE: The Rate Monotonic Manager is built on top of the Watchdog
35 *        Handler.
36 */
37
38void _Rate_monotonic_Manager_initialization(
39  unsigned32 maximum_periods
40)
41{
42  _Objects_Initialize_information(
43     &_Rate_monotonic_Information,
44     OBJECTS_RTEMS_PERIODS,
45     FALSE,
46     maximum_periods,
47     sizeof( Rate_monotonic_Control )
48  );
49}
50
51/*PAGE
52 *
53 *  rtems_rate_monotonic_create
54 *
55 *  This directive creates a rate monotonic timer and performs
56 *  some initialization.
57 *
58 *  Input parameters:
59 *    name - name of period
60 *    id   - pointer to rate monotonic id
61 *
62 *  Output parameters:
63 *    id                - rate monotonic id
64 *    RTEMS_SUCCESSFUL - if successful
65 *    error code        - if unsuccessful
66 */
67
68rtems_status_code rtems_rate_monotonic_create(
69  Objects_Name  name,
70  Objects_Id   *id
71)
72{
73  Rate_monotonic_Control *the_period;
74
75  if ( !_Objects_Is_name_valid( name ) )
76    return( RTEMS_INVALID_NAME );
77
78  _Thread_Disable_dispatch();            /* to prevent deletion */
79
80  the_period = _Rate_monotonic_Allocate();
81
82  if ( !the_period ) {
83    _Thread_Enable_dispatch();
84    return( RTEMS_TOO_MANY );
85  }
86
87  the_period->owner = _Thread_Executing;
88  the_period->state = RATE_MONOTONIC_INACTIVE;
89
90  _Objects_Open( &_Rate_monotonic_Information, &the_period->Object, name );
91
92  *id = the_period->Object.id;
93  _Thread_Enable_dispatch();
94  return( RTEMS_SUCCESSFUL );
95}
96
97/*PAGE
98 *
99 *  rtems_rate_monotonic_ident
100 *
101 *  This directive returns the system ID associated with
102 *  the rate monotonic period name.
103 *
104 *  Input parameters:
105 *    name - user defined period name
106 *    id   - pointer to period id
107 *
108 *  Output parameters:
109 *    *id               - region id
110 *    RTEMS_SUCCESSFUL - if successful
111 *    error code        - if unsuccessful
112 */
113
114rtems_status_code rtems_rate_monotonic_ident(
115  Objects_Name  name,
116  Objects_Id   *id
117)
118{
119  return _Objects_Name_to_id(
120    &_Rate_monotonic_Information,
121    name,
122    RTEMS_SEARCH_LOCAL_NODE,
123    id
124  );
125}
126
127/*PAGE
128 *
129 *  rtems_rate_monotonic_cancel
130 *
131 *  This directive allows a thread to cancel a rate monotonic timer.
132 *
133 *  Input parameters:
134 *    id - rate monotonic id
135 *
136 *  Output parameters:
137 *    RTEMS_SUCCESSFUL - if successful and caller is not the owning thread
138 *    error code        - if unsuccessful
139 */
140
141rtems_status_code rtems_rate_monotonic_cancel(
142  Objects_Id id
143)
144{
145  Rate_monotonic_Control *the_period;
146  Objects_Locations              location;
147
148  the_period = _Rate_monotonic_Get( id, &location );
149  switch ( location ) {
150    case OBJECTS_ERROR:
151      return( RTEMS_INVALID_ID );
152    case OBJECTS_REMOTE:            /* should never return this */
153      return( RTEMS_INTERNAL_ERROR );
154    case OBJECTS_LOCAL:
155      if ( !_Thread_Is_executing( the_period->owner ) ) {
156        _Thread_Enable_dispatch();
157        return( RTEMS_NOT_OWNER_OF_RESOURCE );
158      }
159      (void) _Watchdog_Remove( &the_period->Timer );
160      the_period->state = RATE_MONOTONIC_INACTIVE;
161      _Thread_Enable_dispatch();
162      return( RTEMS_SUCCESSFUL );
163  }
164
165  return( RTEMS_INTERNAL_ERROR );   /* unreached - only to remove warnings */
166}
167
168/*PAGE
169 *
170 *  rtems_rate_monotonic_delete
171 *
172 *  This directive allows a thread to delete a rate monotonic timer.
173 *
174 *  Input parameters:
175 *    id - rate monotonic id
176 *
177 *  Output parameters:
178 *    RTEMS_SUCCESSFUL - if successful
179 *    error code        - if unsuccessful
180 */
181
182rtems_status_code rtems_rate_monotonic_delete(
183  Objects_Id id
184)
185{
186  Rate_monotonic_Control *the_period;
187  Objects_Locations              location;
188
189  the_period = _Rate_monotonic_Get( id, &location );
190  switch ( location ) {
191    case OBJECTS_ERROR:
192      return( RTEMS_INVALID_ID );
193    case OBJECTS_REMOTE:            /* should never return this */
194      return( RTEMS_INTERNAL_ERROR );
195    case OBJECTS_LOCAL:
196      _Objects_Close( &_Rate_monotonic_Information, &the_period->Object );
197      (void) _Watchdog_Remove( &the_period->Timer );
198      the_period->state = RATE_MONOTONIC_INACTIVE;
199      _Rate_monotonic_Free( the_period );
200      _Thread_Enable_dispatch();
201      return( RTEMS_SUCCESSFUL );
202  }
203
204  return( RTEMS_INTERNAL_ERROR );   /* unreached - only to remove warnings */
205}
206
207/*PAGE
208 *
209 *  rtems_rate_monotonic_period
210 *
211 *  This directive allows a thread to manipulate a rate monotonic timer.
212 *
213 *  Input parameters:
214 *    id     - rate monotonic id
215 *    length - length of period (in ticks)
216 *
217 *  Output parameters:
218 *    RTEMS_SUCCESSFUL - if successful
219 *    error code        - if unsuccessful
220 */
221
222rtems_status_code rtems_rate_monotonic_period(
223  Objects_Id        id,
224  rtems_interval length
225)
226{
227  Rate_monotonic_Control *the_period;
228  Objects_Locations       location;
229  rtems_status_code            return_value;
230
231  the_period = _Rate_monotonic_Get( id, &location );
232  switch ( location ) {
233    case OBJECTS_ERROR:
234      return( RTEMS_INVALID_ID );
235    case OBJECTS_REMOTE:            /* should never return this */
236      return( RTEMS_INTERNAL_ERROR );
237    case OBJECTS_LOCAL:
238      if ( !_Thread_Is_executing( the_period->owner ) ) {
239        _Thread_Enable_dispatch();
240        return( RTEMS_NOT_OWNER_OF_RESOURCE );
241      }
242
243      if ( length == RTEMS_PERIOD_STATUS ) {
244        switch ( the_period->state ) {
245          case RATE_MONOTONIC_INACTIVE:
246            return_value = RTEMS_NOT_DEFINED;
247            break;
248          case RATE_MONOTONIC_ACTIVE:
249            return_value = RTEMS_SUCCESSFUL;
250            break;
251          case RATE_MONOTONIC_EXPIRED:
252            return_value = RTEMS_TIMEOUT;
253            break;
254          default:              /* unreached -- only to remove warnings */
255            return_value = RTEMS_INTERNAL_ERROR;
256            break;
257        }
258        _Thread_Enable_dispatch();
259        return( return_value );
260      }
261
262      switch ( the_period->state ) {
263        case RATE_MONOTONIC_INACTIVE:
264          the_period->state = RATE_MONOTONIC_ACTIVE;
265          _Watchdog_Initialize(
266            &the_period->Timer,
267            _Rate_monotonic_Timeout,
268            id,
269            NULL
270          );
271          _Watchdog_Insert_ticks(
272                     &the_period->Timer, length, WATCHDOG_ACTIVATE_NOW );
273          _Thread_Enable_dispatch();
274          return( RTEMS_SUCCESSFUL );
275
276        case RATE_MONOTONIC_ACTIVE:
277/* following is and could be a critical section problem */
278          _Thread_Executing->Wait.id  = the_period->Object.id;
279          if ( _Rate_monotonic_Set_state( the_period ) ) {
280            _Thread_Enable_dispatch();
281            return( RTEMS_SUCCESSFUL );
282          }
283         /* has expired -- fall into next case */
284        case RATE_MONOTONIC_EXPIRED:
285          the_period->state = RATE_MONOTONIC_ACTIVE;
286          _Watchdog_Insert_ticks(
287                     &the_period->Timer, length, WATCHDOG_ACTIVATE_NOW );
288          _Thread_Enable_dispatch();
289          return( RTEMS_TIMEOUT );
290      }
291  }
292
293  return( RTEMS_INTERNAL_ERROR );   /* unreached - only to remove warnings */
294}
295
296/*PAGE
297 *
298 * _Rate_monotonic_Set_state
299 *
300 * This kernel routine sets the STATES_WAITING_FOR_PERIOD state in
301 * the running thread's tcb if the specified period has not expired.
302 * The ready chain is adjusted if necessary.
303 *
304 * Input parameters:
305 *   the_period - pointer to period control block
306 *
307 * Output parameters:
308 *   TRUE  - if blocked successfully for period
309 *   FALSE - if period has expired
310 *
311 *  INTERRUPT LATENCY:
312 *    delete node
313 *    priority map
314 *    select heir
315 */
316
317boolean _Rate_monotonic_Set_state(
318Rate_monotonic_Control *the_period
319)
320{
321  Thread_Control *executing;
322  Chain_Control  *ready;
323  ISR_Level              level;
324  States_Control         old_state;
325
326  executing = _Thread_Executing;
327  ready     = executing->ready;
328  _ISR_Disable( level );
329
330  old_state = executing->current_state;
331
332  if ( _Rate_monotonic_Is_expired( the_period ) ) {
333    _ISR_Enable( level );
334    return( FALSE );
335  }
336
337  executing->current_state =
338             _States_Set( STATES_WAITING_FOR_PERIOD, old_state );
339
340  if ( _States_Is_ready( old_state ) ) {
341    if ( _Chain_Has_only_one_node( ready ) ) {
342      _Chain_Initialize_empty( ready );
343      _Priority_Remove_from_bit_map( &executing->Priority_map );
344      _ISR_Flash( level );
345    } else {
346      _Chain_Extract_unprotected( &executing->Object.Node );
347      _ISR_Flash( level );
348    }
349
350    if ( _Thread_Is_heir( executing ) )
351      _Thread_Calculate_heir();
352
353    _Context_Switch_necessary = TRUE;
354  }
355
356  _ISR_Enable( level );
357  return( TRUE );
358}
359
360/*PAGE
361 *
362 *  _Rate_monotonic_Timeout
363 *
364 *  This routine processes a period ending.  If the owning thread
365 *  is waiting for the period, that thread is unblocked and the
366 *  period reinitiated.  Otherwise, the period is expired.
367 *  This routine is called by the watchdog handler.
368 *
369 *  Input parameters:
370 *    id - period id
371 *
372 *  Output parameters: NONE
373 */
374
375void _Rate_monotonic_Timeout(
376  Objects_Id  id,
377  void       *ignored
378)
379{
380  Rate_monotonic_Control *the_period;
381  Objects_Locations              location;
382  Thread_Control         *the_thread;
383
384  the_period = _Rate_monotonic_Get( id, &location );
385  switch ( location ) {
386    case OBJECTS_ERROR:
387    case OBJECTS_REMOTE:  /* impossible */
388      break;
389    case OBJECTS_LOCAL:
390      the_thread = the_period->owner;
391      if ( _States_Is_waiting_for_period( the_thread->current_state ) &&
392            the_thread->Wait.id == the_period->Object.id ) {
393        _Thread_Unblock( the_thread );
394        _Watchdog_Reset( &the_period->Timer );
395      }
396      else
397        the_period->state = RATE_MONOTONIC_EXPIRED;
398      _Thread_Unnest_dispatch();
399      break;
400  }
401}
402
Note: See TracBrowser for help on using the repository browser.