my_thread.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. /**
  19. @file include/my_thread.h
  20. Defines to make different thread packages compatible.
  21. */
  22. #ifndef MY_THREAD_INCLUDED
  23. #define MY_THREAD_INCLUDED
  24. #include <errno.h>
  25. #include <stdbool.h>
  26. #include <stddef.h>
  27. #include <mysql/components/services/my_thread_bits.h>
  28. #include "my_compiler.h"
  29. #include "my_config.h"
  30. #include "my_inttypes.h"
  31. #include "my_macros.h"
  32. #ifndef ETIME
  33. #define ETIME ETIMEDOUT /* For FreeBSD */
  34. #endif
  35. #ifndef ETIMEDOUT
  36. #define ETIMEDOUT 145 /* Win32 doesn't have this */
  37. #endif
  38. #if defined(__sparc) && (defined(__SUNPRO_CC) || defined(__SUNPRO_C))
  39. #define STACK_MULTIPLIER 2UL
  40. #elif defined HAVE_UBSAN
  41. #define STACK_MULTIPLIER 3UL
  42. #elif defined HAVE_LSAN
  43. #define STACK_MULTIPLIER 4UL
  44. #else
  45. #define STACK_MULTIPLIER 1UL
  46. #endif
  47. #if SIZEOF_CHARP > 4
  48. #define DEFAULT_THREAD_STACK (STACK_MULTIPLIER * 280UL * 1024UL)
  49. #else
  50. #define DEFAULT_THREAD_STACK (STACK_MULTIPLIER * 216UL * 1024UL)
  51. #endif
  52. static inline int is_timeout(int e) {
  53. #if ETIMEDOUT == ETIME
  54. return e == ETIMEDOUT;
  55. #else
  56. return e == ETIMEDOUT || e == ETIME;
  57. #endif
  58. }
  59. #ifdef _WIN32
  60. #define MY_THREAD_CREATE_JOINABLE 0
  61. #define MY_THREAD_CREATE_DETACHED 1
  62. typedef void *(__cdecl *my_start_routine)(void *);
  63. #else
  64. #define MY_THREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
  65. #define MY_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
  66. typedef void *(*my_start_routine)(void *);
  67. #endif
  68. static inline my_thread_t my_thread_self() {
  69. #ifdef _WIN32
  70. return GetCurrentThreadId();
  71. #else
  72. return pthread_self();
  73. #endif
  74. }
  75. static inline int my_thread_equal(my_thread_t t1, my_thread_t t2) {
  76. #ifdef _WIN32
  77. return t1 == t2;
  78. #else
  79. return pthread_equal(t1, t2);
  80. #endif
  81. }
  82. static inline int my_thread_attr_init(my_thread_attr_t *attr) {
  83. #ifdef _WIN32
  84. attr->dwStackSize = 0;
  85. /* Set to joinable by default to match Linux */
  86. attr->detachstate = MY_THREAD_CREATE_JOINABLE;
  87. return 0;
  88. #else
  89. return pthread_attr_init(attr);
  90. #endif
  91. }
  92. static inline int my_thread_attr_destroy(my_thread_attr_t *attr) {
  93. #ifdef _WIN32
  94. attr->dwStackSize = 0;
  95. /* Set to joinable by default to match Linux */
  96. attr->detachstate = MY_THREAD_CREATE_JOINABLE;
  97. return 0;
  98. #else
  99. return pthread_attr_destroy(attr);
  100. #endif
  101. }
  102. static inline int my_thread_attr_setstacksize(my_thread_attr_t *attr,
  103. size_t stacksize) {
  104. #ifdef _WIN32
  105. attr->dwStackSize = (DWORD)stacksize;
  106. return 0;
  107. #else
  108. return pthread_attr_setstacksize(attr, stacksize);
  109. #endif
  110. }
  111. static inline int my_thread_attr_setdetachstate(my_thread_attr_t *attr,
  112. int detachstate) {
  113. #ifdef _WIN32
  114. attr->detachstate = detachstate;
  115. return 0;
  116. #else
  117. return pthread_attr_setdetachstate(attr, detachstate);
  118. #endif
  119. }
  120. static inline int my_thread_attr_getstacksize(my_thread_attr_t *attr,
  121. size_t *stacksize) {
  122. #ifdef _WIN32
  123. *stacksize = (size_t)attr->dwStackSize;
  124. return 0;
  125. #else
  126. return pthread_attr_getstacksize(attr, stacksize);
  127. #endif
  128. }
  129. static inline void my_thread_yield() {
  130. #ifdef _WIN32
  131. SwitchToThread();
  132. #else
  133. sched_yield();
  134. #endif
  135. }
  136. int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr,
  137. my_start_routine func, void *arg);
  138. int my_thread_join(my_thread_handle *thread, void **value_ptr);
  139. int my_thread_cancel(my_thread_handle *thread);
  140. void my_thread_exit(void *value_ptr) MY_ATTRIBUTE((noreturn));
  141. extern bool my_thread_global_init();
  142. extern void my_thread_global_reinit();
  143. extern void my_thread_global_end();
  144. // Need to be extern "C" for the time being, due to memcached.
  145. extern "C" bool my_thread_init();
  146. extern "C" void my_thread_end();
  147. #endif /* MY_THREAD_INCLUDED */