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 | clockid_t clock_id, |
---|
37 | uint64_t ticks, |
---|
38 | struct timespec *timeout, |
---|
39 | struct timespec *rmtp, |
---|
40 | Watchdog_Discipline discipline |
---|
41 | ) |
---|
42 | { |
---|
43 | Thread_queue_Context queue_context; |
---|
44 | struct timespec stop; |
---|
45 | int err; |
---|
46 | |
---|
47 | err = 0; |
---|
48 | |
---|
49 | _Thread_queue_Context_initialize( &queue_context ); |
---|
50 | _Thread_queue_Context_set_thread_state( |
---|
51 | &queue_context, |
---|
52 | STATES_WAITING_FOR_TIME | STATES_INTERRUPTIBLE_BY_SIGNAL |
---|
53 | ); |
---|
54 | _Thread_queue_Context_set_enqueue_do_nothing_extra( &queue_context ); |
---|
55 | |
---|
56 | if ( discipline == WATCHDOG_ABSOLUTE ) { |
---|
57 | _Thread_queue_Context_set_absolute_timeout( &queue_context, ticks ); |
---|
58 | } else { |
---|
59 | _Thread_queue_Context_set_relative_timeout( &queue_context, ticks ); |
---|
60 | } |
---|
61 | |
---|
62 | /* |
---|
63 | * Block for the desired amount of time |
---|
64 | */ |
---|
65 | _Thread_queue_Acquire( &_Nanosleep_Pseudo_queue, &queue_context ); |
---|
66 | _Thread_queue_Enqueue( |
---|
67 | &_Nanosleep_Pseudo_queue.Queue, |
---|
68 | &_Thread_queue_Operations_FIFO, |
---|
69 | _Thread_Executing, |
---|
70 | &queue_context |
---|
71 | ); |
---|
72 | |
---|
73 | clock_gettime( clock_id, &stop ); |
---|
74 | /* |
---|
75 | * If the user wants the time remaining, do the conversion. |
---|
76 | */ |
---|
77 | if ( _Timespec_Less_than( &stop, timeout ) ) { |
---|
78 | /* |
---|
79 | * If there is time remaining, then we were interrupted by a signal. |
---|
80 | */ |
---|
81 | err = EINTR; |
---|
82 | if ( rmtp != NULL ) { |
---|
83 | _Timespec_Subtract( &stop, timeout, rmtp ); |
---|
84 | } |
---|
85 | } else if ( rmtp != NULL ) { |
---|
86 | /* no time remaining */ |
---|
87 | _Timespec_Set_to_zero( rmtp ); |
---|
88 | } |
---|
89 | |
---|
90 | return err; |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | * A nanosleep for zero time is implemented as a yield. |
---|
95 | * This behavior is also beyond the POSIX specification but is |
---|
96 | * consistent with the RTEMS API and yields desirable behavior. |
---|
97 | */ |
---|
98 | static inline int nanosleep_yield( struct timespec *rmtp ) |
---|
99 | { |
---|
100 | /* |
---|
101 | * It is critical to obtain the executing thread after thread dispatching is |
---|
102 | * disabled on SMP configurations. |
---|
103 | */ |
---|
104 | Thread_Control *executing; |
---|
105 | Per_CPU_Control *cpu_self; |
---|
106 | |
---|
107 | executing = _Thread_Get_executing(); |
---|
108 | cpu_self = _Thread_Dispatch_disable(); |
---|
109 | _Thread_Yield( executing ); |
---|
110 | _Thread_Dispatch_direct( cpu_self ); |
---|
111 | if ( rmtp ) { |
---|
112 | rmtp->tv_sec = 0; |
---|
113 | rmtp->tv_nsec = 0; |
---|
114 | } |
---|
115 | return 0; |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | * 14.2.5 High Resolution Sleep, P1003.1b-1993, p. 269 |
---|
120 | */ |
---|
121 | int nanosleep( |
---|
122 | const struct timespec *rqtp, |
---|
123 | struct timespec *rmtp |
---|
124 | ) |
---|
125 | { |
---|
126 | int err; |
---|
127 | struct timespec now; |
---|
128 | uint64_t ticks; |
---|
129 | Watchdog_Interval relative_interval; |
---|
130 | |
---|
131 | /* |
---|
132 | * Return EINVAL if the delay interval is negative. |
---|
133 | * |
---|
134 | * NOTE: This behavior is beyond the POSIX specification. |
---|
135 | * FSU and GNU/Linux pthreads shares this behavior. |
---|
136 | */ |
---|
137 | if ( !_Timespec_Is_valid( rqtp ) ) { |
---|
138 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
139 | } |
---|
140 | |
---|
141 | relative_interval = _Timespec_To_ticks( rqtp ); |
---|
142 | if ( relative_interval == 0 ) |
---|
143 | return nanosleep_yield( rmtp ); |
---|
144 | |
---|
145 | /* CLOCK_REALTIME can be adjusted during the timeout, |
---|
146 | * so convert to an absolute timeout value and put the |
---|
147 | * thread on the WATCHDOG_ABSOLUTE threadq. */ |
---|
148 | err = clock_gettime( CLOCK_REALTIME, &now ); |
---|
149 | if ( err != 0 ) |
---|
150 | return -1; |
---|
151 | _Timespec_Add_to( &now, rqtp ); |
---|
152 | ticks = _Watchdog_Realtime_from_timespec( &now ); |
---|
153 | err = nanosleep_helper( CLOCK_REALTIME, |
---|
154 | ticks, |
---|
155 | &now, |
---|
156 | rmtp, |
---|
157 | WATCHDOG_ABSOLUTE |
---|
158 | ); |
---|
159 | if ( err != 0 ) { |
---|
160 | rtems_set_errno_and_return_minus_one( err ); |
---|
161 | } |
---|
162 | return 0; |
---|
163 | } |
---|
164 | |
---|
165 | /* |
---|
166 | * High Resolution Sleep with Specifiable Clock, IEEE Std 1003.1, 2001 |
---|
167 | */ |
---|
168 | int clock_nanosleep( |
---|
169 | clockid_t clock_id, |
---|
170 | int flags, |
---|
171 | const struct timespec *rqtp, |
---|
172 | struct timespec *rmtp |
---|
173 | ) |
---|
174 | { |
---|
175 | int err = 0; |
---|
176 | struct timespec timeout; |
---|
177 | uint64_t ticks; |
---|
178 | Watchdog_Interval relative_interval; |
---|
179 | TOD_Absolute_timeout_conversion_results status; |
---|
180 | |
---|
181 | if ( !_Timespec_Is_valid( rqtp ) ) |
---|
182 | return EINVAL; |
---|
183 | |
---|
184 | /* get relative ticks of the requested timeout */ |
---|
185 | if ( flags & TIMER_ABSTIME ) { |
---|
186 | /* See if absolute time already passed */ |
---|
187 | status = _TOD_Absolute_timeout_to_ticks(rqtp, clock_id, &relative_interval); |
---|
188 | if ( status == TOD_ABSOLUTE_TIMEOUT_INVALID ) |
---|
189 | return EINVAL; |
---|
190 | if ( status == TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST ) |
---|
191 | return 0; |
---|
192 | if ( status == TOD_ABSOLUTE_TIMEOUT_IS_NOW ) |
---|
193 | return nanosleep_yield( NULL ); |
---|
194 | rmtp = NULL; /* Do not touch rmtp when using absolute time */ |
---|
195 | timeout.tv_sec = 0; |
---|
196 | timeout.tv_nsec = 0; |
---|
197 | } else { |
---|
198 | /* prepare to convert relative ticks to absolute timeout */ |
---|
199 | err = clock_gettime( clock_id, &timeout ); |
---|
200 | if ( err != 0 ) |
---|
201 | return EINVAL; |
---|
202 | relative_interval = _Timespec_To_ticks( rqtp ); |
---|
203 | } |
---|
204 | if ( relative_interval == 0 ) |
---|
205 | return nanosleep_yield( rmtp ); |
---|
206 | |
---|
207 | _Timespec_Add_to( &timeout, rqtp ); |
---|
208 | if ( clock_id == CLOCK_REALTIME ) { |
---|
209 | ticks = _Watchdog_Realtime_from_timespec( &timeout ); |
---|
210 | err = nanosleep_helper( |
---|
211 | clock_id, |
---|
212 | ticks, |
---|
213 | &timeout, |
---|
214 | rmtp, |
---|
215 | WATCHDOG_ABSOLUTE |
---|
216 | ); |
---|
217 | } else if ( clock_id == CLOCK_MONOTONIC ) { |
---|
218 | /* use the WATCHDOG_RELATIVE to ignore changes in wall time */ |
---|
219 | err = nanosleep_helper( |
---|
220 | clock_id, |
---|
221 | relative_interval, |
---|
222 | &timeout, |
---|
223 | rmtp, |
---|
224 | WATCHDOG_RELATIVE |
---|
225 | ); |
---|
226 | } else { |
---|
227 | err = ENOTSUP; |
---|
228 | } |
---|
229 | return err; |
---|
230 | } |
---|