1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief System Generates Signal for process after realtime seconds elapsed |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * 3.4.1 Schedule Alarm, P1003.1b-1993, p. 79 |
---|
10 | */ |
---|
11 | |
---|
12 | /* COPYRIGHT (c) 1989-2007. |
---|
13 | * On-Line Applications Research Corporation (OAR). |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.com/license/LICENSE. |
---|
18 | */ |
---|
19 | |
---|
20 | #if HAVE_CONFIG_H |
---|
21 | #include "config.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <unistd.h> |
---|
25 | |
---|
26 | #include <rtems/posix/pthreadimpl.h> |
---|
27 | #include <rtems/posix/psignalimpl.h> |
---|
28 | #include <rtems/score/threaddispatch.h> |
---|
29 | #include <rtems/score/todimpl.h> |
---|
30 | #include <rtems/score/watchdogimpl.h> |
---|
31 | |
---|
32 | /* |
---|
33 | * _POSIX_signals_Alarm_TSR |
---|
34 | */ |
---|
35 | |
---|
36 | static void _POSIX_signals_Alarm_TSR( |
---|
37 | Objects_Id id __attribute__((unused)), |
---|
38 | void *argument __attribute__((unused)) |
---|
39 | ) |
---|
40 | { |
---|
41 | kill( getpid(), SIGALRM ); |
---|
42 | /* XXX can't print from an ISR, should this be fatal? */ |
---|
43 | } |
---|
44 | |
---|
45 | static Watchdog_Control _POSIX_signals_Alarm_timer = WATCHDOG_INITIALIZER( |
---|
46 | _POSIX_signals_Alarm_TSR, |
---|
47 | 0, |
---|
48 | NULL |
---|
49 | ); |
---|
50 | |
---|
51 | unsigned int alarm( |
---|
52 | unsigned int seconds |
---|
53 | ) |
---|
54 | { |
---|
55 | unsigned int remaining = 0; |
---|
56 | Watchdog_Control *the_timer; |
---|
57 | Watchdog_States state; |
---|
58 | |
---|
59 | the_timer = &_POSIX_signals_Alarm_timer; |
---|
60 | |
---|
61 | _Thread_Disable_dispatch(); |
---|
62 | |
---|
63 | state = _Watchdog_Remove( the_timer ); |
---|
64 | if ( (state == WATCHDOG_ACTIVE) || (state == WATCHDOG_REMOVE_IT) ) { |
---|
65 | /* |
---|
66 | * The stop_time and start_time fields are snapshots of ticks since |
---|
67 | * boot. Since alarm() is dealing in seconds, we must account for |
---|
68 | * this. |
---|
69 | */ |
---|
70 | |
---|
71 | remaining = the_timer->initial - |
---|
72 | ((the_timer->stop_time - the_timer->start_time) / TOD_TICKS_PER_SECOND); |
---|
73 | } |
---|
74 | |
---|
75 | if ( seconds ) |
---|
76 | _Watchdog_Insert_seconds( the_timer, seconds ); |
---|
77 | |
---|
78 | _Thread_Enable_dispatch(); |
---|
79 | |
---|
80 | return remaining; |
---|
81 | } |
---|