my_thr_init.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* Copyright (c) 2000, 2019, 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. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. /**
  23. @file mysys/my_thr_init.cc
  24. Functions to handle initialization and allocation of all mysys & debug
  25. thread variables.
  26. */
  27. #include <stdlib.h>
  28. #include <sys/types.h>
  29. #ifdef _WIN32
  30. #include <signal.h>
  31. #endif
  32. #include <time.h>
  33. #include "my_dbug.h"
  34. #include "my_inttypes.h"
  35. #include "my_loglevel.h"
  36. #include "my_macros.h"
  37. #include "my_psi_config.h"
  38. #include "my_sys.h"
  39. #include "my_systime.h"
  40. #include "my_thread.h"
  41. #include "my_thread_local.h"
  42. #include "mysql/psi/mysql_cond.h"
  43. #include "mysql/psi/mysql_mutex.h"
  44. #include "mysql/psi/mysql_thread.h"
  45. #include "mysql/psi/psi_thread.h"
  46. #include "mysys/mysys_priv.h"
  47. #include "mysys_err.h"
  48. #include "thr_mutex.h"
  49. static bool my_thread_global_init_done = false;
  50. #ifndef DBUG_OFF
  51. static uint THR_thread_count = 0;
  52. static Timeout_type my_thread_end_wait_time = 5;
  53. static my_thread_id thread_id = 0;
  54. struct st_my_thread_var;
  55. static thread_local st_my_thread_var *THR_mysys = nullptr;
  56. #endif
  57. static thread_local int THR_myerrno = 0;
  58. #ifdef _WIN32
  59. static thread_local int THR_winerrno = 0;
  60. #endif
  61. mysql_mutex_t THR_LOCK_myisam_mmap;
  62. mysql_mutex_t THR_LOCK_myisam;
  63. mysql_mutex_t THR_LOCK_heap;
  64. mysql_mutex_t THR_LOCK_malloc;
  65. mysql_mutex_t THR_LOCK_open;
  66. mysql_mutex_t THR_LOCK_lock;
  67. mysql_mutex_t THR_LOCK_net;
  68. mysql_mutex_t THR_LOCK_charset;
  69. #ifndef DBUG_OFF
  70. mysql_mutex_t THR_LOCK_threads;
  71. mysql_cond_t THR_COND_threads;
  72. #endif
  73. #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  74. native_mutexattr_t my_fast_mutexattr;
  75. #endif
  76. #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  77. native_mutexattr_t my_errorcheck_mutexattr;
  78. #endif
  79. #ifdef _WIN32
  80. static void install_sigabrt_handler();
  81. #endif
  82. #ifndef DBUG_OFF
  83. struct st_my_thread_var {
  84. my_thread_id id;
  85. struct CODE_STATE *dbug;
  86. };
  87. static struct st_my_thread_var *mysys_thread_var() { return THR_mysys; }
  88. static int set_mysys_thread_var(struct st_my_thread_var *mysys_var) {
  89. THR_mysys = mysys_var;
  90. return 0;
  91. }
  92. #endif
  93. /**
  94. Re-initialize components initialized early with @c my_thread_global_init.
  95. Some mutexes were initialized before the instrumentation.
  96. Destroy + create them again, now that the instrumentation
  97. is in place.
  98. This is safe, since this function() is called before creating new threads,
  99. so the mutexes are not in use.
  100. */
  101. void my_thread_global_reinit() {
  102. DBUG_ASSERT(my_thread_global_init_done);
  103. #ifdef HAVE_PSI_INTERFACE
  104. my_init_mysys_psi_keys();
  105. #endif
  106. mysql_mutex_destroy(&THR_LOCK_heap);
  107. mysql_mutex_init(key_THR_LOCK_heap, &THR_LOCK_heap, MY_MUTEX_INIT_FAST);
  108. mysql_mutex_destroy(&THR_LOCK_net);
  109. mysql_mutex_init(key_THR_LOCK_net, &THR_LOCK_net, MY_MUTEX_INIT_FAST);
  110. mysql_mutex_destroy(&THR_LOCK_myisam);
  111. mysql_mutex_init(key_THR_LOCK_myisam, &THR_LOCK_myisam, MY_MUTEX_INIT_SLOW);
  112. mysql_mutex_destroy(&THR_LOCK_malloc);
  113. mysql_mutex_init(key_THR_LOCK_malloc, &THR_LOCK_malloc, MY_MUTEX_INIT_FAST);
  114. mysql_mutex_destroy(&THR_LOCK_open);
  115. mysql_mutex_init(key_THR_LOCK_open, &THR_LOCK_open, MY_MUTEX_INIT_FAST);
  116. mysql_mutex_destroy(&THR_LOCK_charset);
  117. mysql_mutex_init(key_THR_LOCK_charset, &THR_LOCK_charset, MY_MUTEX_INIT_FAST);
  118. #ifndef DBUG_OFF
  119. mysql_mutex_destroy(&THR_LOCK_threads);
  120. mysql_mutex_init(key_THR_LOCK_threads, &THR_LOCK_threads, MY_MUTEX_INIT_FAST);
  121. mysql_cond_destroy(&THR_COND_threads);
  122. mysql_cond_init(key_THR_COND_threads, &THR_COND_threads);
  123. #endif
  124. }
  125. /**
  126. initialize thread environment
  127. @retval false ok
  128. @retval true error
  129. */
  130. bool my_thread_global_init() {
  131. if (my_thread_global_init_done) return false;
  132. my_thread_global_init_done = true;
  133. #if defined(SAFE_MUTEX)
  134. safe_mutex_global_init(); /* Must be called early */
  135. #endif
  136. #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  137. /*
  138. Set mutex type to "fast" a.k.a "adaptive"
  139. In this case the thread may steal the mutex from some other thread
  140. that is waiting for the same mutex. This will save us some
  141. context switches but may cause a thread to 'starve forever' while
  142. waiting for the mutex (not likely if the code within the mutex is
  143. short).
  144. */
  145. pthread_mutexattr_init(&my_fast_mutexattr);
  146. pthread_mutexattr_settype(&my_fast_mutexattr, PTHREAD_MUTEX_ADAPTIVE_NP);
  147. #endif
  148. #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  149. /*
  150. Set mutex type to "errorcheck"
  151. */
  152. pthread_mutexattr_init(&my_errorcheck_mutexattr);
  153. pthread_mutexattr_settype(&my_errorcheck_mutexattr, PTHREAD_MUTEX_ERRORCHECK);
  154. #endif
  155. mysql_mutex_init(key_THR_LOCK_malloc, &THR_LOCK_malloc, MY_MUTEX_INIT_FAST);
  156. mysql_mutex_init(key_THR_LOCK_open, &THR_LOCK_open, MY_MUTEX_INIT_FAST);
  157. mysql_mutex_init(key_THR_LOCK_charset, &THR_LOCK_charset, MY_MUTEX_INIT_FAST);
  158. mysql_mutex_init(key_THR_LOCK_lock, &THR_LOCK_lock, MY_MUTEX_INIT_FAST);
  159. mysql_mutex_init(key_THR_LOCK_myisam, &THR_LOCK_myisam, MY_MUTEX_INIT_SLOW);
  160. mysql_mutex_init(key_THR_LOCK_myisam_mmap, &THR_LOCK_myisam_mmap,
  161. MY_MUTEX_INIT_FAST);
  162. mysql_mutex_init(key_THR_LOCK_heap, &THR_LOCK_heap, MY_MUTEX_INIT_FAST);
  163. mysql_mutex_init(key_THR_LOCK_net, &THR_LOCK_net, MY_MUTEX_INIT_FAST);
  164. #ifndef DBUG_OFF
  165. mysql_mutex_init(key_THR_LOCK_threads, &THR_LOCK_threads, MY_MUTEX_INIT_FAST);
  166. mysql_cond_init(key_THR_COND_threads, &THR_COND_threads);
  167. #endif
  168. return false;
  169. }
  170. void my_thread_global_end() {
  171. #ifndef DBUG_OFF
  172. struct timespec abstime;
  173. bool all_threads_killed = true;
  174. set_timespec(&abstime, my_thread_end_wait_time);
  175. mysql_mutex_lock(&THR_LOCK_threads);
  176. while (THR_thread_count > 0) {
  177. int error =
  178. mysql_cond_timedwait(&THR_COND_threads, &THR_LOCK_threads, &abstime);
  179. if (is_timeout(error)) {
  180. #ifndef _WIN32
  181. /*
  182. We shouldn't give an error here, because if we don't have
  183. pthread_kill(), programs like mysqld can't ensure that all threads
  184. are killed when we enter here.
  185. */
  186. if (THR_thread_count) /* purecov: begin inspected */
  187. my_message_local(ERROR_LEVEL, EE_FAILED_TO_KILL_ALL_THREADS,
  188. THR_thread_count);
  189. /* purecov: end */
  190. #endif
  191. all_threads_killed = false;
  192. break;
  193. }
  194. }
  195. mysql_mutex_unlock(&THR_LOCK_threads);
  196. #endif
  197. #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  198. pthread_mutexattr_destroy(&my_fast_mutexattr);
  199. #endif
  200. #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  201. pthread_mutexattr_destroy(&my_errorcheck_mutexattr);
  202. #endif
  203. mysql_mutex_destroy(&THR_LOCK_malloc);
  204. mysql_mutex_destroy(&THR_LOCK_open);
  205. mysql_mutex_destroy(&THR_LOCK_lock);
  206. mysql_mutex_destroy(&THR_LOCK_myisam);
  207. mysql_mutex_destroy(&THR_LOCK_myisam_mmap);
  208. mysql_mutex_destroy(&THR_LOCK_heap);
  209. mysql_mutex_destroy(&THR_LOCK_net);
  210. mysql_mutex_destroy(&THR_LOCK_charset);
  211. #ifndef DBUG_OFF
  212. if (all_threads_killed) {
  213. mysql_mutex_destroy(&THR_LOCK_threads);
  214. mysql_cond_destroy(&THR_COND_threads);
  215. }
  216. #endif
  217. my_thread_global_init_done = false;
  218. }
  219. /**
  220. Allocate thread specific memory for the thread, used by mysys and dbug
  221. @note This function may called multiple times for a thread, for example
  222. if one uses my_init() followed by mysql_server_init().
  223. @retval false ok
  224. @retval true Fatal error; mysys/dbug functions can't be used
  225. */
  226. extern "C" bool my_thread_init() {
  227. #ifndef DBUG_OFF
  228. struct st_my_thread_var *tmp;
  229. #endif
  230. if (!my_thread_global_init_done)
  231. return true; /* cannot proceed with unintialized library */
  232. #ifdef _WIN32
  233. install_sigabrt_handler();
  234. #endif
  235. #ifndef DBUG_OFF
  236. if (mysys_thread_var()) return false;
  237. if (!(tmp = (struct st_my_thread_var *)calloc(1, sizeof(*tmp)))) return true;
  238. mysql_mutex_lock(&THR_LOCK_threads);
  239. tmp->id = ++thread_id;
  240. ++THR_thread_count;
  241. mysql_mutex_unlock(&THR_LOCK_threads);
  242. set_mysys_thread_var(tmp);
  243. #endif
  244. return false;
  245. }
  246. /**
  247. Deallocate memory used by the thread for book-keeping
  248. @note This may be called multiple times for a thread.
  249. This happens for example when one calls 'mysql_server_init()'
  250. mysql_server_end() and then ends with a mysql_end().
  251. */
  252. extern "C" void my_thread_end() {
  253. #ifndef DBUG_OFF
  254. struct st_my_thread_var *tmp = mysys_thread_var();
  255. #endif
  256. #ifdef HAVE_PSI_THREAD_INTERFACE
  257. /*
  258. Remove the instrumentation for this thread.
  259. This must be done before trashing st_my_thread_var,
  260. because the LF_HASH depends on it.
  261. */
  262. PSI_THREAD_CALL(delete_current_thread)();
  263. #endif
  264. #if !defined(DBUG_OFF)
  265. if (tmp) {
  266. /* tmp->dbug is allocated inside DBUG library */
  267. if (tmp->dbug) {
  268. DBUG_POP();
  269. free(tmp->dbug);
  270. tmp->dbug = NULL;
  271. }
  272. free(tmp);
  273. /*
  274. Decrement counter for number of running threads. We are using this
  275. in my_thread_global_end() to wait until all threads have called
  276. my_thread_end and thus freed all memory they have allocated in
  277. my_thread_init() and DBUG_xxxx
  278. */
  279. mysql_mutex_lock(&THR_LOCK_threads);
  280. DBUG_ASSERT(THR_thread_count != 0);
  281. if (--THR_thread_count == 0) mysql_cond_signal(&THR_COND_threads);
  282. mysql_mutex_unlock(&THR_LOCK_threads);
  283. }
  284. set_mysys_thread_var(NULL);
  285. #endif
  286. }
  287. int my_errno() { return THR_myerrno; }
  288. void set_my_errno(int my_errno) { THR_myerrno = my_errno; }
  289. #ifdef _WIN32
  290. int thr_winerr() { return THR_winerrno; }
  291. void set_thr_winerr(int winerr) { THR_winerrno = winerr; }
  292. #endif
  293. #ifndef DBUG_OFF
  294. my_thread_id my_thread_var_id() { return mysys_thread_var()->id; }
  295. void set_my_thread_var_id(my_thread_id id) { mysys_thread_var()->id = id; }
  296. CODE_STATE **my_thread_var_dbug() {
  297. struct st_my_thread_var *tmp = THR_mysys;
  298. return tmp ? &tmp->dbug : NULL;
  299. }
  300. #endif /* DBUG_OFF */
  301. #ifdef _WIN32
  302. /*
  303. In Visual Studio 2005 and later, default SIGABRT handler will overwrite
  304. any unhandled exception filter set by the application and will try to
  305. call JIT debugger. This is not what we want, this we calling __debugbreak
  306. to stop in debugger, if process is being debugged or to generate
  307. EXCEPTION_BREAKPOINT and then handle_segfault will do its magic.
  308. */
  309. static void my_sigabrt_handler(int sig) { __debugbreak(); }
  310. static void install_sigabrt_handler() {
  311. /*abort() should not override our exception filter*/
  312. _set_abort_behavior(0, _CALL_REPORTFAULT);
  313. signal(SIGABRT, my_sigabrt_handler);
  314. }
  315. #endif