source: rtems/c/src/exec/rtems/src/timer.c @ 88d594a

4.104.114.84.95
Last change on this file since 88d594a was 88d594a, checked in by Joel Sherrill <joel.sherrill@…>, on 05/24/95 at 21:39:42

Fully tested on all in-house targets

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