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

my_thread.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
  12. /* Defines to make different thread packages compatible */
  13. #ifndef MY_THREAD_INCLUDED
  14. #define MY_THREAD_INCLUDED
  15. #include "my_global.h" /* my_bool */
  16. #if !defined(_WIN32)
  17. #include <pthread.h>
  18. #endif
  19. #ifndef ETIME
  20. #define ETIME ETIMEDOUT /* For FreeBSD */
  21. #endif
  22. #ifndef ETIMEDOUT
  23. #define ETIMEDOUT 145 /* Win32 doesn't have this */
  24. #endif
  25. /*
  26. MySQL can survive with 32K, but some glibc libraries require > 128K stack
  27. To resolve hostnames. Also recursive stored procedures needs stack.
  28. */
  29. #if SIZEOF_CHARP > 4
  30. #define DEFAULT_THREAD_STACK (256*1024L)
  31. #else
  32. #define DEFAULT_THREAD_STACK (192*1024)
  33. #endif
  34. #ifdef __cplusplus
  35. #define EXTERNC extern "C"
  36. #else
  37. #define EXTERNC
  38. #endif
  39. C_MODE_START
  40. #ifdef _WIN32
  41. typedef volatile LONG my_thread_once_t;
  42. typedef DWORD my_thread_t;
  43. typedef struct thread_attr
  44. {
  45. DWORD dwStackSize;
  46. int detachstate;
  47. } my_thread_attr_t;
  48. #define MY_THREAD_CREATE_JOINABLE 0
  49. #define MY_THREAD_CREATE_DETACHED 1
  50. typedef void * (__cdecl *my_start_routine)(void *);
  51. #define MY_THREAD_ONCE_INIT 0
  52. #define MY_THREAD_ONCE_INPROGRESS 1
  53. #define MY_THREAD_ONCE_DONE 2
  54. #else
  55. typedef pthread_once_t my_thread_once_t;
  56. typedef pthread_t my_thread_t;
  57. typedef pthread_attr_t my_thread_attr_t;
  58. #define MY_THREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
  59. #define MY_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
  60. typedef void *(* my_start_routine)(void *);
  61. #define MY_THREAD_ONCE_INIT PTHREAD_ONCE_INIT
  62. #endif
  63. typedef struct st_my_thread_handle
  64. {
  65. my_thread_t thread;
  66. #ifdef _WIN32
  67. HANDLE handle;
  68. #endif
  69. } my_thread_handle;
  70. int my_thread_once(my_thread_once_t *once_control, void (*init_routine)(void));
  71. static inline my_thread_t my_thread_self()
  72. {
  73. #ifdef _WIN32
  74. return GetCurrentThreadId();
  75. #else
  76. return pthread_self();
  77. #endif
  78. }
  79. static inline int my_thread_equal(my_thread_t t1, my_thread_t t2)
  80. {
  81. #ifdef _WIN32
  82. return t1 == t2;
  83. #else
  84. return pthread_equal(t1, t2);
  85. #endif
  86. }
  87. static inline int my_thread_attr_init(my_thread_attr_t *attr)
  88. {
  89. #ifdef _WIN32
  90. attr->dwStackSize= 0;
  91. /* Set to joinable by default to match Linux */
  92. attr->detachstate= MY_THREAD_CREATE_JOINABLE;
  93. return 0;
  94. #else
  95. return pthread_attr_init(attr);
  96. #endif
  97. }
  98. static inline int my_thread_attr_destroy(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_destroy(attr);
  107. #endif
  108. }
  109. static inline int my_thread_attr_setstacksize(my_thread_attr_t *attr,
  110. size_t stacksize)
  111. {
  112. #ifdef _WIN32
  113. attr->dwStackSize= (DWORD)stacksize;
  114. return 0;
  115. #else
  116. return pthread_attr_setstacksize(attr, stacksize);
  117. #endif
  118. }
  119. static inline int my_thread_attr_setdetachstate(my_thread_attr_t *attr,
  120. int detachstate)
  121. {
  122. #ifdef _WIN32
  123. attr->detachstate= detachstate;
  124. return 0;
  125. #else
  126. return pthread_attr_setdetachstate(attr, detachstate);
  127. #endif
  128. }
  129. static inline int my_thread_attr_getstacksize(my_thread_attr_t *attr,
  130. size_t *stacksize)
  131. {
  132. #ifdef _WIN32
  133. *stacksize= (size_t)attr->dwStackSize;
  134. return 0;
  135. #else
  136. return pthread_attr_getstacksize(attr, stacksize);
  137. #endif
  138. }
  139. static inline void my_thread_yield()
  140. {
  141. #ifdef _WIN32
  142. SwitchToThread();
  143. #else
  144. sched_yield();
  145. #endif
  146. }
  147. int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr,
  148. my_start_routine func, void *arg);
  149. int my_thread_join(my_thread_handle *thread, void **value_ptr);
  150. int my_thread_cancel(my_thread_handle *thread);
  151. void my_thread_exit(void *value_ptr);
  152. extern my_bool my_thread_global_init();
  153. extern void my_thread_global_reinit();
  154. extern void my_thread_global_end();
  155. extern my_bool my_thread_init();
  156. extern void my_thread_end();
  157. C_MODE_END
  158. #endif /* MY_THREAD_INCLUDED */