summaryrefslogtreecommitdiffstats
path: root/cpukit/posix/src/nanosleep.c
blob: 5a3db2a22052ee5a019b2e3d33f981a697e40edd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
 * @file
 *
 * @brief Suspends Execution of calling thread until Time elapses
 * @ingroup POSIXAPI
 */

/*
 *  COPYRIGHT (c) 1989-2015.
 *  On-Line Applications Research Corporation (OAR).
 * 
 *  Copyright (c) 2016. Gedare Bloom.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <time.h>
#include <errno.h>

#include <rtems/seterr.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/threadqimpl.h>
#include <rtems/score/timespec.h>
#include <rtems/score/watchdogimpl.h>

static Thread_queue_Control _Nanosleep_Pseudo_queue =
  THREAD_QUEUE_INITIALIZER( "Nanosleep" );

static inline int nanosleep_helper(
  uint64_t              ticks,
  struct timespec      *rmtp,
  Watchdog_Discipline   discipline
)
{
  /*
   * It is critical to obtain the executing thread after thread dispatching is
   * disabled on SMP configurations.
   */
  Thread_Control  *executing;
  Per_CPU_Control *cpu_self;

  Watchdog_Interval  start;
  Watchdog_Interval  elapsed;

  executing = _Thread_Get_executing();

  /*
   *  A nanosleep for zero time is implemented as a yield.
   *  This behavior is also beyond the POSIX specification but is
   *  consistent with the RTEMS API and yields desirable behavior.
   */
  if ( !ticks ) {
    cpu_self = _Thread_Dispatch_disable();
      _Thread_Yield( executing );
    _Thread_Dispatch_enable( cpu_self );
    if ( rmtp ) {
       rmtp->tv_sec = 0;
       rmtp->tv_nsec = 0;
    }
    return 0;
  }

  start = _Watchdog_Ticks_since_boot;

  /*
   *  Block for the desired amount of time
   */
  _Thread_queue_Enqueue(
    &_Nanosleep_Pseudo_queue,
    &_Thread_queue_Operations_FIFO,
    executing,
    STATES_DELAYING | STATES_INTERRUPTIBLE_BY_SIGNAL,
    ticks,
    discipline,
    1
  );

  /*
   * Calculate the time that passed while we were sleeping and how
   * much remains from what we requested.
   */
  elapsed = _Watchdog_Ticks_since_boot - start;
  if ( elapsed >= ticks )
    ticks = 0;
  else
    ticks -= elapsed;

  /*
   * If the user wants the time remaining, do the conversion.
   */
  if ( rmtp ) {
    _Timespec_From_ticks( ticks, rmtp );
  }

  /*
   *  Only when POSIX is enabled, can a sleep be interrupted.
   */
  #if defined(RTEMS_POSIX_API)
    /*
     *  If there is time remaining, then we were interrupted by a signal.
     */
    if ( ticks )
      return EINTR;
  #endif

  return 0;
}
/*
 *  14.2.5 High Resolution Sleep, P1003.1b-1993, p. 269
 */
int nanosleep(
  const struct timespec  *rqtp,
  struct timespec        *rmtp
)
{
  int err;
  struct timespec timeout;
  uint64_t ticks;

  /*
   *  Return EINVAL if the delay interval is negative.
   *
   *  NOTE:  This behavior is beyond the POSIX specification.
   *         FSU and GNU/Linux pthreads shares this behavior.
   */
  if ( !_Timespec_Is_valid( rqtp ) )
    return EINVAL;

 /* CLOCK_REALTIME can be adjusted during the timeout,
  * so convert to an absolute timeout value and put the
  * thread on the WATCHDOG_ABSOLUTE threadq. */
  err = clock_gettime( CLOCK_REALTIME, &timeout );
  if ( err != 0 )
    return -1;

  _Timespec_Add_to( &timeout, rqtp );
  ticks = _Watchdog_Ticks_from_timespec( &timeout );
  err = nanosleep_helper(ticks, rmtp, WATCHDOG_ABSOLUTE );
  if ( err != 0 ) {
    rtems_set_errno_and_return_minus_one( err );
  }
  return 0;
}

/*
 * High Resolution Sleep with Specifiable Clock, IEEE Std 1003.1, 2001
 */
int clock_nanosleep(
  clockid_t               clock_id,
  int                     flags,
  const struct timespec  *rqtp,
  struct timespec        *rmtp
)
{
  int err = 0;
  struct timespec absolute_timeout;
  uint64_t ticks;
  Watchdog_Interval relative_ticks;
  TOD_Absolute_timeout_conversion_results status;

  /*
   *  Return EINVAL if the delay interval is negative.
   *
   *  NOTE:  This behavior is beyond the POSIX specification.
   *         FSU and GNU/Linux pthreads shares this behavior.
   */
  if ( !_Timespec_Is_valid( rqtp ) )
    return EINVAL;

  if ( flags & TIMER_ABSTIME ) {
    /* See if absolute time already passed */
    status = _TOD_Absolute_timeout_to_ticks(rqtp, clock_id, &relative_ticks);
    if ( status == TOD_ABSOLUTE_TIMEOUT_INVALID )
      return EINVAL;
    if ( status == TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST ||
        status == TOD_ABSOLUTE_TIMEOUT_IS_NOW ) {
      return 0;
    }
    rmtp = NULL; /* Do not touch rmtp when using absolute time */
  } else {
    relative_ticks = _Timespec_To_ticks(rqtp);
  }

  if ( clock_id == CLOCK_REALTIME ) {
    if ( flags & TIMER_ABSTIME ) {
      ticks = _Watchdog_Ticks_from_timespec(rqtp);
    } else {
      err = clock_gettime( CLOCK_REALTIME, &absolute_timeout );
      if ( err != 0 ) {
        return EINVAL;
      }
      _Timespec_Add_to( &absolute_timeout, rqtp );
      ticks = _Watchdog_Ticks_from_timespec( &absolute_timeout );
    }
    err = nanosleep_helper( ticks, rmtp, WATCHDOG_ABSOLUTE );
  } else if ( clock_id == CLOCK_MONOTONIC ) {
    /* use the WATCHDOG_RELATIVE to ignore changes in wall time */
    err = nanosleep_helper( relative_ticks, rmtp, WATCHDOG_RELATIVE );
  } else {
    err = ENOTSUP;
  }
  return err;
}