Clone of PhatAC @ https://github.com/floaterxk/PhatAC

thr_cond.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #ifndef THR_COND_INCLUDED
  2. #define THR_COND_INCLUDED
  3. /* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  14. /**
  15. MySQL condition variable implementation.
  16. There are three "layers":
  17. 1) native_cond_*()
  18. Functions that map directly down to OS primitives.
  19. Windows - ConditionVariable
  20. Other OSes - pthread
  21. 2) my_cond_*()
  22. Functions that use SAFE_MUTEX (default for debug).
  23. Otherwise native_cond_*() is used.
  24. 3) mysql_cond*()
  25. Functions that include Performance Schema instrumentation.
  26. See include/mysql/psi/mysql_thread.h
  27. */
  28. #include "my_thread.h"
  29. #include "thr_mutex.h"
  30. C_MODE_START
  31. #ifdef _WIN32
  32. typedef CONDITION_VARIABLE native_cond_t;
  33. #else
  34. typedef pthread_cond_t native_cond_t;
  35. #endif
  36. #ifdef _WIN32
  37. /**
  38. Convert abstime to milliseconds
  39. */
  40. static DWORD get_milliseconds(const struct timespec *abstime)
  41. {
  42. #ifndef HAVE_STRUCT_TIMESPEC
  43. long long millis;
  44. union ft64 now;
  45. if (abstime == NULL)
  46. return INFINITE;
  47. GetSystemTimeAsFileTime(&now.ft);
  48. /*
  49. Calculate time left to abstime
  50. - subtract start time from current time(values are in 100ns units)
  51. - convert to millisec by dividing with 10000
  52. */
  53. millis= (abstime->tv.i64 - now.i64) / 10000;
  54. /* Don't allow the timeout to be negative */
  55. if (millis < 0)
  56. return 0;
  57. /*
  58. Make sure the calculated timeout does not exceed original timeout
  59. value which could cause "wait for ever" if system time changes
  60. */
  61. if (millis > abstime->max_timeout_msec)
  62. millis= abstime->max_timeout_msec;
  63. if (millis > UINT_MAX)
  64. millis= UINT_MAX;
  65. return (DWORD)millis;
  66. #else
  67. /*
  68. Convert timespec to millis and subtract current time.
  69. my_getsystime() returns time in 100 ns units.
  70. */
  71. ulonglong future= abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000;
  72. ulonglong now= my_getsystime() / 10000;
  73. /* Don't allow the timeout to be negative. */
  74. if (future < now)
  75. return 0;
  76. return (DWORD)(future - now);
  77. #endif
  78. }
  79. #endif /* _WIN32 */
  80. static inline int native_cond_init(native_cond_t *cond)
  81. {
  82. #ifdef _WIN32
  83. InitializeConditionVariable(cond);
  84. return 0;
  85. #else
  86. /* pthread_condattr_t is not used in MySQL */
  87. return pthread_cond_init(cond, NULL);
  88. #endif
  89. }
  90. static inline int native_cond_destroy(native_cond_t *cond)
  91. {
  92. #ifdef _WIN32
  93. return 0; /* no destroy function */
  94. #else
  95. return pthread_cond_destroy(cond);
  96. #endif
  97. }
  98. static inline int native_cond_timedwait(native_cond_t *cond,
  99. native_mutex_t *mutex,
  100. const struct timespec *abstime)
  101. {
  102. #ifdef _WIN32
  103. DWORD timeout= get_milliseconds(abstime);
  104. if (!SleepConditionVariableCS(cond, mutex, timeout))
  105. return ETIMEDOUT;
  106. return 0;
  107. #else
  108. return pthread_cond_timedwait(cond, mutex, abstime);
  109. #endif
  110. }
  111. static inline int native_cond_wait(native_cond_t *cond, native_mutex_t *mutex)
  112. {
  113. #ifdef _WIN32
  114. if (!SleepConditionVariableCS(cond, mutex, INFINITE))
  115. return ETIMEDOUT;
  116. return 0;
  117. #else
  118. return pthread_cond_wait(cond, mutex);
  119. #endif
  120. }
  121. static inline int native_cond_signal(native_cond_t *cond)
  122. {
  123. #ifdef _WIN32
  124. WakeConditionVariable(cond);
  125. return 0;
  126. #else
  127. return pthread_cond_signal(cond);
  128. #endif
  129. }
  130. static inline int native_cond_broadcast(native_cond_t *cond)
  131. {
  132. #ifdef _WIN32
  133. WakeAllConditionVariable(cond);
  134. return 0;
  135. #else
  136. return pthread_cond_broadcast(cond);
  137. #endif
  138. }
  139. #ifdef SAFE_MUTEX
  140. int safe_cond_wait(native_cond_t *cond, my_mutex_t *mp,
  141. const char *file, uint line);
  142. int safe_cond_timedwait(native_cond_t *cond, my_mutex_t *mp,
  143. const struct timespec *abstime,
  144. const char *file, uint line);
  145. #endif
  146. static inline int my_cond_timedwait(native_cond_t *cond, my_mutex_t *mp,
  147. const struct timespec *abstime
  148. #ifdef SAFE_MUTEX
  149. , const char *file, uint line
  150. #endif
  151. )
  152. {
  153. #ifdef SAFE_MUTEX
  154. return safe_cond_timedwait(cond, mp, abstime, file, line);
  155. #else
  156. return native_cond_timedwait(cond, mp, abstime);
  157. #endif
  158. }
  159. static inline int my_cond_wait(native_cond_t *cond, my_mutex_t *mp
  160. #ifdef SAFE_MUTEX
  161. , const char *file, uint line
  162. #endif
  163. )
  164. {
  165. #ifdef SAFE_MUTEX
  166. return safe_cond_wait(cond, mp, file, line);
  167. #else
  168. return native_cond_wait(cond, mp);
  169. #endif
  170. }
  171. C_MODE_END
  172. #endif /* THR_COND_INCLUDED */