my_thread.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright (c) 2000, 2016, 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 Street, Fifth Floor, Boston, MA 02110-1301, USA */
  18. /* Defines to make different thread packages compatible */
  19. #ifndef MY_THREAD_INCLUDED
  20. #define MY_THREAD_INCLUDED
  21. #include "my_global.h" /* my_bool */
  22. #if !defined(_WIN32)
  23. #include <pthread.h>
  24. #endif
  25. #ifndef ETIME
  26. #define ETIME ETIMEDOUT /* For FreeBSD */
  27. #endif
  28. #ifndef ETIMEDOUT
  29. #define ETIMEDOUT 145 /* Win32 doesn't have this */
  30. #endif
  31. /*
  32. MySQL can survive with 32K, but some glibc libraries require > 128K stack
  33. To resolve hostnames. Also recursive stored procedures needs stack.
  34. */
  35. #if defined(__sparc) && (defined(__SUNPRO_CC) || defined(__SUNPRO_C))
  36. #define STACK_MULTIPLIER 2UL
  37. #else
  38. #define STACK_MULTIPLIER 1UL
  39. #endif
  40. #if SIZEOF_CHARP > 4
  41. #define DEFAULT_THREAD_STACK (STACK_MULTIPLIER * 256UL * 1024UL)
  42. #else
  43. #define DEFAULT_THREAD_STACK (STACK_MULTIPLIER * 192UL * 1024UL)
  44. #endif
  45. #ifdef __cplusplus
  46. #define EXTERNC extern "C"
  47. #else
  48. #define EXTERNC
  49. #endif
  50. C_MODE_START
  51. #ifdef _WIN32
  52. typedef volatile LONG my_thread_once_t;
  53. typedef DWORD my_thread_t;
  54. typedef struct thread_attr
  55. {
  56. DWORD dwStackSize;
  57. int detachstate;
  58. } my_thread_attr_t;
  59. #define MY_THREAD_CREATE_JOINABLE 0
  60. #define MY_THREAD_CREATE_DETACHED 1
  61. typedef void * (__cdecl *my_start_routine)(void *);
  62. #define MY_THREAD_ONCE_INIT 0
  63. #define MY_THREAD_ONCE_INPROGRESS 1
  64. #define MY_THREAD_ONCE_DONE 2
  65. #else
  66. typedef pthread_once_t my_thread_once_t;
  67. typedef pthread_t my_thread_t;
  68. typedef pthread_attr_t my_thread_attr_t;
  69. #define MY_THREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
  70. #define MY_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
  71. typedef void *(* my_start_routine)(void *);
  72. #define MY_THREAD_ONCE_INIT PTHREAD_ONCE_INIT
  73. #endif
  74. typedef struct st_my_thread_handle
  75. {
  76. my_thread_t thread;
  77. #ifdef _WIN32
  78. HANDLE handle;
  79. #endif
  80. } my_thread_handle;
  81. int my_thread_once(my_thread_once_t *once_control, void (*init_routine)(void));
  82. static inline my_thread_t my_thread_self()
  83. {
  84. #ifdef _WIN32
  85. return GetCurrentThreadId();
  86. #else
  87. return pthread_self();
  88. #endif
  89. }
  90. static inline int my_thread_equal(my_thread_t t1, my_thread_t t2)
  91. {
  92. #ifdef _WIN32
  93. return t1 == t2;
  94. #else
  95. return pthread_equal(t1, t2);
  96. #endif
  97. }
  98. static inline int my_thread_attr_init(my_thread_attr_t *attr)
  99. {
  100. #ifdef _WIN32
  101. attr->dwStackSize= 0;
  102. /* Set to joinable by default to match Linux */
  103. attr->detachstate= MY_THREAD_CREATE_JOINABLE;
  104. return 0;
  105. #else
  106. return pthread_attr_init(attr);
  107. #endif
  108. }
  109. static inline int my_thread_attr_destroy(my_thread_attr_t *attr)
  110. {
  111. #ifdef _WIN32
  112. attr->dwStackSize= 0;
  113. /* Set to joinable by default to match Linux */
  114. attr->detachstate= MY_THREAD_CREATE_JOINABLE;
  115. return 0;
  116. #else
  117. return pthread_attr_destroy(attr);
  118. #endif
  119. }
  120. static inline int my_thread_attr_setstacksize(my_thread_attr_t *attr,
  121. size_t stacksize)
  122. {
  123. #ifdef _WIN32
  124. attr->dwStackSize= (DWORD)stacksize;
  125. return 0;
  126. #else
  127. return pthread_attr_setstacksize(attr, stacksize);
  128. #endif
  129. }
  130. static inline int my_thread_attr_setdetachstate(my_thread_attr_t *attr,
  131. int detachstate)
  132. {
  133. #ifdef _WIN32
  134. attr->detachstate= detachstate;
  135. return 0;
  136. #else
  137. return pthread_attr_setdetachstate(attr, detachstate);
  138. #endif
  139. }
  140. static inline int my_thread_attr_getstacksize(my_thread_attr_t *attr,
  141. size_t *stacksize)
  142. {
  143. #ifdef _WIN32
  144. *stacksize= (size_t)attr->dwStackSize;
  145. return 0;
  146. #else
  147. return pthread_attr_getstacksize(attr, stacksize);
  148. #endif
  149. }
  150. static inline void my_thread_yield()
  151. {
  152. #ifdef _WIN32
  153. SwitchToThread();
  154. #else
  155. sched_yield();
  156. #endif
  157. }
  158. int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr,
  159. my_start_routine func, void *arg);
  160. int my_thread_join(my_thread_handle *thread, void **value_ptr);
  161. int my_thread_cancel(my_thread_handle *thread);
  162. void my_thread_exit(void *value_ptr);
  163. extern my_bool my_thread_global_init();
  164. extern void my_thread_global_reinit();
  165. extern void my_thread_global_end();
  166. extern my_bool my_thread_init();
  167. extern void my_thread_end();
  168. C_MODE_END
  169. #endif /* MY_THREAD_INCLUDED */