1 | /* |
---|
2 | * 3.4.1 Schedule Alarm, P1003.1b-1993, p. 79 |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2008. |
---|
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 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <pthread.h> |
---|
17 | /* #include <errno.h> */ |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/posix/pthread.h> |
---|
21 | #include <rtems/posix/psignalimpl.h> |
---|
22 | #include <rtems/posix/time.h> |
---|
23 | |
---|
24 | /* |
---|
25 | * _POSIX_signals_Ualarm_TSR |
---|
26 | */ |
---|
27 | |
---|
28 | static void _POSIX_signals_Ualarm_TSR( |
---|
29 | Objects_Id id __attribute__((unused)), |
---|
30 | void *argument __attribute__((unused)) |
---|
31 | ) |
---|
32 | { |
---|
33 | /* |
---|
34 | * Send a SIGALRM but if there is a problem, ignore it. |
---|
35 | * It's OK, there isn't a way this should fail. |
---|
36 | */ |
---|
37 | (void) kill( getpid(), SIGALRM ); |
---|
38 | |
---|
39 | /* |
---|
40 | * If the reset interval is non-zero, reschedule ourselves. |
---|
41 | */ |
---|
42 | _Watchdog_Reset( &_POSIX_signals_Ualarm_timer ); |
---|
43 | } |
---|
44 | |
---|
45 | useconds_t ualarm( |
---|
46 | useconds_t useconds, |
---|
47 | useconds_t interval |
---|
48 | ) |
---|
49 | { |
---|
50 | useconds_t remaining = 0; |
---|
51 | Watchdog_Control *the_timer; |
---|
52 | Watchdog_Interval ticks; |
---|
53 | struct timespec tp; |
---|
54 | |
---|
55 | the_timer = &_POSIX_signals_Ualarm_timer; |
---|
56 | |
---|
57 | /* |
---|
58 | * Initialize the timer used to implement alarm(). |
---|
59 | */ |
---|
60 | |
---|
61 | if ( !the_timer->routine ) { |
---|
62 | _Watchdog_Initialize( the_timer, _POSIX_signals_Ualarm_TSR, 0, NULL ); |
---|
63 | } else { |
---|
64 | Watchdog_States state; |
---|
65 | |
---|
66 | state = _Watchdog_Remove( the_timer ); |
---|
67 | if ( (state == WATCHDOG_ACTIVE) || (state == WATCHDOG_REMOVE_IT) ) { |
---|
68 | /* |
---|
69 | * The stop_time and start_time fields are snapshots of ticks since |
---|
70 | * boot. Since alarm() is dealing in seconds, we must account for |
---|
71 | * this. |
---|
72 | */ |
---|
73 | |
---|
74 | ticks = the_timer->initial; |
---|
75 | ticks -= (the_timer->stop_time - the_timer->start_time); |
---|
76 | /* remaining is now in ticks */ |
---|
77 | |
---|
78 | _Timespec_From_ticks( ticks, &tp ); |
---|
79 | remaining = tp.tv_sec * TOD_MICROSECONDS_PER_SECOND; |
---|
80 | remaining += tp.tv_nsec / 1000; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /* |
---|
85 | * If useconds is non-zero, then the caller wants to schedule |
---|
86 | * the alarm repeatedly at that interval. If the interval is |
---|
87 | * less than a single clock tick, then fudge it to a clock tick. |
---|
88 | */ |
---|
89 | if ( useconds ) { |
---|
90 | Watchdog_Interval ticks; |
---|
91 | |
---|
92 | tp.tv_sec = useconds / TOD_MICROSECONDS_PER_SECOND; |
---|
93 | tp.tv_nsec = (useconds % TOD_MICROSECONDS_PER_SECOND) * 1000; |
---|
94 | ticks = _Timespec_To_ticks( &tp ); |
---|
95 | if ( ticks == 0 ) |
---|
96 | ticks = 1; |
---|
97 | |
---|
98 | _Watchdog_Insert_ticks( the_timer, _Timespec_To_ticks( &tp ) ); |
---|
99 | } |
---|
100 | |
---|
101 | return remaining; |
---|
102 | } |
---|