1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Suspends Execution of calling thread until Time elapses |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2015. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * Copyright (c) 2016. Gedare Bloom. |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #if HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <time.h> |
---|
24 | #include <errno.h> |
---|
25 | |
---|
26 | #include <rtems/seterr.h> |
---|
27 | #include <rtems/score/threadimpl.h> |
---|
28 | #include <rtems/score/threadqimpl.h> |
---|
29 | #include <rtems/score/timespec.h> |
---|
30 | #include <rtems/score/watchdogimpl.h> |
---|
31 | |
---|
32 | static Thread_queue_Control _Nanosleep_Pseudo_queue = |
---|
33 | THREAD_QUEUE_INITIALIZER( "Nanosleep" ); |
---|
34 | |
---|
35 | static inline int nanosleep_helper( |
---|
36 | uint64_t ticks, |
---|
37 | Watchdog_Interval relative_interval, |
---|
38 | struct timespec *rmtp, |
---|
39 | Watchdog_Discipline discipline |
---|
40 | ) |
---|
41 | { |
---|
42 | Thread_Control *executing; |
---|
43 | |
---|
44 | Watchdog_Interval start; |
---|
45 | Watchdog_Interval elapsed; |
---|
46 | |
---|
47 | executing = _Thread_Get_executing(); |
---|
48 | |
---|
49 | start = _Watchdog_Ticks_since_boot; |
---|
50 | |
---|
51 | /* |
---|
52 | * Block for the desired amount of time |
---|
53 | */ |
---|
54 | _Thread_queue_Enqueue( |
---|
55 | &_Nanosleep_Pseudo_queue, |
---|
56 | &_Thread_queue_Operations_FIFO, |
---|
57 | executing, |
---|
58 | STATES_DELAYING | STATES_INTERRUPTIBLE_BY_SIGNAL, |
---|
59 | ticks, |
---|
60 | discipline, |
---|
61 | 1 |
---|
62 | ); |
---|
63 | |
---|
64 | /* |
---|
65 | * Calculate the time that passed while we were sleeping and how |
---|
66 | * much remains from what we requested. |
---|
67 | */ |
---|
68 | elapsed = _Watchdog_Ticks_since_boot - start; |
---|
69 | if ( elapsed >= relative_interval ) |
---|
70 | relative_interval = 0; |
---|
71 | else |
---|
72 | relative_interval -= elapsed; |
---|
73 | |
---|
74 | /* |
---|
75 | * If the user wants the time remaining, do the conversion. |
---|
76 | */ |
---|
77 | if ( rmtp ) { |
---|
78 | _Timespec_From_ticks( relative_interval, rmtp ); |
---|
79 | } |
---|
80 | |
---|
81 | /* |
---|
82 | * Only when POSIX is enabled, can a sleep be interrupted. |
---|
83 | */ |
---|
84 | #if defined(RTEMS_POSIX_API) |
---|
85 | /* |
---|
86 | * If there is time remaining, then we were interrupted by a signal. |
---|
87 | */ |
---|
88 | if ( relative_interval ) |
---|
89 | return EINTR; |
---|
90 | #endif |
---|
91 | |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | /* |
---|
96 | * A nanosleep for zero time is implemented as a yield. |
---|
97 | * This behavior is also beyond the POSIX specification but is |
---|
98 | * consistent with the RTEMS API and yields desirable behavior. |
---|
99 | */ |
---|
100 | static inline int nanosleep_yield( struct timespec *rmtp ) |
---|
101 | { |
---|
102 | /* |
---|
103 | * It is critical to obtain the executing thread after thread dispatching is |
---|
104 | * disabled on SMP configurations. |
---|
105 | */ |
---|
106 | Thread_Control *executing; |
---|
107 | Per_CPU_Control *cpu_self; |
---|
108 | |
---|
109 | executing = _Thread_Get_executing(); |
---|
110 | cpu_self = _Thread_Dispatch_disable(); |
---|
111 | _Thread_Yield( executing ); |
---|
112 | _Thread_Dispatch_enable( cpu_self ); |
---|
113 | if ( rmtp ) { |
---|
114 | rmtp->tv_sec = 0; |
---|
115 | rmtp->tv_nsec = 0; |
---|
116 | } |
---|
117 | return 0; |
---|
118 | } |
---|
119 | |
---|
120 | /* |
---|
121 | * 14.2.5 High Resolution Sleep, P1003.1b-1993, p. 269 |
---|
122 | */ |
---|
123 | int nanosleep( |
---|
124 | const struct timespec *rqtp, |
---|
125 | struct timespec *rmtp |
---|
126 | ) |
---|
127 | { |
---|
128 | int err; |
---|
129 | struct timespec now; |
---|
130 | uint64_t ticks; |
---|
131 | Watchdog_Interval relative_interval; |
---|
132 | |
---|
133 | /* |
---|
134 | * Return EINVAL if the delay interval is negative. |
---|
135 | * |
---|
136 | * NOTE: This behavior is beyond the POSIX specification. |
---|
137 | * FSU and GNU/Linux pthreads shares this behavior. |
---|
138 | */ |
---|
139 | if ( !_Timespec_Is_valid( rqtp ) ) { |
---|
140 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
141 | } |
---|
142 | |
---|
143 | relative_interval = _Timespec_To_ticks( rqtp ); |
---|
144 | if ( relative_interval == 0 ) |
---|
145 | return nanosleep_yield( rmtp ); |
---|
146 | |
---|
147 | /* CLOCK_REALTIME can be adjusted during the timeout, |
---|
148 | * so convert to an absolute timeout value and put the |
---|
149 | * thread on the WATCHDOG_ABSOLUTE threadq. */ |
---|
150 | err = clock_gettime( CLOCK_REALTIME, &now ); |
---|
151 | if ( err != 0 ) |
---|
152 | return -1; |
---|
153 | _Timespec_Add_to( &now, rqtp ); |
---|
154 | ticks = _Watchdog_Ticks_from_timespec( &now ); |
---|
155 | err = nanosleep_helper(ticks, relative_interval, rmtp, WATCHDOG_ABSOLUTE ); |
---|
156 | if ( err != 0 ) { |
---|
157 | rtems_set_errno_and_return_minus_one( err ); |
---|
158 | } |
---|
159 | return 0; |
---|
160 | } |
---|
161 | |
---|
162 | /* |
---|
163 | * High Resolution Sleep with Specifiable Clock, IEEE Std 1003.1, 2001 |
---|
164 | */ |
---|
165 | int clock_nanosleep( |
---|
166 | clockid_t clock_id, |
---|
167 | int flags, |
---|
168 | const struct timespec *rqtp, |
---|
169 | struct timespec *rmtp |
---|
170 | ) |
---|
171 | { |
---|
172 | int err = 0; |
---|
173 | struct timespec now; |
---|
174 | uint64_t ticks; |
---|
175 | Watchdog_Interval relative_interval; |
---|
176 | struct timespec relative_ts; |
---|
177 | TOD_Absolute_timeout_conversion_results status; |
---|
178 | |
---|
179 | if ( !_Timespec_Is_valid( rqtp ) ) |
---|
180 | return EINVAL; |
---|
181 | |
---|
182 | /* get relative ticks of the requested timeout */ |
---|
183 | if ( flags & TIMER_ABSTIME ) { |
---|
184 | /* See if absolute time already passed */ |
---|
185 | status = _TOD_Absolute_timeout_to_ticks(rqtp, clock_id, &relative_interval); |
---|
186 | if ( status == TOD_ABSOLUTE_TIMEOUT_INVALID ) |
---|
187 | return EINVAL; |
---|
188 | if ( status == TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST ) |
---|
189 | return 0; |
---|
190 | if ( status == TOD_ABSOLUTE_TIMEOUT_IS_NOW ) |
---|
191 | return nanosleep_yield( NULL ); |
---|
192 | rmtp = NULL; /* Do not touch rmtp when using absolute time */ |
---|
193 | } else { |
---|
194 | relative_interval = _Timespec_To_ticks( rqtp ); |
---|
195 | } |
---|
196 | if ( relative_interval == 0 ) |
---|
197 | return nanosleep_yield( rmtp ); |
---|
198 | |
---|
199 | if ( clock_id == CLOCK_REALTIME ) { |
---|
200 | /* convert relative ticks to absolute timeout */ |
---|
201 | err = clock_gettime( CLOCK_REALTIME, &now ); |
---|
202 | if ( err != 0 ) |
---|
203 | return EINVAL; |
---|
204 | _Timespec_Add_to( &now, rqtp ); |
---|
205 | ticks = _Watchdog_Ticks_from_timespec( &now ); |
---|
206 | err = nanosleep_helper( ticks, relative_interval, rmtp, WATCHDOG_ABSOLUTE ); |
---|
207 | } else if ( clock_id == CLOCK_MONOTONIC ) { |
---|
208 | /* use the WATCHDOG_RELATIVE to ignore changes in wall time */ |
---|
209 | err = nanosleep_helper( |
---|
210 | relative_interval, |
---|
211 | relative_interval, |
---|
212 | rmtp, |
---|
213 | WATCHDOG_RELATIVE |
---|
214 | ); |
---|
215 | } else { |
---|
216 | err = ENOTSUP; |
---|
217 | } |
---|
218 | return err; |
---|
219 | } |
---|