ptw32_relmillisecs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * ptw32_relmillisecs.c
  3. *
  4. * Description:
  5. * This translation unit implements miscellaneous thread functions.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-win32 - POSIX Threads Library for Win32
  10. * Copyright(C) 1998 John E. Bossom
  11. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. * The current list of contributors is contained
  16. * in the file CONTRIBUTORS included with the source
  17. * code distribution. The list can also be seen at the
  18. * following World Wide Web location:
  19. * http://sources.redhat.com/pthreads-win32/contributors.html
  20. *
  21. * This library is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU Lesser General Public
  23. * License as published by the Free Software Foundation; either
  24. * version 2 of the License, or (at your option) any later version.
  25. *
  26. * This library is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public
  32. * License along with this library in the file COPYING.LIB;
  33. * if not, write to the Free Software Foundation, Inc.,
  34. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  35. */
  36. #include "pthread.h"
  37. #include "implement.h"
  38. #if !defined(NEED_FTIME)
  39. #include <sys/timeb.h>
  40. #endif
  41. #if defined(PTW32_BUILD_INLINED)
  42. INLINE
  43. #endif /* PTW32_BUILD_INLINED */
  44. DWORD
  45. ptw32_relmillisecs (const struct timespec * abstime)
  46. {
  47. const int64_t NANOSEC_PER_MILLISEC = 1000000;
  48. const int64_t MILLISEC_PER_SEC = 1000;
  49. DWORD milliseconds;
  50. int64_t tmpAbsMilliseconds;
  51. int64_t tmpCurrMilliseconds;
  52. #if defined(NEED_FTIME)
  53. struct timespec currSysTime;
  54. FILETIME ft;
  55. SYSTEMTIME st;
  56. #else /* ! NEED_FTIME */
  57. #if ( defined(_MSC_VER) && _MSC_VER >= 1300 ) || \
  58. ( (defined(__MINGW64__) || defined(__MINGW32__)) && __MSVCRT_VERSION__ >= 0x0601 )
  59. struct __timeb64 currSysTime;
  60. #else
  61. struct _timeb currSysTime;
  62. #endif
  63. #endif /* NEED_FTIME */
  64. /*
  65. * Calculate timeout as milliseconds from current system time.
  66. */
  67. /*
  68. * subtract current system time from abstime in a way that checks
  69. * that abstime is never in the past, or is never equivalent to the
  70. * defined INFINITE value (0xFFFFFFFF).
  71. *
  72. * Assume all integers are unsigned, i.e. cannot test if less than 0.
  73. */
  74. tmpAbsMilliseconds = (int64_t)abstime->tv_sec * MILLISEC_PER_SEC;
  75. tmpAbsMilliseconds += ((int64_t)abstime->tv_nsec + (NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC;
  76. /* get current system time */
  77. #if defined(NEED_FTIME)
  78. GetSystemTime(&st);
  79. SystemTimeToFileTime(&st, &ft);
  80. /*
  81. * GetSystemTimeAsFileTime(&ft); would be faster,
  82. * but it does not exist on WinCE
  83. */
  84. ptw32_filetime_to_timespec(&ft, &currSysTime);
  85. tmpCurrMilliseconds = (int64_t)currSysTime.tv_sec * MILLISEC_PER_SEC;
  86. tmpCurrMilliseconds += ((int64_t)currSysTime.tv_nsec + (NANOSEC_PER_MILLISEC/2))
  87. / NANOSEC_PER_MILLISEC;
  88. #else /* ! NEED_FTIME */
  89. #if defined(_MSC_VER) && _MSC_VER >= 1400
  90. _ftime64_s(&currSysTime);
  91. #elif ( defined(_MSC_VER) && _MSC_VER >= 1300 ) || \
  92. ( (defined(__MINGW64__) || defined(__MINGW32__)) && __MSVCRT_VERSION__ >= 0x0601 )
  93. _ftime64(&currSysTime);
  94. #else
  95. _ftime(&currSysTime);
  96. #endif
  97. tmpCurrMilliseconds = (int64_t) currSysTime.time * MILLISEC_PER_SEC;
  98. tmpCurrMilliseconds += (int64_t) currSysTime.millitm;
  99. #endif /* NEED_FTIME */
  100. if (tmpAbsMilliseconds > tmpCurrMilliseconds)
  101. {
  102. milliseconds = (DWORD) (tmpAbsMilliseconds - tmpCurrMilliseconds);
  103. if (milliseconds == INFINITE)
  104. {
  105. /* Timeouts must be finite */
  106. milliseconds--;
  107. }
  108. }
  109. else
  110. {
  111. /* The abstime given is in the past */
  112. milliseconds = 0;
  113. }
  114. return milliseconds;
  115. }