summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/px_abs_time_to_rel_ticks.c
blob: 79a7b90f7fa9a63588334ca3881a3c5e69a51a10 (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
/***************************************************************************
 * 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 "time.h"
#include <limits.h>

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    posix_abs_time_to_rel_ticks                         PORTABLE C      */
/*                                                           6.1.7        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function converts the absolute time specified in a POSIX       */
/*    timespec structure into the relative number of timer ticks until    */
/*    that time will occur.                                               */
/*                                                                        */
/**************************************************************************/
ULONG posix_abs_time_to_rel_ticks(struct timespec *abs_timeout)
{
   ULONG current_ticks, ticks_ns, ticks_sec, timeout_ticks;

   current_ticks = tx_time_get();
   /* convert ns to ticks (will lose any ns < 1 tick) */
   ticks_ns = abs_timeout->tv_nsec / NANOSECONDS_IN_CPU_TICK;
   /*
    * if ns < 1 tick were lost, bump up to next tick so the delay is never
    * less than what was specified.
    */
   if (ticks_ns * NANOSECONDS_IN_CPU_TICK != abs_timeout->tv_nsec)
   {
      ++ticks_ns;
   }
   ticks_sec = (ULONG) (abs_timeout->tv_sec * CPU_TICKS_PER_SECOND);
   /* add in sec. ticks, subtract current ticks to get relative value. */
   timeout_ticks = ticks_sec + ticks_ns - current_ticks;
   /*
    * Unless a relative timeout of zero was specified, bump up 1 tick to
    * compensate for the fact that there is an unknown time between 0 and
    * < 1 tick until the next tick. We never want the delay to be less than
    * what was requested.
    */
   if (timeout_ticks != 0)
   {
      ++timeout_ticks;
   }
      /*
    * If the absolute time was in the past, then we need to set the
    * relative time to zero; otherwise, we get an essentially infinite timeout.
    */
   if (timeout_ticks > LONG_MAX)
   {
       timeout_ticks = 0;
   }

   return timeout_ticks;
}