You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: unixstl/synch/sleep_functions.h
  3. *
  4. * Purpose: UNIXSTL time functions.
  5. *
  6. * Created: 2nd September 2005
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file unixstl/synch/sleep_functions.h
  40. *
  41. * \brief [C, C++] Various time functions
  42. * (\ref group__library__synch "Synchronisation" Library).
  43. */
  44. #ifndef UNIXSTL_INCL_UNIXSTL_SYNCH_H_SLEEP_FUNCTIONS
  45. #define UNIXSTL_INCL_UNIXSTL_SYNCH_H_SLEEP_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define UNIXSTL_VER_UNIXSTL_SYNCH_H_SLEEP_FUNCTIONS_MAJOR 2
  48. # define UNIXSTL_VER_UNIXSTL_SYNCH_H_SLEEP_FUNCTIONS_MINOR 0
  49. # define UNIXSTL_VER_UNIXSTL_SYNCH_H_SLEEP_FUNCTIONS_REVISION 5
  50. # define UNIXSTL_VER_UNIXSTL_SYNCH_H_SLEEP_FUNCTIONS_EDIT 17
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
  56. # include <unixstl/unixstl.h>
  57. #endif /* !UNIXSTL_INCL_UNIXSTL_H_UNIXSTL */
  58. #ifndef STLSOFT_INCL_SYS_H_SELECT
  59. # define STLSOFT_INCL_SYS_H_SELECT
  60. # include <sys/select.h>
  61. #endif /* !STLSOFT_INCL_SYS_H_SELECT */
  62. #ifndef STLSOFT_INCL_SYS_H_TIME
  63. # define STLSOFT_INCL_SYS_H_TIME
  64. # include <sys/time.h>
  65. #endif /* !STLSOFT_INCL_SYS_H_TIME */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Namespace
  68. */
  69. #if !defined(_UNIXSTL_NO_NAMESPACE) && \
  70. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  71. # if defined(_STLSOFT_NO_NAMESPACE)
  72. /* There is no stlsoft namespace, so must define ::unixstl */
  73. namespace unixstl
  74. {
  75. # else
  76. /* Define stlsoft::unixstl_project */
  77. namespace stlsoft
  78. {
  79. namespace unixstl_project
  80. {
  81. # endif /* _STLSOFT_NO_NAMESPACE */
  82. #endif /* !_UNIXSTL_NO_NAMESPACE */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * Functions
  85. */
  86. /** \brief [C, C++] Puts the calling thread to sleep for the given number of
  87. * microseconds.
  88. \code
  89. unixstl__micro_sleep(100000); // Sleep for 0.1 seconds
  90. unixstl__micro_sleep(100); // Sleep for 0.1 milliseconds
  91. \endcode
  92. *
  93. * \param microseconds The number of microseconds to wait
  94. *
  95. * \return A boolean value indicating whether the operation was
  96. * successful. If not, <code>errno</code> will contain an error code
  97. * representing the reason for failure.
  98. *
  99. * \see unixstl::micro_sleep
  100. */
  101. STLSOFT_INLINE us_int_t unixstl__micro_sleep(us_uint_t microseconds)
  102. {
  103. #ifdef _WIN32
  104. return (STLSOFT_NS_GLOBAL(Sleep)(microseconds / 1000), us_true_v);
  105. #else /* ? _WIN32 */
  106. struct timeval ts;
  107. ts.tv_sec = stlsoft_static_cast(long, microseconds / 1000000);
  108. ts.tv_usec = stlsoft_static_cast(long, microseconds % 1000000);
  109. return -1 != STLSOFT_NS_GLOBAL(select)(0, NULL, NULL, NULL, &ts);
  110. #endif /* _WIN32 */
  111. }
  112. /* /////////////////////////////////////////////////////////////////////////
  113. * Namespace
  114. */
  115. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  116. namespace unixstl
  117. {
  118. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  119. /* /////////////////////////////////////////////////////////////////////////
  120. * C++ functions
  121. */
  122. #ifdef __cplusplus
  123. /** \brief [C++ only] Puts the calling thread to sleep for the given number of
  124. * microseconds.
  125. \code
  126. unixstl::micro_sleep(100000); // Sleep for 0.1 seconds
  127. unixstl::micro_sleep(100); // Sleep for 0.1 milliseconds
  128. \endcode
  129. *
  130. * \param microseconds The number of microseconds to wait
  131. *
  132. * \return A boolean value indicating whether the operation was
  133. * successful. If not, <code>errno</code> will contain an error code
  134. * representing the reason for failure.
  135. */
  136. inline us_int_t micro_sleep(us_uint_t microseconds)
  137. {
  138. return unixstl__micro_sleep(microseconds);
  139. }
  140. #endif /* __cplusplus */
  141. /* /////////////////////////////////////////////////////////////////////////
  142. * Unit-testing
  143. */
  144. #ifdef STLSOFT_UNITTEST
  145. # include "./unittest/sleep_functions_unittest_.h"
  146. #endif /* STLSOFT_UNITTEST */
  147. /* ////////////////////////////////////////////////////////////////////// */
  148. #ifndef _UNIXSTL_NO_NAMESPACE
  149. # if defined(_STLSOFT_NO_NAMESPACE) || \
  150. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  151. } /* namespace unixstl */
  152. # else
  153. } /* namespace unixstl_project */
  154. } /* namespace stlsoft */
  155. # endif /* _STLSOFT_NO_NAMESPACE */
  156. #endif /* !_UNIXSTL_NO_NAMESPACE */
  157. /* ////////////////////////////////////////////////////////////////////// */
  158. #endif /* !UNIXSTL_INCL_UNIXSTL_SYNCH_H_SLEEP_FUNCTIONS */
  159. /* ///////////////////////////// end of file //////////////////////////// */