1 | /* |
---|
2 | * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267 |
---|
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 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <time.h> |
---|
19 | #include <errno.h> |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/seterr.h> |
---|
23 | #include <rtems/score/thread.h> |
---|
24 | #include <rtems/score/tod.h> |
---|
25 | #include <rtems/posix/time.h> |
---|
26 | #include <rtems/posix/ptimer.h> |
---|
27 | #include <rtems/posix/timer.h> |
---|
28 | |
---|
29 | int timer_settime( |
---|
30 | timer_t timerid, |
---|
31 | int flags, |
---|
32 | const struct itimerspec *value, |
---|
33 | struct itimerspec *ovalue |
---|
34 | ) |
---|
35 | { |
---|
36 | POSIX_Timer_Control *ptimer; |
---|
37 | Objects_Locations location; |
---|
38 | bool activated; |
---|
39 | uint32_t initial_period; |
---|
40 | struct itimerspec normalize; |
---|
41 | |
---|
42 | if ( !value ) |
---|
43 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
44 | |
---|
45 | /* First, it verifies if the structure "value" is correct */ |
---|
46 | if ( ( value->it_value.tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) || |
---|
47 | ( value->it_value.tv_nsec < 0 ) || |
---|
48 | ( value->it_interval.tv_nsec >= TOD_NANOSECONDS_PER_SECOND) || |
---|
49 | ( value->it_interval.tv_nsec < 0 )) { |
---|
50 | /* The number of nanoseconds is not correct */ |
---|
51 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
52 | } |
---|
53 | |
---|
54 | if ( flags != TIMER_ABSTIME && flags != POSIX_TIMER_RELATIVE ) { |
---|
55 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
56 | } |
---|
57 | |
---|
58 | normalize = *value; |
---|
59 | |
---|
60 | /* Convert absolute to relative time */ |
---|
61 | if (flags == TIMER_ABSTIME) { |
---|
62 | struct timespec now; |
---|
63 | _TOD_Get( &now ); |
---|
64 | /* Check for seconds in the past */ |
---|
65 | if ( _Timespec_Greater_than( &now, &normalize.it_value ) ) |
---|
66 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
67 | _Timespec_Subtract( &now, &normalize.it_value, &normalize.it_value ); |
---|
68 | } |
---|
69 | |
---|
70 | /* If the function reaches this point, then it will be necessary to do |
---|
71 | * something with the structure of times of the timer: to stop, start |
---|
72 | * or start it again |
---|
73 | */ |
---|
74 | |
---|
75 | ptimer = _POSIX_Timer_Get( timerid, &location ); |
---|
76 | switch ( location ) { |
---|
77 | |
---|
78 | case OBJECTS_LOCAL: |
---|
79 | /* First, it verifies if the timer must be stopped */ |
---|
80 | if ( normalize.it_value.tv_sec == 0 && normalize.it_value.tv_nsec == 0 ) { |
---|
81 | /* Stop the timer */ |
---|
82 | (void) _Watchdog_Remove( &ptimer->Timer ); |
---|
83 | /* The old data of the timer are returned */ |
---|
84 | if ( ovalue ) |
---|
85 | *ovalue = ptimer->timer_data; |
---|
86 | /* The new data are set */ |
---|
87 | ptimer->timer_data = normalize; |
---|
88 | /* Indicates that the timer is created and stopped */ |
---|
89 | ptimer->state = POSIX_TIMER_STATE_CREATE_STOP; |
---|
90 | /* Returns with success */ |
---|
91 | _Thread_Enable_dispatch(); |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | /* Convert from seconds and nanoseconds to ticks */ |
---|
96 | ptimer->ticks = _Timespec_To_ticks( &value->it_interval ); |
---|
97 | initial_period = _Timespec_To_ticks( &normalize.it_value ); |
---|
98 | |
---|
99 | |
---|
100 | activated = _POSIX_Timer_Insert_helper( |
---|
101 | &ptimer->Timer, |
---|
102 | initial_period, |
---|
103 | ptimer->Object.id, |
---|
104 | _POSIX_Timer_TSR, |
---|
105 | ptimer |
---|
106 | ); |
---|
107 | if ( !activated ) |
---|
108 | return 0; |
---|
109 | |
---|
110 | /* The timer has been started and is running */ |
---|
111 | /* return the old ones in "ovalue" */ |
---|
112 | if ( ovalue ) |
---|
113 | *ovalue = ptimer->timer_data; |
---|
114 | ptimer->timer_data = normalize; |
---|
115 | |
---|
116 | /* Indicate that the time is running */ |
---|
117 | ptimer->state = POSIX_TIMER_STATE_CREATE_RUN; |
---|
118 | _TOD_Get( &ptimer->time ); |
---|
119 | _Thread_Enable_dispatch(); |
---|
120 | return 0; |
---|
121 | |
---|
122 | #if defined(RTEMS_MULTIPROCESSING) |
---|
123 | case OBJECTS_REMOTE: |
---|
124 | #endif |
---|
125 | case OBJECTS_ERROR: |
---|
126 | break; |
---|
127 | } |
---|
128 | |
---|
129 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
130 | } |
---|