source: rtems/cpukit/posix/src/clocknanosleep.c @ 5500232

Last change on this file since 5500232 was c3c4525, checked in by Sebastian Huber <sebastian.huber@…>, on 08/05/22 at 06:18:36

posix: Avoid dead code in clock_nanosleep()

This issue was reported by Coverity Scan for RTEMS:

CID 1507760: Control flow issues (DEADCODE)

Closes #4690.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup POSIXAPI
7 *
8 * @brief Suspends Execution of calling thread until Time elapses
9 */
10
11/*
12 *  COPYRIGHT (c) 1989-2015.
13 *  On-Line Applications Research Corporation (OAR).
14 *
15 *  Copyright (c) 2016. Gedare Bloom.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 *    notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 *    notice, this list of conditions and the following disclaimer in the
24 *    documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifdef HAVE_CONFIG_H
40#include "config.h"
41#endif
42
43#include <time.h>
44
45#include <rtems/score/threadimpl.h>
46#include <rtems/score/threadqimpl.h>
47#include <rtems/score/timespec.h>
48#include <rtems/score/timecounter.h>
49#include <rtems/score/watchdogimpl.h>
50#include <rtems/posix/posixapi.h>
51
52static Thread_queue_Control _Nanosleep_Pseudo_queue =
53  THREAD_QUEUE_INITIALIZER( "Nanosleep" );
54
55/*
56 * High Resolution Sleep with Specifiable Clock, IEEE Std 1003.1, 2001
57 */
58int clock_nanosleep(
59  clockid_t               clock_id,
60  int                     flags,
61  const struct timespec  *rqtp,
62  struct timespec        *rmtp
63)
64{
65  Thread_queue_Context queue_context;
66  bool                 absolute;
67  Thread_Control      *executing;
68  int                  eno;
69
70  if ( clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC ) {
71    return ENOTSUP;
72  }
73
74  _Thread_queue_Context_initialize( &queue_context );
75  _Thread_queue_Context_set_thread_state(
76    &queue_context,
77    STATES_WAITING_FOR_TIME | STATES_INTERRUPTIBLE_BY_SIGNAL
78  );
79
80  if ( ( flags & TIMER_ABSTIME ) != 0 ) {
81    absolute = true;
82    rmtp = NULL;
83  } else {
84    absolute = false;
85
86    /*
87     * A relative CLOCK_REALTIME time out shall not be affected by
88     * CLOCK_REALTIME changes through clock_settime().  Since our
89     * CLOCK_REALTIME is basically just CLOCK_MONOTONIC plus an offset, we can
90     * simply use the CLOCK_MONOTONIC watchdog for relative CLOCK_REALTIME time
91     * outs.
92     */
93    clock_id = CLOCK_MONOTONIC;
94  }
95
96  if ( clock_id == CLOCK_REALTIME ) {
97    _Thread_queue_Context_set_enqueue_timeout_realtime_timespec(
98      &queue_context,
99      rqtp,
100      absolute
101    );
102  } else {
103    _Thread_queue_Context_set_enqueue_timeout_monotonic_timespec(
104      &queue_context,
105      rqtp,
106      absolute
107    );
108  }
109
110  _Thread_queue_Acquire( &_Nanosleep_Pseudo_queue, &queue_context );
111  executing = _Thread_Executing;
112  _Thread_queue_Enqueue(
113    &_Nanosleep_Pseudo_queue.Queue,
114    &_Thread_queue_Operations_FIFO,
115    executing,
116    &queue_context
117  );
118  eno = _POSIX_Get_error_after_wait( executing );
119
120  if ( eno == ETIMEDOUT ) {
121    eno = 0;
122  }
123
124  if ( rmtp != NULL ) {
125#if defined( RTEMS_POSIX_API )
126    if ( eno == EINTR ) {
127      struct timespec actual_end;
128      struct timespec planned_end;
129
130      _Assert( clock_id == CLOCK_MONOTONIC );
131      _Timecounter_Nanouptime( &actual_end );
132
133      _Watchdog_Ticks_to_timespec(
134        executing->Timer.Watchdog.expire,
135        &planned_end
136      );
137
138      if ( _Timespec_Less_than( &actual_end, &planned_end ) ) {
139        _Timespec_Subtract( &actual_end, &planned_end, rmtp );
140      } else {
141        _Timespec_Set_to_zero( rmtp );
142      }
143    } else {
144      _Timespec_Set_to_zero( rmtp );
145    }
146#else
147    _Assert( eno != EINTR );
148    _Timespec_Set_to_zero( rmtp );
149#endif
150  }
151
152  return eno;
153}
Note: See TracBrowser for help on using the repository browser.