sleep.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* $Id:$ */
  2. /*
  3. * Copyright (c) 2008 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #ifdef HAVE_SYS_SELECT /* According to POSIX 1003.1-2001 */
  35. #include <sys/select.h>
  36. #endif
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39. #include <unistd.h>
  40. #include <errno.h>
  41. #include <string.h>
  42. #ifdef HAVE_SYS_EVENT
  43. #include <sys/event.h>
  44. #endif
  45. /* necessary for ioport_sleep() functions */
  46. #ifdef HAVE_SYS_IO_H /* Linux */
  47. #include <sys/io.h>
  48. #elif defined HAVE_ARCHITECTURE_I386_PIO_H /* OS X */
  49. #include <architecture/i386/pio.h>
  50. #endif
  51. #ifndef __SLEEP_H__
  52. #define __SLEEP_H__
  53. static inline void
  54. nanosleep_sleep(struct timespec nap)
  55. {
  56. nanosleep(&nap, NULL);
  57. }
  58. /*
  59. * Straight forward... keep calling gettimeofday() unti the apporpriate amount
  60. * of time has passed. Pretty damn accurate from 1 to 100Mbps
  61. */
  62. static inline void
  63. gettimeofday_sleep(struct timespec nap)
  64. {
  65. struct timeval now, sleep_until, nap_for;
  66. gettimeofday(&now, NULL);
  67. TIMESPEC_TO_TIMEVAL(&nap_for, &nap);
  68. timeradd(&now, &nap_for, &sleep_until);
  69. do {
  70. gettimeofday(&now, NULL);
  71. } while (timercmp(&now, &sleep_until, <));
  72. }
  73. #ifdef HAVE_ABSOLUTE_TIME
  74. #include <CoreServices/CoreServices.h>
  75. /*
  76. * Apple's AbsoluteTime functions give at least .1usec precision
  77. * which is pretty damn sweet
  78. */
  79. static inline void
  80. absolute_time_sleep(struct timespec nap)
  81. {
  82. AbsoluteTime sleep_until, naptime, time_left;
  83. Nanoseconds nanosec;
  84. nanosec = UInt64ToUnsignedWide(TIMESPEC_TO_NANOSEC(&nap));
  85. naptime = NanosecondsToAbsolute(nanosec);
  86. sleep_until = AddAbsoluteToAbsolute(UpTime(), naptime);
  87. do {
  88. time_left = SubAbsoluteFromAbsolute(sleep_until, UpTime());
  89. } while (NonZero(time_left));
  90. }
  91. #endif /* HAVE_ABSOLUTE_TIME */
  92. #ifdef HAVE_SELECT
  93. /*
  94. * sleep for some time using the select() call timeout method. This is
  95. * highly portable for sub-second sleeping, but only for about 1msec
  96. * resolution which is pretty much useless for our needs. Keeping it here
  97. * for furture reference
  98. */
  99. static inline void
  100. select_sleep(const struct timespec nap)
  101. {
  102. struct timeval timeout;
  103. TIMESPEC_TO_TIMEVAL(&timeout, &nap);
  104. if (select(0, NULL, NULL, NULL, &timeout) < 0)
  105. warnx("select_sleep() returned early due to error: %s", strerror(errno));
  106. }
  107. #endif /* HAVE_SELECT */
  108. /*
  109. * ioport_sleep() only works on Intel and quite possibly only Linux.
  110. * But the basic idea is to write to the IO Port 0x80 which should
  111. * take exactly 1usec regardless of the CPU speed and without
  112. * calling a sleep method which allows the kernel to service another thread
  113. * Idea stolen from: http://c-faq.com/osdep/sd25.html
  114. */
  115. extern int ioport_sleep_value;
  116. /* before calling port_sleep(), you have to call port_sleep_init() */
  117. void ioport_sleep_init(void);
  118. void ioport_sleep(const struct timespec nap);
  119. #endif /* __SLEEP_H__ */