[ac7d5ef0] | 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> |
---|
[3a4ae6c] | 17 | #include <rtems/rtems/status.h> |
---|
| 18 | #include <rtems/rtems/support.h> |
---|
| 19 | #include <rtems/core/isr.h> |
---|
| 20 | #include <rtems/core/object.h> |
---|
| 21 | #include <rtems/rtems/ratemon.h> |
---|
| 22 | #include <rtems/core/thread.h> |
---|
[ac7d5ef0] | 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( |
---|
[3235ad9] | 45 | &_Rate_monotonic_Information, |
---|
| 46 | OBJECTS_RTEMS_PERIODS, |
---|
| 47 | FALSE, |
---|
| 48 | maximum_periods, |
---|
| 49 | sizeof( Rate_monotonic_Control ), |
---|
| 50 | FALSE, |
---|
[5250ff39] | 51 | RTEMS_MAXIMUM_NAME_LENGTH, |
---|
| 52 | FALSE |
---|
[ac7d5ef0] | 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( |
---|
[3235ad9] | 74 | rtems_name name, |
---|
[ac7d5ef0] | 75 | Objects_Id *id |
---|
| 76 | ) |
---|
| 77 | { |
---|
| 78 | Rate_monotonic_Control *the_period; |
---|
| 79 | |
---|
[3235ad9] | 80 | if ( !rtems_is_name_valid( name ) ) |
---|
[3a4ae6c] | 81 | return RTEMS_INVALID_NAME; |
---|
[ac7d5ef0] | 82 | |
---|
| 83 | _Thread_Disable_dispatch(); /* to prevent deletion */ |
---|
| 84 | |
---|
| 85 | the_period = _Rate_monotonic_Allocate(); |
---|
| 86 | |
---|
| 87 | if ( !the_period ) { |
---|
| 88 | _Thread_Enable_dispatch(); |
---|
[3a4ae6c] | 89 | return RTEMS_TOO_MANY; |
---|
[ac7d5ef0] | 90 | } |
---|
| 91 | |
---|
| 92 | the_period->owner = _Thread_Executing; |
---|
| 93 | the_period->state = RATE_MONOTONIC_INACTIVE; |
---|
| 94 | |
---|
[3235ad9] | 95 | _Objects_Open( &_Rate_monotonic_Information, &the_period->Object, &name ); |
---|
[ac7d5ef0] | 96 | |
---|
| 97 | *id = the_period->Object.id; |
---|
| 98 | _Thread_Enable_dispatch(); |
---|
[3a4ae6c] | 99 | return RTEMS_SUCCESSFUL; |
---|
[ac7d5ef0] | 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( |
---|
[3235ad9] | 120 | rtems_name name, |
---|
[ac7d5ef0] | 121 | Objects_Id *id |
---|
| 122 | ) |
---|
| 123 | { |
---|
[3a4ae6c] | 124 | Objects_Name_to_id_errors status; |
---|
| 125 | |
---|
| 126 | status = _Objects_Name_to_id( |
---|
[ac7d5ef0] | 127 | &_Rate_monotonic_Information, |
---|
[3235ad9] | 128 | &name, |
---|
[3a4ae6c] | 129 | OBJECTS_SEARCH_LOCAL_NODE, |
---|
[ac7d5ef0] | 130 | id |
---|
| 131 | ); |
---|
[3a4ae6c] | 132 | |
---|
| 133 | return _Status_Object_name_errors_to_status[ status ]; |
---|
[ac7d5ef0] | 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: |
---|
[3a4ae6c] | 160 | return RTEMS_INVALID_ID; |
---|
| 161 | case OBJECTS_REMOTE: |
---|
| 162 | return RTEMS_INTERNAL_ERROR; /* should never return this */ |
---|
[ac7d5ef0] | 163 | case OBJECTS_LOCAL: |
---|
| 164 | if ( !_Thread_Is_executing( the_period->owner ) ) { |
---|
| 165 | _Thread_Enable_dispatch(); |
---|
[3a4ae6c] | 166 | return RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
[ac7d5ef0] | 167 | } |
---|
| 168 | (void) _Watchdog_Remove( &the_period->Timer ); |
---|
| 169 | the_period->state = RATE_MONOTONIC_INACTIVE; |
---|
| 170 | _Thread_Enable_dispatch(); |
---|
[3a4ae6c] | 171 | return RTEMS_SUCCESSFUL; |
---|
[ac7d5ef0] | 172 | } |
---|
| 173 | |
---|
[3a4ae6c] | 174 | return RTEMS_INTERNAL_ERROR; /* unreached - only to remove warnings */ |
---|
[ac7d5ef0] | 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: |
---|
[3a4ae6c] | 201 | return RTEMS_INVALID_ID; |
---|
[ac7d5ef0] | 202 | case OBJECTS_REMOTE: /* should never return this */ |
---|
[3a4ae6c] | 203 | return RTEMS_INTERNAL_ERROR; |
---|
[ac7d5ef0] | 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(); |
---|
[3a4ae6c] | 210 | return RTEMS_SUCCESSFUL; |
---|
[ac7d5ef0] | 211 | } |
---|
| 212 | |
---|
[3a4ae6c] | 213 | return RTEMS_INTERNAL_ERROR; /* unreached - only to remove warnings */ |
---|
[ac7d5ef0] | 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, |
---|
[3a4ae6c] | 233 | rtems_interval length |
---|
[ac7d5ef0] | 234 | ) |
---|
| 235 | { |
---|
| 236 | Rate_monotonic_Control *the_period; |
---|
| 237 | Objects_Locations location; |
---|
[3a4ae6c] | 238 | rtems_status_code return_value; |
---|
[ac7d5ef0] | 239 | |
---|
| 240 | the_period = _Rate_monotonic_Get( id, &location ); |
---|
| 241 | switch ( location ) { |
---|
| 242 | case OBJECTS_ERROR: |
---|
[3a4ae6c] | 243 | return RTEMS_INVALID_ID; |
---|
[ac7d5ef0] | 244 | case OBJECTS_REMOTE: /* should never return this */ |
---|
[3a4ae6c] | 245 | return RTEMS_INTERNAL_ERROR; |
---|
[ac7d5ef0] | 246 | case OBJECTS_LOCAL: |
---|
| 247 | if ( !_Thread_Is_executing( the_period->owner ) ) { |
---|
| 248 | _Thread_Enable_dispatch(); |
---|
[3a4ae6c] | 249 | return RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
[ac7d5ef0] | 250 | } |
---|
| 251 | |
---|
| 252 | if ( length == RTEMS_PERIOD_STATUS ) { |
---|
| 253 | switch ( the_period->state ) { |
---|
| 254 | case RATE_MONOTONIC_INACTIVE: |
---|
| 255 | return_value = RTEMS_NOT_DEFINED; |
---|
| 256 | break; |
---|
| 257 | case RATE_MONOTONIC_ACTIVE: |
---|
| 258 | return_value = RTEMS_SUCCESSFUL; |
---|
| 259 | break; |
---|
| 260 | case RATE_MONOTONIC_EXPIRED: |
---|
| 261 | return_value = RTEMS_TIMEOUT; |
---|
| 262 | break; |
---|
| 263 | default: /* unreached -- only to remove warnings */ |
---|
| 264 | return_value = RTEMS_INTERNAL_ERROR; |
---|
| 265 | break; |
---|
| 266 | } |
---|
| 267 | _Thread_Enable_dispatch(); |
---|
| 268 | return( return_value ); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | switch ( the_period->state ) { |
---|
| 272 | case RATE_MONOTONIC_INACTIVE: |
---|
| 273 | the_period->state = RATE_MONOTONIC_ACTIVE; |
---|
| 274 | _Watchdog_Initialize( |
---|
| 275 | &the_period->Timer, |
---|
| 276 | _Rate_monotonic_Timeout, |
---|
| 277 | id, |
---|
| 278 | NULL |
---|
| 279 | ); |
---|
| 280 | _Watchdog_Insert_ticks( |
---|
| 281 | &the_period->Timer, length, WATCHDOG_ACTIVATE_NOW ); |
---|
| 282 | _Thread_Enable_dispatch(); |
---|
[3a4ae6c] | 283 | return RTEMS_SUCCESSFUL; |
---|
[ac7d5ef0] | 284 | |
---|
| 285 | case RATE_MONOTONIC_ACTIVE: |
---|
| 286 | /* following is and could be a critical section problem */ |
---|
| 287 | _Thread_Executing->Wait.id = the_period->Object.id; |
---|
| 288 | if ( _Rate_monotonic_Set_state( the_period ) ) { |
---|
| 289 | _Thread_Enable_dispatch(); |
---|
[3a4ae6c] | 290 | return RTEMS_SUCCESSFUL; |
---|
[ac7d5ef0] | 291 | } |
---|
| 292 | /* has expired -- fall into next case */ |
---|
| 293 | case RATE_MONOTONIC_EXPIRED: |
---|
| 294 | the_period->state = RATE_MONOTONIC_ACTIVE; |
---|
| 295 | _Watchdog_Insert_ticks( |
---|
| 296 | &the_period->Timer, length, WATCHDOG_ACTIVATE_NOW ); |
---|
| 297 | _Thread_Enable_dispatch(); |
---|
[3a4ae6c] | 298 | return RTEMS_TIMEOUT; |
---|
[ac7d5ef0] | 299 | } |
---|
| 300 | } |
---|
| 301 | |
---|
[3a4ae6c] | 302 | return RTEMS_INTERNAL_ERROR; /* unreached - only to remove warnings */ |
---|
[ac7d5ef0] | 303 | } |
---|
| 304 | |
---|
| 305 | /*PAGE |
---|
| 306 | * |
---|
| 307 | * _Rate_monotonic_Set_state |
---|
| 308 | * |
---|
| 309 | * This kernel routine sets the STATES_WAITING_FOR_PERIOD state in |
---|
| 310 | * the running thread's tcb if the specified period has not expired. |
---|
| 311 | * The ready chain is adjusted if necessary. |
---|
| 312 | * |
---|
| 313 | * Input parameters: |
---|
| 314 | * the_period - pointer to period control block |
---|
| 315 | * |
---|
| 316 | * Output parameters: |
---|
| 317 | * TRUE - if blocked successfully for period |
---|
| 318 | * FALSE - if period has expired |
---|
| 319 | * |
---|
| 320 | * INTERRUPT LATENCY: |
---|
| 321 | * delete node |
---|
| 322 | * priority map |
---|
| 323 | * select heir |
---|
| 324 | */ |
---|
| 325 | |
---|
| 326 | boolean _Rate_monotonic_Set_state( |
---|
| 327 | Rate_monotonic_Control *the_period |
---|
| 328 | ) |
---|
| 329 | { |
---|
| 330 | Thread_Control *executing; |
---|
| 331 | Chain_Control *ready; |
---|
| 332 | ISR_Level level; |
---|
| 333 | States_Control old_state; |
---|
| 334 | |
---|
| 335 | executing = _Thread_Executing; |
---|
| 336 | ready = executing->ready; |
---|
| 337 | _ISR_Disable( level ); |
---|
| 338 | |
---|
| 339 | old_state = executing->current_state; |
---|
| 340 | |
---|
| 341 | if ( _Rate_monotonic_Is_expired( the_period ) ) { |
---|
| 342 | _ISR_Enable( level ); |
---|
| 343 | return( FALSE ); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | executing->current_state = |
---|
| 347 | _States_Set( STATES_WAITING_FOR_PERIOD, old_state ); |
---|
| 348 | |
---|
| 349 | if ( _States_Is_ready( old_state ) ) { |
---|
| 350 | if ( _Chain_Has_only_one_node( ready ) ) { |
---|
| 351 | _Chain_Initialize_empty( ready ); |
---|
| 352 | _Priority_Remove_from_bit_map( &executing->Priority_map ); |
---|
| 353 | _ISR_Flash( level ); |
---|
| 354 | } else { |
---|
| 355 | _Chain_Extract_unprotected( &executing->Object.Node ); |
---|
| 356 | _ISR_Flash( level ); |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | if ( _Thread_Is_heir( executing ) ) |
---|
| 360 | _Thread_Calculate_heir(); |
---|
| 361 | |
---|
| 362 | _Context_Switch_necessary = TRUE; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | _ISR_Enable( level ); |
---|
| 366 | return( TRUE ); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | /*PAGE |
---|
| 370 | * |
---|
| 371 | * _Rate_monotonic_Timeout |
---|
| 372 | * |
---|
| 373 | * This routine processes a period ending. If the owning thread |
---|
| 374 | * is waiting for the period, that thread is unblocked and the |
---|
| 375 | * period reinitiated. Otherwise, the period is expired. |
---|
| 376 | * This routine is called by the watchdog handler. |
---|
| 377 | * |
---|
| 378 | * Input parameters: |
---|
| 379 | * id - period id |
---|
| 380 | * |
---|
| 381 | * Output parameters: NONE |
---|
| 382 | */ |
---|
| 383 | |
---|
| 384 | void _Rate_monotonic_Timeout( |
---|
| 385 | Objects_Id id, |
---|
| 386 | void *ignored |
---|
| 387 | ) |
---|
| 388 | { |
---|
| 389 | Rate_monotonic_Control *the_period; |
---|
| 390 | Objects_Locations location; |
---|
| 391 | Thread_Control *the_thread; |
---|
| 392 | |
---|
| 393 | the_period = _Rate_monotonic_Get( id, &location ); |
---|
| 394 | switch ( location ) { |
---|
| 395 | case OBJECTS_ERROR: |
---|
| 396 | case OBJECTS_REMOTE: /* impossible */ |
---|
| 397 | break; |
---|
| 398 | case OBJECTS_LOCAL: |
---|
| 399 | the_thread = the_period->owner; |
---|
| 400 | if ( _States_Is_waiting_for_period( the_thread->current_state ) && |
---|
| 401 | the_thread->Wait.id == the_period->Object.id ) { |
---|
| 402 | _Thread_Unblock( the_thread ); |
---|
| 403 | _Watchdog_Reset( &the_period->Timer ); |
---|
| 404 | } |
---|
| 405 | else |
---|
| 406 | the_period->state = RATE_MONOTONIC_EXPIRED; |
---|
| 407 | _Thread_Unnest_dispatch(); |
---|
| 408 | break; |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|