1 | /* |
---|
2 | * Timer 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/support.h> |
---|
18 | #include <rtems/object.h> |
---|
19 | #include <rtems/thread.h> |
---|
20 | #include <rtems/timer.h> |
---|
21 | #include <rtems/tod.h> |
---|
22 | #include <rtems/watchdog.h> |
---|
23 | |
---|
24 | /*PAGE |
---|
25 | * |
---|
26 | * _Timer_Manager_initialization |
---|
27 | * |
---|
28 | * This routine initializes all timer manager related data structures. |
---|
29 | * |
---|
30 | * Input parameters: |
---|
31 | * maximum_timers - number of timers to initialize |
---|
32 | * |
---|
33 | * Output parameters: NONE |
---|
34 | */ |
---|
35 | |
---|
36 | void _Timer_Manager_initialization( |
---|
37 | unsigned32 maximum_timers |
---|
38 | ) |
---|
39 | { |
---|
40 | _Objects_Initialize_information( |
---|
41 | &_Timer_Information, |
---|
42 | OBJECTS_RTEMS_TIMERS, |
---|
43 | FALSE, |
---|
44 | maximum_timers, |
---|
45 | sizeof( Timer_Control ), |
---|
46 | FALSE, |
---|
47 | RTEMS_MAXIMUM_NAME_LENGTH, |
---|
48 | FALSE |
---|
49 | ); |
---|
50 | } |
---|
51 | |
---|
52 | /*PAGE |
---|
53 | * |
---|
54 | * rtems_timer_create |
---|
55 | * |
---|
56 | * This directive creates a timer and performs some initialization. |
---|
57 | * |
---|
58 | * Input parameters: |
---|
59 | * name - timer name |
---|
60 | * id - pointer to timer id |
---|
61 | * |
---|
62 | * Output parameters: |
---|
63 | * id - timer id |
---|
64 | * RTEMS_SUCCESSFUL - if successful |
---|
65 | * error code - if unsuccessful |
---|
66 | */ |
---|
67 | |
---|
68 | rtems_status_code rtems_timer_create( |
---|
69 | rtems_name name, |
---|
70 | Objects_Id *id |
---|
71 | ) |
---|
72 | { |
---|
73 | Timer_Control *the_timer; |
---|
74 | |
---|
75 | if ( !rtems_is_name_valid( name ) ) |
---|
76 | return ( RTEMS_INVALID_NAME ); |
---|
77 | |
---|
78 | _Thread_Disable_dispatch(); /* to prevent deletion */ |
---|
79 | |
---|
80 | the_timer = _Timer_Allocate(); |
---|
81 | |
---|
82 | if ( !the_timer ) { |
---|
83 | _Thread_Enable_dispatch(); |
---|
84 | return( RTEMS_TOO_MANY ); |
---|
85 | } |
---|
86 | |
---|
87 | the_timer->the_class = TIMER_DORMANT; |
---|
88 | |
---|
89 | _Objects_Open( &_Timer_Information, &the_timer->Object, &name ); |
---|
90 | |
---|
91 | *id = the_timer->Object.id; |
---|
92 | _Thread_Enable_dispatch(); |
---|
93 | return( RTEMS_SUCCESSFUL ); |
---|
94 | } |
---|
95 | |
---|
96 | /*PAGE |
---|
97 | * |
---|
98 | * rtems_timer_ident |
---|
99 | * |
---|
100 | * This directive returns the system ID associated with |
---|
101 | * the timer name. |
---|
102 | * |
---|
103 | * Input parameters: |
---|
104 | * name - user defined message queue name |
---|
105 | * id - pointer to timer id |
---|
106 | * |
---|
107 | * Output parameters: |
---|
108 | * *id - message queue id |
---|
109 | * RTEMS_SUCCESSFUL - if successful |
---|
110 | * error code - if unsuccessful |
---|
111 | */ |
---|
112 | |
---|
113 | rtems_status_code rtems_timer_ident( |
---|
114 | rtems_name name, |
---|
115 | Objects_Id *id |
---|
116 | ) |
---|
117 | { |
---|
118 | return _Objects_Name_to_id( |
---|
119 | &_Timer_Information, |
---|
120 | &name, |
---|
121 | RTEMS_SEARCH_LOCAL_NODE, |
---|
122 | id |
---|
123 | ); |
---|
124 | } |
---|
125 | |
---|
126 | /*PAGE |
---|
127 | * |
---|
128 | * rtems_timer_cancel |
---|
129 | * |
---|
130 | * This directive allows a thread to cancel a timer. |
---|
131 | * |
---|
132 | * Input parameters: |
---|
133 | * id - timer id |
---|
134 | * |
---|
135 | * Output parameters: |
---|
136 | * RTEMS_SUCCESSFUL - if successful |
---|
137 | * error code - if unsuccessful |
---|
138 | */ |
---|
139 | |
---|
140 | rtems_status_code rtems_timer_cancel( |
---|
141 | Objects_Id id |
---|
142 | ) |
---|
143 | { |
---|
144 | Timer_Control *the_timer; |
---|
145 | Objects_Locations location; |
---|
146 | |
---|
147 | the_timer = _Timer_Get( id, &location ); |
---|
148 | switch ( location ) { |
---|
149 | case OBJECTS_ERROR: |
---|
150 | return( RTEMS_INVALID_ID ); |
---|
151 | case OBJECTS_REMOTE: /* should never return this */ |
---|
152 | return( RTEMS_INTERNAL_ERROR ); |
---|
153 | case OBJECTS_LOCAL: |
---|
154 | if ( !_Timer_Is_dormant_class( the_timer->the_class ) ) |
---|
155 | (void) _Watchdog_Remove( &the_timer->Ticker ); |
---|
156 | _Thread_Enable_dispatch(); |
---|
157 | return( RTEMS_SUCCESSFUL ); |
---|
158 | } |
---|
159 | |
---|
160 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
161 | } |
---|
162 | |
---|
163 | /*PAGE |
---|
164 | * |
---|
165 | * rtems_timer_delete |
---|
166 | * |
---|
167 | * This directive allows a thread to delete a timer. |
---|
168 | * |
---|
169 | * Input parameters: |
---|
170 | * id - timer id |
---|
171 | * |
---|
172 | * Output parameters: |
---|
173 | * RTEMS_SUCCESSFUL - if successful |
---|
174 | * error code - if unsuccessful |
---|
175 | */ |
---|
176 | |
---|
177 | rtems_status_code rtems_timer_delete( |
---|
178 | Objects_Id id |
---|
179 | ) |
---|
180 | { |
---|
181 | Timer_Control *the_timer; |
---|
182 | Objects_Locations location; |
---|
183 | |
---|
184 | the_timer = _Timer_Get( id, &location ); |
---|
185 | switch ( location ) { |
---|
186 | case OBJECTS_ERROR: |
---|
187 | return( RTEMS_INVALID_ID ); |
---|
188 | case OBJECTS_REMOTE: /* should never return this */ |
---|
189 | return( RTEMS_INTERNAL_ERROR ); |
---|
190 | case OBJECTS_LOCAL: |
---|
191 | _Objects_Close( &_Timer_Information, &the_timer->Object ); |
---|
192 | (void) _Watchdog_Remove( &the_timer->Ticker ); |
---|
193 | _Timer_Free( the_timer ); |
---|
194 | _Thread_Enable_dispatch(); |
---|
195 | return( RTEMS_SUCCESSFUL ); |
---|
196 | } |
---|
197 | |
---|
198 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
199 | } |
---|
200 | |
---|
201 | /*PAGE |
---|
202 | * |
---|
203 | * rtems_timer_fire_after |
---|
204 | * |
---|
205 | * This directive allows a thread to start a timer. |
---|
206 | * |
---|
207 | * Input parameters: |
---|
208 | * id - timer id |
---|
209 | * ticks - interval until routine is fired |
---|
210 | * routine - routine to schedule |
---|
211 | * |
---|
212 | * Output parameters: |
---|
213 | * RTEMS_SUCCESSFUL - if successful |
---|
214 | * error code - if unsuccessful |
---|
215 | */ |
---|
216 | |
---|
217 | rtems_status_code rtems_timer_fire_after( |
---|
218 | Objects_Id id, |
---|
219 | rtems_interval ticks, |
---|
220 | Timer_Service routine, |
---|
221 | void *user_data |
---|
222 | ) |
---|
223 | { |
---|
224 | Timer_Control *the_timer; |
---|
225 | Objects_Locations location; |
---|
226 | |
---|
227 | if ( ticks == 0 ) |
---|
228 | return( RTEMS_INVALID_NUMBER ); |
---|
229 | |
---|
230 | the_timer = _Timer_Get( id, &location ); |
---|
231 | switch ( location ) { |
---|
232 | case OBJECTS_ERROR: |
---|
233 | return( RTEMS_INVALID_ID ); |
---|
234 | case OBJECTS_REMOTE: /* should never return this */ |
---|
235 | return( RTEMS_INTERNAL_ERROR ); |
---|
236 | case OBJECTS_LOCAL: |
---|
237 | (void) _Watchdog_Remove( &the_timer->Ticker ); |
---|
238 | the_timer->the_class = TIMER_INTERVAL; |
---|
239 | _Watchdog_Initialize( &the_timer->Ticker, routine, id, user_data ); |
---|
240 | _Watchdog_Insert_ticks( &the_timer->Ticker, |
---|
241 | ticks, WATCHDOG_ACTIVATE_NOW ); |
---|
242 | _Thread_Enable_dispatch(); |
---|
243 | return( RTEMS_SUCCESSFUL ); |
---|
244 | } |
---|
245 | |
---|
246 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
247 | } |
---|
248 | |
---|
249 | /*PAGE |
---|
250 | * |
---|
251 | * rtems_timer_fire_when |
---|
252 | * |
---|
253 | * This directive allows a thread to start a timer. |
---|
254 | * |
---|
255 | * Input parameters: |
---|
256 | * id - timer id |
---|
257 | * wall_time - time of day to fire timer |
---|
258 | * routine - routine to schedule |
---|
259 | * |
---|
260 | * Output parameters: |
---|
261 | * RTEMS_SUCCESSFUL - if successful |
---|
262 | * error code - if unsuccessful |
---|
263 | */ |
---|
264 | |
---|
265 | rtems_status_code rtems_timer_fire_when( |
---|
266 | Objects_Id id, |
---|
267 | rtems_time_of_day *wall_time, |
---|
268 | Timer_Service routine, |
---|
269 | void *user_data |
---|
270 | ) |
---|
271 | { |
---|
272 | Timer_Control *the_timer; |
---|
273 | Objects_Locations location; |
---|
274 | rtems_status_code validate_status; |
---|
275 | rtems_interval seconds; |
---|
276 | |
---|
277 | if ( !_TOD_Is_set() ) |
---|
278 | return( RTEMS_NOT_DEFINED ); |
---|
279 | |
---|
280 | validate_status = _TOD_Validate( wall_time ); |
---|
281 | if ( !rtems_is_status_successful( validate_status ) ) |
---|
282 | return( validate_status ); |
---|
283 | |
---|
284 | seconds = _TOD_To_seconds( wall_time ); |
---|
285 | if ( seconds <= _TOD_Seconds_since_epoch ) |
---|
286 | return( RTEMS_INVALID_CLOCK ); |
---|
287 | |
---|
288 | the_timer = _Timer_Get( id, &location ); |
---|
289 | switch ( location ) { |
---|
290 | case OBJECTS_ERROR: |
---|
291 | return( RTEMS_INVALID_ID ); |
---|
292 | case OBJECTS_REMOTE: /* should never return this */ |
---|
293 | return( RTEMS_INTERNAL_ERROR ); |
---|
294 | case OBJECTS_LOCAL: |
---|
295 | (void) _Watchdog_Remove( &the_timer->Ticker ); |
---|
296 | the_timer->the_class = TIMER_TIME_OF_DAY; |
---|
297 | _Watchdog_Initialize( &the_timer->Ticker, routine, id, user_data ); |
---|
298 | _Watchdog_Insert_seconds( &the_timer->Ticker, |
---|
299 | seconds - _TOD_Seconds_since_epoch, WATCHDOG_ACTIVATE_NOW ); |
---|
300 | _Thread_Enable_dispatch(); |
---|
301 | return( RTEMS_SUCCESSFUL ); |
---|
302 | } |
---|
303 | |
---|
304 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
305 | } |
---|
306 | |
---|
307 | /*PAGE |
---|
308 | * |
---|
309 | * rtems_timer_reset |
---|
310 | * |
---|
311 | * This directive allows a thread to reset a timer. |
---|
312 | * |
---|
313 | * Input parameters: |
---|
314 | * id - timer id |
---|
315 | * |
---|
316 | * Output parameters: |
---|
317 | * RTEMS_SUCCESSFUL - if successful |
---|
318 | * error code - if unsuccessful |
---|
319 | */ |
---|
320 | |
---|
321 | rtems_status_code rtems_timer_reset( |
---|
322 | Objects_Id id |
---|
323 | ) |
---|
324 | { |
---|
325 | Timer_Control *the_timer; |
---|
326 | Objects_Locations location; |
---|
327 | |
---|
328 | the_timer = _Timer_Get( id, &location ); |
---|
329 | switch ( location ) { |
---|
330 | case OBJECTS_ERROR: |
---|
331 | return( RTEMS_INVALID_ID ); |
---|
332 | case OBJECTS_REMOTE: /* should never return this */ |
---|
333 | return( RTEMS_INTERNAL_ERROR ); |
---|
334 | case OBJECTS_LOCAL: |
---|
335 | if ( _Timer_Is_interval_class( the_timer->the_class ) ) { |
---|
336 | _Watchdog_Reset( &the_timer->Ticker ); |
---|
337 | _Thread_Enable_dispatch(); |
---|
338 | return( RTEMS_SUCCESSFUL ); |
---|
339 | } |
---|
340 | _Thread_Enable_dispatch(); |
---|
341 | return( RTEMS_NOT_DEFINED ); |
---|
342 | } |
---|
343 | |
---|
344 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
345 | } |
---|