source: rtems/cpukit/posix/src/nanosleep.c @ 04da96c7

5
Last change on this file since 04da96c7 was 1e1a91ed, checked in by Sebastian Huber <sebastian.huber@…>, on 03/23/16 at 09:01:31

score: Remove Thread_queue_Queue::operations field

Remove the Thread_queue_Queue::operations field to reduce the size of
this structure. Add a thread queue operations parameter to the
_Thread_queue_First(), _Thread_queue_First_locked(),
_Thread_queue_Enqueue(), _Thread_queue_Dequeue() and
_Thread_queue_Flush() functions. This is a preparation patch to reduce
the size of several synchronization objects.

  • 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_INITIALIZER( "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    &_Thread_queue_Operations_FIFO,
93    executing,
94    STATES_DELAYING | STATES_INTERRUPTIBLE_BY_SIGNAL,
95    ticks,
96    0
97  );
98
99  /*
100   * Calculate the time that passed while we were sleeping and how
101   * much remains from what we requested.
102   */
103  elapsed = _Watchdog_Ticks_since_boot - start;
104  if ( elapsed >= ticks )
105    ticks = 0;
106  else
107    ticks -= elapsed;
108
109  /*
110   * If the user wants the time remaining, do the conversion.
111   */
112  if ( rmtp ) {
113    _Timespec_From_ticks( ticks, rmtp );
114  }
115
116  /*
117   *  Only when POSIX is enabled, can a sleep be interrupted.
118   */
119  #if defined(RTEMS_POSIX_API)
120    /*
121     *  If there is time remaining, then we were interrupted by a signal.
122     */
123    if ( ticks )
124      rtems_set_errno_and_return_minus_one( EINTR );
125  #endif
126
127  return 0;
128}
Note: See TracBrowser for help on using the repository browser.