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