source: rtems/cpukit/rtems/src/rtemstimer.c @ 98e4ebf

4.104.114.84.95
Last change on this file since 98e4ebf was 98e4ebf, checked in by Joel Sherrill <joel.sherrill@…>, on Oct 8, 1997 at 3:45:54 PM

Fixed typo in the pointer to the license terms.

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