clock_abstime2ticks.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /****************************************************************************
  2. * sched/clock/clock_abstime2ticks.c
  3. *
  4. * Copyright (C) 2007, 2008, 2013-2014, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <time.h>
  40. #include <errno.h>
  41. #include <debug.h>
  42. #include "clock/clock.h"
  43. /****************************************************************************
  44. * Public Functions
  45. ****************************************************************************/
  46. /****************************************************************************
  47. * Name: clock_timespec_compare
  48. *
  49. * Description:
  50. * Return < 0 if time a is before time b
  51. * Return > 0 if time b is before time a
  52. * Return 0 if time a is the same as time b
  53. *
  54. ****************************************************************************/
  55. int clock_timespec_compare(FAR const struct timespec *a,
  56. FAR const struct timespec *b)
  57. {
  58. if (a->tv_sec < b->tv_sec)
  59. {
  60. return -1;
  61. }
  62. if (a->tv_sec > b->tv_sec)
  63. {
  64. return 1;
  65. }
  66. return a->tv_nsec - b->tv_nsec;
  67. }
  68. /****************************************************************************
  69. * Name: clock_abstime2ticks
  70. *
  71. * Description:
  72. * Convert an absolute timespec delay to system timer ticks.
  73. *
  74. * Input Parameters:
  75. * clockid - The timing source to use in the conversion
  76. * reltime - Convert this absolute time to system clock ticks.
  77. * ticks - Return the converted number of ticks here.
  78. *
  79. * Returned Value:
  80. * OK on success; A non-zero error number on failure
  81. *
  82. * Assumptions:
  83. * Interrupts should be disabled so that the time is not changing during
  84. * the calculation
  85. *
  86. ****************************************************************************/
  87. int clock_abstime2ticks(clockid_t clockid, FAR const struct timespec *abstime,
  88. FAR sclock_t *ticks)
  89. {
  90. struct timespec currtime;
  91. struct timespec reltime;
  92. int ret;
  93. /* Convert the timespec to clock ticks. NOTE: Here we use internal knowledge
  94. * that CLOCK_REALTIME is defined to be zero!
  95. */
  96. ret = clock_gettime(clockid, &currtime);
  97. if (ret != OK)
  98. {
  99. return EINVAL;
  100. }
  101. if (clock_timespec_compare(abstime, &currtime) < 0)
  102. {
  103. /* Every caller of clock_abstime2ticks check 'ticks < 0' to see if
  104. * absolute time is in the past. So lets just return negative tick
  105. * here.
  106. */
  107. *ticks = -1;
  108. return OK;
  109. }
  110. /* The relative time to wait is the absolute time minus the current time. */
  111. reltime.tv_nsec = (abstime->tv_nsec - currtime.tv_nsec);
  112. reltime.tv_sec = (abstime->tv_sec - currtime.tv_sec);
  113. /* Check if we were supposed to borrow from the seconds. */
  114. if (reltime.tv_nsec < 0)
  115. {
  116. reltime.tv_nsec += NSEC_PER_SEC;
  117. reltime.tv_sec -= 1;
  118. }
  119. /* Convert this relative time into clock ticks. */
  120. return clock_time2ticks(&reltime, ticks);
  121. }