thr_cond.h 5.4 KB

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