1 | /* |
---|
2 | * XXX 3.4.1 Schedule Alarm, P1003.1b-1993, p. 79 |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2003. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <pthread.h> |
---|
19 | /* #include <errno.h> */ |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/posix/pthread.h> |
---|
23 | #include <rtems/posix/psignal.h> |
---|
24 | #include <rtems/posix/time.h> |
---|
25 | |
---|
26 | Watchdog_Control _POSIX_signals_Ualarm_timer; |
---|
27 | |
---|
28 | /*PAGE |
---|
29 | * |
---|
30 | * _POSIX_signals_Ualarm_TSR |
---|
31 | */ |
---|
32 | |
---|
33 | void _POSIX_signals_Ualarm_TSR( |
---|
34 | Objects_Id id, |
---|
35 | void *argument |
---|
36 | ) |
---|
37 | { |
---|
38 | int status; |
---|
39 | |
---|
40 | status = kill( getpid(), SIGALRM ); |
---|
41 | /* XXX can't print from an ISR, should this be fatal? */ |
---|
42 | } |
---|
43 | |
---|
44 | useconds_t ualarm( |
---|
45 | useconds_t useconds, |
---|
46 | useconds_t interval |
---|
47 | ) |
---|
48 | { |
---|
49 | useconds_t remaining = 0; |
---|
50 | Watchdog_Control *the_timer; |
---|
51 | Watchdog_Interval ticks; |
---|
52 | struct timespec tp; |
---|
53 | |
---|
54 | the_timer = &_POSIX_signals_Ualarm_timer; |
---|
55 | |
---|
56 | /* |
---|
57 | * Initialize the timer used to implement alarm(). |
---|
58 | */ |
---|
59 | |
---|
60 | if ( !the_timer->routine ) { |
---|
61 | _Watchdog_Initialize( the_timer, _POSIX_signals_Ualarm_TSR, 0, NULL ); |
---|
62 | } else { |
---|
63 | switch ( _Watchdog_Remove( the_timer ) ) { |
---|
64 | case WATCHDOG_INACTIVE: |
---|
65 | case WATCHDOG_BEING_INSERTED: |
---|
66 | break; |
---|
67 | |
---|
68 | case WATCHDOG_ACTIVE: |
---|
69 | case WATCHDOG_REMOVE_IT: |
---|
70 | /* |
---|
71 | * The stop_time and start_time fields are snapshots of ticks since |
---|
72 | * boot. Since alarm() is dealing in seconds, we must account for |
---|
73 | * this. |
---|
74 | */ |
---|
75 | |
---|
76 | |
---|
77 | ticks = the_timer->initial - |
---|
78 | ((the_timer->stop_time - the_timer->start_time) / |
---|
79 | _TOD_Ticks_per_second); |
---|
80 | |
---|
81 | _POSIX_Interval_to_timespec( ticks, &tp ); |
---|
82 | remaining = tp.tv_sec * TOD_MICROSECONDS_PER_SECOND; |
---|
83 | remaining += tp.tv_nsec / 1000; |
---|
84 | break; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | tp.tv_sec = useconds / TOD_MICROSECONDS_PER_SECOND; |
---|
89 | tp.tv_nsec = (useconds % TOD_MICROSECONDS_PER_SECOND) * 1000; |
---|
90 | ticks = _POSIX_Timespec_to_interval( &tp ); |
---|
91 | _Watchdog_Insert_ticks( the_timer, ticks ); |
---|
92 | |
---|
93 | return remaining; |
---|
94 | } |
---|