clock_abstime2ticks.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * Private Functions
  45. ****************************************************************************/
  46. /****************************************************************************
  47. * Name: compare_timespec
  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. static long compare_timespec(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 (long)a->tv_nsec - (long)b->tv_nsec;
  67. }
  68. /****************************************************************************
  69. * Public Functions
  70. ****************************************************************************/
  71. /****************************************************************************
  72. * Name: clock_abstime2ticks
  73. *
  74. * Description:
  75. * Convert an absolute timespec delay to system timer ticks.
  76. *
  77. * Input Parameters:
  78. * clockid - The timing source to use in the conversion
  79. * reltime - Convert this absolue time to system clock ticks.
  80. * ticks - Return the converted number of ticks here.
  81. *
  82. * Returned Value:
  83. * OK on success; A non-zero error number on failure;
  84. *
  85. * Assumptions:
  86. * Interrupts should be disabled so that the time is not changing during
  87. * the calculation
  88. *
  89. ****************************************************************************/
  90. int clock_abstime2ticks(clockid_t clockid, FAR const struct timespec *abstime,
  91. FAR sclock_t *ticks)
  92. {
  93. struct timespec currtime;
  94. struct timespec reltime;
  95. int ret;
  96. /* Convert the timespec to clock ticks. NOTE: Here we use internal knowledge
  97. * that CLOCK_REALTIME is defined to be zero!
  98. */
  99. ret = clock_gettime(clockid, &currtime);
  100. if (ret != OK)
  101. {
  102. return EINVAL;
  103. }
  104. if (compare_timespec(abstime, &currtime) < 0)
  105. {
  106. /* Every caller of clock_abstime2ticks check 'ticks < 0' to see if
  107. * absolute time is in the past. So lets just return negative tick
  108. * here.
  109. */
  110. *ticks = -1;
  111. return OK;
  112. }
  113. /* The relative time to wait is the absolute time minus the current time. */
  114. reltime.tv_nsec = (abstime->tv_nsec - currtime.tv_nsec);
  115. reltime.tv_sec = (abstime->tv_sec - currtime.tv_sec);
  116. /* Check if we were supposed to borrow from the seconds to borrow from the
  117. * seconds
  118. */
  119. if (reltime.tv_nsec < 0)
  120. {
  121. reltime.tv_nsec += NSEC_PER_SEC;
  122. reltime.tv_sec -= 1;
  123. }
  124. /* Convert this relative time into clock ticks. */
  125. return clock_time2ticks(&reltime, ticks);
  126. }