[36a63d78] | 1 | /* |
---|
| 2 | * Timer Manager - rtems_timer_initiate_server directive along with |
---|
| 3 | * the Timer Server Body and support routines |
---|
| 4 | * |
---|
| 5 | * COPYRIGHT (c) 1989-2002. |
---|
| 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.OARcorp.com/rtems/license.html. |
---|
| 11 | * |
---|
| 12 | * $Id$ |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #include <rtems/system.h> |
---|
| 16 | #include <rtems/rtems/status.h> |
---|
| 17 | #include <rtems/rtems/support.h> |
---|
| 18 | #include <rtems/score/object.h> |
---|
| 19 | #include <rtems/score/thread.h> |
---|
| 20 | #include <rtems/rtems/timer.h> |
---|
| 21 | #include <rtems/score/tod.h> |
---|
| 22 | #include <rtems/score/watchdog.h> |
---|
| 23 | |
---|
| 24 | #include <rtems/rtems/tasks.h> |
---|
[8560ed94] | 25 | #include <rtems/rtems/support.h> |
---|
[36a63d78] | 26 | #include <rtems/score/thread.h> |
---|
| 27 | |
---|
| 28 | /* |
---|
| 29 | * The following chains contain the list of interval timers that are |
---|
| 30 | * executed in the context of the Timer Server. |
---|
| 31 | * |
---|
| 32 | * NOTE: These are prototyped in rtems/timer/timer.h but since we |
---|
| 33 | * do not actually use them until after the Timer Server is |
---|
| 34 | * initiated, we can actually declare them here and avoid forcing |
---|
| 35 | * them into the minimum footprint. |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | Chain_Control _Timer_Ticks_chain; |
---|
| 39 | Chain_Control _Timer_Seconds_chain; |
---|
| 40 | |
---|
[422289e] | 41 | /* |
---|
| 42 | * These variables keep track of the last time the Timer Server actually |
---|
| 43 | * processed the chain. |
---|
| 44 | */ |
---|
| 45 | |
---|
| 46 | Watchdog_Interval _Timer_Server_seconds_last_time; |
---|
| 47 | Watchdog_Interval _Timer_Server_ticks_last_time; |
---|
| 48 | |
---|
[36a63d78] | 49 | /* |
---|
| 50 | * The timer used to control when the Timer Server wakes up to service |
---|
| 51 | * "when" timers. |
---|
| 52 | */ |
---|
| 53 | |
---|
| 54 | Watchdog_Control _Timer_Seconds_timer; |
---|
| 55 | |
---|
| 56 | /*PAGE |
---|
| 57 | * |
---|
| 58 | * _Timer_Server_body |
---|
| 59 | * |
---|
| 60 | * This is the server for task based timers. This task executes whenever |
---|
| 61 | * a task-based timer should fire. It services both "after" and "when" |
---|
| 62 | * timers. It is not created automatically but must be created explicitly |
---|
| 63 | * by the application before task-based timers may be initiated. |
---|
| 64 | * |
---|
| 65 | * Input parameters: |
---|
| 66 | * Ignored - the task argument is ignored |
---|
| 67 | * |
---|
| 68 | * Output parameters: NONE |
---|
| 69 | */ |
---|
| 70 | |
---|
| 71 | Thread _Timer_Server_body( |
---|
| 72 | unsigned32 ignored |
---|
| 73 | ) |
---|
| 74 | { |
---|
| 75 | /* |
---|
| 76 | * Initialize the "last time" markers to indicate the timer that |
---|
| 77 | * the server was initiated. |
---|
| 78 | */ |
---|
| 79 | |
---|
[422289e] | 80 | _Timer_Server_ticks_last_time = _Watchdog_Ticks_since_boot; |
---|
| 81 | _Timer_Server_seconds_last_time = _TOD_Seconds_since_epoch; |
---|
[36a63d78] | 82 | |
---|
| 83 | _Thread_Disable_dispatch(); |
---|
| 84 | while(1) { |
---|
| 85 | |
---|
| 86 | /* |
---|
| 87 | * Block until there is something to do. |
---|
| 88 | */ |
---|
| 89 | |
---|
| 90 | _Thread_Set_state( _Timer_Server, STATES_DELAYING ); |
---|
[894d01c] | 91 | _Timer_Server_reset_ticks_timer(); |
---|
| 92 | _Timer_Server_reset_seconds_timer(); |
---|
[36a63d78] | 93 | _Thread_Enable_dispatch(); |
---|
| 94 | |
---|
[894d01c] | 95 | /* |
---|
| 96 | * At this point, at least one of the timers this task relies |
---|
| 97 | * upon has fired. Stop them both while we process any outstanding |
---|
| 98 | * timers. Before we block, we will restart them. |
---|
| 99 | */ |
---|
| 100 | |
---|
| 101 | _Timer_Server_stop_ticks_timer(); |
---|
| 102 | _Timer_Server_stop_seconds_timer(); |
---|
[36a63d78] | 103 | |
---|
| 104 | /* |
---|
| 105 | * Disable dispatching while processing the timers since we want |
---|
| 106 | * to mimic the environment that non-task-based TSRs execute in. |
---|
| 107 | * This ensures that the primary difference is that _ISR_Nest_level |
---|
| 108 | * is 0 for task-based timers and non-zero for the others. |
---|
| 109 | */ |
---|
| 110 | |
---|
| 111 | _Thread_Disable_dispatch(); |
---|
[894d01c] | 112 | _Timer_Server_process_ticks_chain(); |
---|
| 113 | _Timer_Server_process_seconds_chain(); |
---|
[36a63d78] | 114 | } |
---|
[0b993a94] | 115 | |
---|
| 116 | return 0; /* unreached - only to remove warnings */ |
---|
[36a63d78] | 117 | } |
---|
| 118 | |
---|
| 119 | /*PAGE |
---|
| 120 | * |
---|
| 121 | * rtems_timer_initiate_server |
---|
| 122 | * |
---|
| 123 | * This directive creates and starts the server for task-based timers. |
---|
| 124 | * It must be invoked before any task-based timers can be initiated. |
---|
| 125 | * |
---|
| 126 | * Input parameters: |
---|
[1ad83eb] | 127 | * priority - timer server priority |
---|
[36a63d78] | 128 | * stack_size - stack size in bytes |
---|
[1ad83eb] | 129 | * attribute_set - timer server attributes |
---|
[36a63d78] | 130 | * |
---|
| 131 | * Output parameters: |
---|
| 132 | * RTEMS_SUCCESSFUL - if successful |
---|
| 133 | * error code - if unsuccessful |
---|
| 134 | */ |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | rtems_status_code rtems_timer_initiate_server( |
---|
[1ad83eb] | 138 | unsigned32 priority, |
---|
[36a63d78] | 139 | unsigned32 stack_size, |
---|
| 140 | rtems_attribute attribute_set |
---|
| 141 | ) |
---|
| 142 | { |
---|
[1ad83eb] | 143 | rtems_id id; |
---|
| 144 | rtems_status_code status; |
---|
| 145 | rtems_task_priority _priority; |
---|
| 146 | |
---|
| 147 | /* |
---|
| 148 | * Make sure the requested priority is valid. |
---|
| 149 | */ |
---|
| 150 | |
---|
| 151 | _priority = priority; |
---|
| 152 | if ( priority == RTEMS_TIMER_SERVER_DEFAULT_PRIORITY ) |
---|
| 153 | _priority = 0; |
---|
| 154 | else if ( !_RTEMS_tasks_Priority_is_valid( priority ) ) |
---|
| 155 | return RTEMS_INVALID_PRIORITY; |
---|
[36a63d78] | 156 | |
---|
| 157 | /* |
---|
| 158 | * Just to make sure the test versus create/start operation are atomic. |
---|
| 159 | */ |
---|
| 160 | |
---|
| 161 | _Thread_Disable_dispatch(); |
---|
| 162 | |
---|
| 163 | if ( _Timer_Server ) { |
---|
| 164 | _Thread_Enable_dispatch(); |
---|
| 165 | return RTEMS_INCORRECT_STATE; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /* |
---|
[1ad83eb] | 169 | * Create the Timer Server with the name the name of "TIME". The attribute |
---|
| 170 | * RTEMS_SYSTEM_TASK allows us to set a priority to 0 which will makes it |
---|
| 171 | * higher than any other task in the system. It can be viewed as a low |
---|
| 172 | * priority interrupt. It is also always NO_PREEMPT so it looks like |
---|
| 173 | * an interrupt to other tasks. |
---|
| 174 | * |
---|
| 175 | * We allow the user to override the default priority because the Timer |
---|
| 176 | * Server can invoke TSRs which must adhere to language run-time or |
---|
| 177 | * other library rules. For example, if using a TSR written in Ada the |
---|
| 178 | * Server should run at the same priority as the priority Ada task. |
---|
| 179 | * Otherwise, the priority ceiling for the mutex used to protect the |
---|
| 180 | * GNAT run-time is violated. |
---|
[36a63d78] | 181 | */ |
---|
| 182 | |
---|
| 183 | status = rtems_task_create( |
---|
[1ad83eb] | 184 | 0x4954454d, /* "TIME" */ |
---|
| 185 | _priority, /* create with priority 1 since 0 is illegal */ |
---|
[36a63d78] | 186 | stack_size, /* let user specify stack size */ |
---|
| 187 | RTEMS_NO_PREEMPT, /* no preempt is like an interrupt */ |
---|
[1ad83eb] | 188 | /* user may want floating point but we need */ |
---|
| 189 | /* system task specified for 0 priority */ |
---|
| 190 | attribute_set | RTEMS_SYSTEM_TASK, |
---|
[36a63d78] | 191 | &id /* get the id back */ |
---|
| 192 | ); |
---|
| 193 | if (status) { |
---|
| 194 | _Thread_Enable_dispatch(); |
---|
| 195 | return status; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | status = rtems_task_start( |
---|
| 199 | id, /* the id from create */ |
---|
| 200 | (rtems_task_entry) _Timer_Server_body, /* the timer server entry point */ |
---|
| 201 | 0 /* there is no argument */ |
---|
| 202 | ); |
---|
| 203 | if (status) { |
---|
| 204 | /* |
---|
| 205 | * One would expect a call to rtems_task_delete() here to clean up |
---|
| 206 | * but there is actually no way (in normal circumstances) that the |
---|
| 207 | * start can fail. The id and starting address are known to be |
---|
| 208 | * be good. If this service fails, something is weirdly wrong on the |
---|
| 209 | * target such as a stray write in an ISR or incorrect memory layout. |
---|
| 210 | */ |
---|
| 211 | _Thread_Enable_dispatch(); |
---|
| 212 | return status; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /* |
---|
| 216 | * We work with the TCB pointer, not the ID, so we need to convert |
---|
| 217 | * to a TCB pointer from here out. |
---|
| 218 | * |
---|
| 219 | * NOTE: Setting the pointer to the Timer Server TCB to a value other than |
---|
| 220 | * NULL indicates that task-based timer support is initialized. |
---|
| 221 | */ |
---|
| 222 | |
---|
| 223 | _Timer_Server = (Thread_Control *)_Objects_Get_local_object( |
---|
| 224 | &_RTEMS_tasks_Information, |
---|
| 225 | _Objects_Get_index(id) |
---|
| 226 | ); |
---|
| 227 | |
---|
| 228 | /* |
---|
| 229 | * Initialize the timer lists that the server will manage. |
---|
| 230 | */ |
---|
| 231 | |
---|
| 232 | _Chain_Initialize_empty( &_Timer_Ticks_chain ); |
---|
| 233 | _Chain_Initialize_empty( &_Timer_Seconds_chain ); |
---|
| 234 | |
---|
| 235 | /* |
---|
| 236 | * Initialize the timers that will be used to control when the |
---|
| 237 | * Timer Server wakes up and services the task-based timers. |
---|
| 238 | */ |
---|
| 239 | |
---|
| 240 | _Watchdog_Initialize( &_Timer_Server->Timer, _Thread_Delay_ended, id, NULL ); |
---|
| 241 | _Watchdog_Initialize( &_Timer_Seconds_timer, _Thread_Delay_ended, id, NULL ); |
---|
| 242 | |
---|
| 243 | _Thread_Enable_dispatch(); |
---|
| 244 | return RTEMS_SUCCESSFUL; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /*PAGE |
---|
| 248 | * |
---|
[894d01c] | 249 | * _Timer_Server_process_ticks_chain |
---|
[422289e] | 250 | * |
---|
| 251 | * This routine is responsible for adjusting the list of task-based |
---|
| 252 | * interval timers to reflect the passage of time. |
---|
| 253 | * |
---|
| 254 | * Input parameters: NONE |
---|
| 255 | * |
---|
| 256 | * Output parameters: NONE |
---|
| 257 | */ |
---|
| 258 | |
---|
[894d01c] | 259 | void _Timer_Server_process_ticks_chain(void) |
---|
[422289e] | 260 | { |
---|
| 261 | Watchdog_Interval snapshot; |
---|
| 262 | Watchdog_Interval ticks; |
---|
| 263 | |
---|
| 264 | snapshot = _Watchdog_Ticks_since_boot; |
---|
| 265 | if ( snapshot >= _Timer_Server_ticks_last_time ) |
---|
| 266 | ticks = snapshot - _Timer_Server_ticks_last_time; |
---|
| 267 | else |
---|
| 268 | ticks = (0xFFFFFFFF - _Timer_Server_ticks_last_time) + snapshot; |
---|
| 269 | |
---|
| 270 | _Timer_Server_ticks_last_time = snapshot; |
---|
| 271 | _Watchdog_Adjust( &_Timer_Ticks_chain, WATCHDOG_FORWARD, ticks ); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | /*PAGE |
---|
| 275 | * |
---|
[894d01c] | 276 | * _Timer_Server_process_seconds_chain |
---|
[422289e] | 277 | * |
---|
| 278 | * This routine is responsible for adjusting the list of task-based |
---|
| 279 | * time of day timers to reflect the passage of time. |
---|
| 280 | * |
---|
| 281 | * Input parameters: NONE |
---|
| 282 | * |
---|
| 283 | * Output parameters: NONE |
---|
| 284 | */ |
---|
| 285 | |
---|
[894d01c] | 286 | void _Timer_Server_process_seconds_chain(void) |
---|
[422289e] | 287 | { |
---|
| 288 | Watchdog_Interval snapshot; |
---|
| 289 | Watchdog_Interval ticks; |
---|
| 290 | |
---|
| 291 | /* |
---|
| 292 | * Process the seconds chain. Start by checking that the Time |
---|
| 293 | * of Day (TOD) has not been set backwards. If it has then |
---|
| 294 | * we want to adjust the _Timer_Seconds_chain to indicate this. |
---|
| 295 | */ |
---|
| 296 | |
---|
| 297 | snapshot = _TOD_Seconds_since_epoch; |
---|
| 298 | if ( snapshot > _Timer_Server_seconds_last_time ) { |
---|
| 299 | /* |
---|
| 300 | * This path is for normal forward movement and cases where the |
---|
| 301 | * TOD has been set forward. |
---|
| 302 | */ |
---|
| 303 | |
---|
| 304 | ticks = snapshot - _Timer_Server_seconds_last_time; |
---|
| 305 | _Watchdog_Adjust( &_Timer_Seconds_chain, WATCHDOG_FORWARD, ticks ); |
---|
| 306 | |
---|
| 307 | } else if ( snapshot < _Timer_Server_seconds_last_time ) { |
---|
| 308 | /* |
---|
| 309 | * The current TOD is before the last TOD which indicates that |
---|
| 310 | * TOD has been set backwards. |
---|
| 311 | */ |
---|
| 312 | |
---|
| 313 | ticks = _Timer_Server_seconds_last_time - snapshot; |
---|
| 314 | _Watchdog_Adjust( &_Timer_Seconds_chain, WATCHDOG_BACKWARD, ticks ); |
---|
| 315 | } |
---|
| 316 | _Timer_Server_seconds_last_time = snapshot; |
---|
| 317 | } |
---|
| 318 | |
---|