source: rtems/cpukit/posix/src/nanosleep.c @ 709594f0

5
Last change on this file since 709594f0 was 709594f0, checked in by Gedare Bloom <gedare@…>, on 08/01/16 at 20:54:18

posix: nanosleep: adjust elapsed time calculation

Use clock_gettime before and after sleep to calculate
the time spent asleep, and the amount of time remaining.

updates #2732

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