summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/px_nanosleep.c
blob: 6d09e71f4a9244783bb43815251739deaaa30910 (plain)
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
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** POSIX wrapper for THREADX                                             */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/* Include necessary system files.  */

#include "tx_api.h"     /* Threadx API */
#include "pthread.h"    /* Posix API */
#include "px_int.h"     /* Posix helper functions */
#include <limits.h>

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*  nanosleep                                              PORTABLE C     */
/*                                                           6.2.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function shall cause the current thread to be suspended from   */
/*    execution until the time interval specified by the req argument has */
/*    elapsed.                                                            */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*   req                          The number of real-time (as opposed     */
/*                                to CPU-time) seconds and nanoseconds to */
/*                                suspend the calling thread.             */
/*   rem                          Points to a structure to receive the    */
/*                                remaining time if the function is       */
/*                                interrupted by a signal. This pointer   */
/*                                may be NULL.                            */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*   zero                         If the function returns because the     */
/*                                requested time has elapsed.             */
/*                                                                        */
/*   -1                           If this functions fails if req argument */
/*                                specified a nanosecond value greater    */
/*                                than or equal to 1 billion.             */
/*                                                                        */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_thread_sleep             ThreadX thread sleep service            */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
INT nanosleep(struct timespec *req, struct timespec *rem)
{

ULONG    timer_ticks;

    /* Check for valid inputs - the nanosecond value must be less than 1 billion
       and not roll over when converting to ThreadX timer ticks. */
    if ( (!req) || (req->tv_nsec > 999999999) || ((timer_ticks = (req->tv_sec * CPU_TICKS_PER_SECOND + req->tv_nsec/NANOSECONDS_IN_CPU_TICK)) < req->tv_sec) )
    {
        posix_errno = EINVAL;
        posix_set_pthread_errno(EINVAL);
        return(ERROR);
    }

    /* Add padding of 1 so that the thread will sleep no less than the specified time,
       except in the case that timer_ticks is ULONG_MAX */
    if(timer_ticks != ULONG_MAX)
    {
        timer_ticks = timer_ticks + 1;
    }

    /* Now call ThreadX thread sleep service. */
    tx_thread_sleep(timer_ticks);

    /* Sleep completed. */
    if (rem)
    {
        rem->tv_nsec =  0;
        rem->tv_sec = 0;
    }
    return(OK);
}