deadline.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Computing deadlines for timeouts.
  2. Copyright (C) 2017-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <net-internal.h>
  16. #include <assert.h>
  17. #include <limits.h>
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <time.h>
  21. struct deadline_current_time
  22. __deadline_current_time (void)
  23. {
  24. struct deadline_current_time result;
  25. if (__clock_gettime64 (CLOCK_MONOTONIC, &result.current) != 0)
  26. __clock_gettime64 (CLOCK_REALTIME, &result.current);
  27. assert (result.current.tv_sec >= 0);
  28. return result;
  29. }
  30. /* A special deadline value for which __deadline_is_infinite is
  31. true. */
  32. static inline struct deadline
  33. infinite_deadline (void)
  34. {
  35. return (struct deadline) { { -1, -1 } };
  36. }
  37. struct deadline
  38. __deadline_from_timeval (struct deadline_current_time current,
  39. struct timeval tv)
  40. {
  41. assert (__is_timeval_valid_timeout (tv));
  42. /* Compute second-based deadline. Perform the addition in
  43. uintmax_t, which is unsigned, to simply overflow detection. */
  44. uintmax_t sec = current.current.tv_sec;
  45. sec += tv.tv_sec;
  46. if (sec < (uintmax_t) tv.tv_sec)
  47. return infinite_deadline ();
  48. /* Compute nanosecond deadline. */
  49. int nsec = current.current.tv_nsec + tv.tv_usec * 1000;
  50. if (nsec >= 1000 * 1000 * 1000)
  51. {
  52. /* Carry nanosecond overflow to seconds. */
  53. nsec -= 1000 * 1000 * 1000;
  54. if (sec + 1 < sec)
  55. return infinite_deadline ();
  56. ++sec;
  57. }
  58. /* This uses a GCC extension, otherwise these casts for detecting
  59. overflow would not be defined. */
  60. if ((time_t) sec < 0 || sec != (uintmax_t) (time_t) sec)
  61. return infinite_deadline ();
  62. return (struct deadline) { { sec, nsec } };
  63. }
  64. int
  65. __deadline_to_ms (struct deadline_current_time current,
  66. struct deadline deadline)
  67. {
  68. if (__deadline_is_infinite (deadline))
  69. return INT_MAX;
  70. if (current.current.tv_sec > deadline.absolute.tv_sec
  71. || (current.current.tv_sec == deadline.absolute.tv_sec
  72. && current.current.tv_nsec >= deadline.absolute.tv_nsec))
  73. return 0;
  74. time_t sec = deadline.absolute.tv_sec - current.current.tv_sec;
  75. if (sec >= INT_MAX)
  76. /* This value will overflow below. */
  77. return INT_MAX;
  78. int nsec = deadline.absolute.tv_nsec - current.current.tv_nsec;
  79. if (nsec < 0)
  80. {
  81. /* Borrow from the seconds field. */
  82. assert (sec > 0);
  83. --sec;
  84. nsec += 1000 * 1000 * 1000;
  85. }
  86. /* Prepare for rounding up to milliseconds. */
  87. nsec += 999999;
  88. if (nsec > 1000 * 1000 * 1000)
  89. {
  90. assert (sec < INT_MAX);
  91. ++sec;
  92. nsec -= 1000 * 1000 * 1000;
  93. }
  94. unsigned int msec = nsec / (1000 * 1000);
  95. if (sec > INT_MAX / 1000)
  96. return INT_MAX;
  97. msec += sec * 1000;
  98. if (msec > INT_MAX)
  99. return INT_MAX;
  100. return msec;
  101. }