source: rtems/cpukit/posix/src/nanosleep.c @ 03b900d

5
Last change on this file since 03b900d was 03b900d, checked in by Sebastian Huber <sebastian.huber@…>, on 02/18/16 at 07:36:26

score: Replace watchdog handler implementation

Use a red-black tree instead of delta chains.

Close #2344.
Update #2554.
Update #2555.
Close #2606.

  • Property mode set to 100644
File size: 2.9 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 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <time.h>
22#include <errno.h>
23
24#include <rtems/seterr.h>
25#include <rtems/score/threadimpl.h>
26#include <rtems/score/threadqimpl.h>
27#include <rtems/score/timespec.h>
28#include <rtems/score/watchdogimpl.h>
29
30static Thread_queue_Control _Nanosleep_Pseudo_queue =
31  THREAD_QUEUE_FIFO_INITIALIZER( _Nanosleep_Pseudo_queue, "Nanosleep" );
32
33/*
34 *  14.2.5 High Resolution Sleep, P1003.1b-1993, p. 269
35 */
36int nanosleep(
37  const struct timespec  *rqtp,
38  struct timespec        *rmtp
39)
40{
41  /*
42   * It is critical to obtain the executing thread after thread dispatching is
43   * disabled on SMP configurations.
44   */
45  Thread_Control  *executing;
46  Per_CPU_Control *cpu_self;
47
48  Watchdog_Interval  ticks;
49  Watchdog_Interval  start;
50  Watchdog_Interval  elapsed;
51
52
53  /*
54   *  Return EINVAL if the delay interval is negative.
55   *
56   *  NOTE:  This behavior is beyond the POSIX specification.
57   *         FSU and GNU/Linux pthreads shares this behavior.
58   */
59  if ( !_Timespec_Is_valid( rqtp ) )
60    rtems_set_errno_and_return_minus_one( EINVAL );
61
62  /*
63   * Convert the timespec delay into the appropriate number of clock ticks.
64   */
65  ticks = _Timespec_To_ticks( rqtp );
66
67  executing = _Thread_Get_executing();
68
69  /*
70   *  A nanosleep for zero time is implemented as a yield.
71   *  This behavior is also beyond the POSIX specification but is
72   *  consistent with the RTEMS API and yields desirable behavior.
73   */
74  if ( !ticks ) {
75    cpu_self = _Thread_Dispatch_disable();
76      _Thread_Yield( executing );
77    _Thread_Dispatch_enable( cpu_self );
78    if ( rmtp ) {
79       rmtp->tv_sec = 0;
80       rmtp->tv_nsec = 0;
81    }
82    return 0;
83  }
84
85  start = _Watchdog_Ticks_since_boot;
86
87  /*
88   *  Block for the desired amount of time
89   */
90  _Thread_queue_Enqueue(
91    &_Nanosleep_Pseudo_queue,
92    executing,
93    STATES_DELAYING | STATES_INTERRUPTIBLE_BY_SIGNAL,
94    ticks,
95    0
96  );
97
98  /*
99   * Calculate the time that passed while we were sleeping and how
100   * much remains from what we requested.
101   */
102  elapsed = _Watchdog_Ticks_since_boot - start;
103  if ( elapsed >= ticks )
104    ticks = 0;
105  else
106    ticks -= elapsed;
107
108  /*
109   * If the user wants the time remaining, do the conversion.
110   */
111  if ( rmtp ) {
112    _Timespec_From_ticks( ticks, rmtp );
113  }
114
115  /*
116   *  Only when POSIX is enabled, can a sleep be interrupted.
117   */
118  #if defined(RTEMS_POSIX_API)
119    /*
120     *  If there is time remaining, then we were interrupted by a signal.
121     */
122    if ( ticks )
123      rtems_set_errno_and_return_minus_one( EINTR );
124  #endif
125
126  return 0;
127}
Note: See TracBrowser for help on using the repository browser.