MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_thr_init.c
1 /* Copyright (c) 2000, 2011, 2012 Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /*
17  Functions to handle initializating and allocationg of all mysys & debug
18  thread variables.
19 */
20 
21 #include "mysys_priv.h"
22 #include <m_string.h>
23 #include <signal.h>
24 
25 pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
26 mysql_mutex_t THR_LOCK_malloc, THR_LOCK_open,
27  THR_LOCK_lock, THR_LOCK_myisam, THR_LOCK_heap,
28  THR_LOCK_net, THR_LOCK_charset, THR_LOCK_threads,
29  THR_LOCK_myisam_mmap;
30 
31 mysql_cond_t THR_COND_threads;
32 uint THR_thread_count= 0;
33 uint my_thread_end_wait_time= 5;
34 #if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
35 mysql_mutex_t LOCK_localtime_r;
36 #endif
37 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
38 pthread_mutexattr_t my_fast_mutexattr;
39 #endif
40 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
41 pthread_mutexattr_t my_errorcheck_mutexattr;
42 #endif
43 #ifdef _MSC_VER
44 static void install_sigabrt_handler();
45 #endif
46 #ifdef TARGET_OS_LINUX
47 
48 /*
49  Dummy thread spawned in my_thread_global_init() below to avoid
50  race conditions in NPTL pthread_exit code.
51 */
52 
53 static pthread_handler_t
54 nptl_pthread_exit_hack_handler(void *arg __attribute((unused)))
55 {
56  /* Do nothing! */
57  pthread_exit(0);
58  return 0;
59 }
60 
61 #endif /* TARGET_OS_LINUX */
62 
63 
64 static uint get_thread_lib(void);
65 
67 static my_bool my_thread_global_init_done= 0;
68 
69 
78 void my_thread_global_reinit(void)
79 {
80  struct st_my_thread_var *tmp;
81 
82  DBUG_ASSERT(my_thread_global_init_done);
83 
84 #ifdef HAVE_PSI_INTERFACE
85  my_init_mysys_psi_keys();
86 #endif
87 
88  mysql_mutex_destroy(&THR_LOCK_heap);
89  mysql_mutex_init(key_THR_LOCK_heap, &THR_LOCK_heap, MY_MUTEX_INIT_FAST);
90 
91  mysql_mutex_destroy(&THR_LOCK_net);
92  mysql_mutex_init(key_THR_LOCK_net, &THR_LOCK_net, MY_MUTEX_INIT_FAST);
93 
94  mysql_mutex_destroy(&THR_LOCK_myisam);
95  mysql_mutex_init(key_THR_LOCK_myisam, &THR_LOCK_myisam, MY_MUTEX_INIT_SLOW);
96 
97  mysql_mutex_destroy(&THR_LOCK_malloc);
98  mysql_mutex_init(key_THR_LOCK_malloc, &THR_LOCK_malloc, MY_MUTEX_INIT_FAST);
99 
100  mysql_mutex_destroy(&THR_LOCK_open);
101  mysql_mutex_init(key_THR_LOCK_open, &THR_LOCK_open, MY_MUTEX_INIT_FAST);
102 
103  mysql_mutex_destroy(&THR_LOCK_charset);
104  mysql_mutex_init(key_THR_LOCK_charset, &THR_LOCK_charset, MY_MUTEX_INIT_FAST);
105 
106  mysql_mutex_destroy(&THR_LOCK_threads);
107  mysql_mutex_init(key_THR_LOCK_threads, &THR_LOCK_threads, MY_MUTEX_INIT_FAST);
108 
109  mysql_cond_destroy(&THR_COND_threads);
110  mysql_cond_init(key_THR_COND_threads, &THR_COND_threads, NULL);
111 
112  tmp= my_pthread_getspecific(struct st_my_thread_var*, THR_KEY_mysys);
113  DBUG_ASSERT(tmp);
114 
115  mysql_mutex_destroy(&tmp->mutex);
116  mysql_mutex_init(key_my_thread_var_mutex, &tmp->mutex, MY_MUTEX_INIT_FAST);
117 
118  mysql_cond_destroy(&tmp->suspend);
119  mysql_cond_init(key_my_thread_var_suspend, &tmp->suspend, NULL);
120 }
121 
122 /*
123  initialize thread environment
124 
125  SYNOPSIS
126  my_thread_global_init()
127 
128  RETURN
129  0 ok
130  1 error (Couldn't create THR_KEY_mysys)
131 */
132 
133 my_bool my_thread_global_init(void)
134 {
135  int pth_ret;
136 
137  if (my_thread_global_init_done)
138  return 0;
139  my_thread_global_init_done= 1;
140 
141 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
142  /*
143  Set mutex type to "fast" a.k.a "adaptive"
144 
145  In this case the thread may steal the mutex from some other thread
146  that is waiting for the same mutex. This will save us some
147  context switches but may cause a thread to 'starve forever' while
148  waiting for the mutex (not likely if the code within the mutex is
149  short).
150  */
151  pthread_mutexattr_init(&my_fast_mutexattr);
152  pthread_mutexattr_settype(&my_fast_mutexattr,
153  PTHREAD_MUTEX_ADAPTIVE_NP);
154 #endif
155 
156 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
157  /*
158  Set mutex type to "errorcheck"
159  */
160  pthread_mutexattr_init(&my_errorcheck_mutexattr);
161  pthread_mutexattr_settype(&my_errorcheck_mutexattr,
162  PTHREAD_MUTEX_ERRORCHECK);
163 #endif
164 
165  if ((pth_ret= pthread_key_create(&THR_KEY_mysys, NULL)) != 0)
166  {
167  fprintf(stderr, "Can't initialize threads: error %d\n", pth_ret);
168  return 1;
169  }
170 
171  mysql_mutex_init(key_THR_LOCK_malloc, &THR_LOCK_malloc, MY_MUTEX_INIT_FAST);
172  mysql_mutex_init(key_THR_LOCK_open, &THR_LOCK_open, MY_MUTEX_INIT_FAST);
173  mysql_mutex_init(key_THR_LOCK_charset, &THR_LOCK_charset, MY_MUTEX_INIT_FAST);
174  mysql_mutex_init(key_THR_LOCK_threads, &THR_LOCK_threads, MY_MUTEX_INIT_FAST);
175 
176  if (my_thread_init())
177  return 1;
178 
179  thd_lib_detected= get_thread_lib();
180 
181 #ifdef TARGET_OS_LINUX
182  /*
183  BUG#24507: Race conditions inside current NPTL pthread_exit()
184  implementation.
185 
186  To avoid a possible segmentation fault during concurrent
187  executions of pthread_exit(), a dummy thread is spawned which
188  initializes internal variables of pthread lib. See bug description
189  for a full explanation.
190 
191  TODO: Remove this code when fixed versions of glibc6 are in common
192  use.
193  */
194  if (thd_lib_detected == THD_LIB_NPTL)
195  {
196  pthread_t dummy_thread;
197  pthread_attr_t dummy_thread_attr;
198 
199  pthread_attr_init(&dummy_thread_attr);
200  pthread_attr_setdetachstate(&dummy_thread_attr, PTHREAD_CREATE_JOINABLE);
201 
202  if (pthread_create(&dummy_thread,&dummy_thread_attr,
203  nptl_pthread_exit_hack_handler, NULL) == 0)
204  (void)pthread_join(dummy_thread, NULL);
205  }
206 #endif /* TARGET_OS_LINUX */
207 
208  mysql_mutex_init(key_THR_LOCK_lock, &THR_LOCK_lock, MY_MUTEX_INIT_FAST);
209  mysql_mutex_init(key_THR_LOCK_myisam, &THR_LOCK_myisam, MY_MUTEX_INIT_SLOW);
210  mysql_mutex_init(key_THR_LOCK_myisam_mmap, &THR_LOCK_myisam_mmap, MY_MUTEX_INIT_FAST);
211  mysql_mutex_init(key_THR_LOCK_heap, &THR_LOCK_heap, MY_MUTEX_INIT_FAST);
212  mysql_mutex_init(key_THR_LOCK_net, &THR_LOCK_net, MY_MUTEX_INIT_FAST);
213  mysql_cond_init(key_THR_COND_threads, &THR_COND_threads, NULL);
214 
215 #if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
216  mysql_mutex_init(key_LOCK_localtime_r, &LOCK_localtime_r, MY_MUTEX_INIT_SLOW);
217 #endif
218 
219 #ifdef _MSC_VER
220  install_sigabrt_handler();
221 #endif
222 
223  return 0;
224 }
225 
226 
227 void my_thread_global_end(void)
228 {
229  struct timespec abstime;
230  my_bool all_threads_killed= 1;
231 
232  set_timespec(abstime, my_thread_end_wait_time);
233  mysql_mutex_lock(&THR_LOCK_threads);
234  while (THR_thread_count > 0)
235  {
236  int error= mysql_cond_timedwait(&THR_COND_threads, &THR_LOCK_threads,
237  &abstime);
238  if (error == ETIMEDOUT || error == ETIME)
239  {
240 #ifdef HAVE_PTHREAD_KILL
241  /*
242  We shouldn't give an error here, because if we don't have
243  pthread_kill(), programs like mysqld can't ensure that all threads
244  are killed when we enter here.
245  */
246  if (THR_thread_count)
247  fprintf(stderr,
248  "Error in my_thread_global_end(): %d threads didn't exit\n",
249  THR_thread_count);
250 #endif
251  all_threads_killed= 0;
252  break;
253  }
254  }
255  mysql_mutex_unlock(&THR_LOCK_threads);
256 
257  pthread_key_delete(THR_KEY_mysys);
258 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
259  pthread_mutexattr_destroy(&my_fast_mutexattr);
260 #endif
261 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
262  pthread_mutexattr_destroy(&my_errorcheck_mutexattr);
263 #endif
264  mysql_mutex_destroy(&THR_LOCK_malloc);
265  mysql_mutex_destroy(&THR_LOCK_open);
266  mysql_mutex_destroy(&THR_LOCK_lock);
267  mysql_mutex_destroy(&THR_LOCK_myisam);
268  mysql_mutex_destroy(&THR_LOCK_myisam_mmap);
269  mysql_mutex_destroy(&THR_LOCK_heap);
270  mysql_mutex_destroy(&THR_LOCK_net);
271  mysql_mutex_destroy(&THR_LOCK_charset);
272  if (all_threads_killed)
273  {
274  mysql_mutex_destroy(&THR_LOCK_threads);
275  mysql_cond_destroy(&THR_COND_threads);
276  }
277 #if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
278  mysql_mutex_destroy(&LOCK_localtime_r);
279 #endif
280 
281  my_thread_global_init_done= 0;
282 }
283 
284 static my_thread_id thread_id= 0;
285 
286 /*
287  Allocate thread specific memory for the thread, used by mysys and dbug
288 
289  SYNOPSIS
290  my_thread_init()
291 
292  NOTES
293  We can't use mutex_locks here if we are using windows as
294  we may have compiled the program with SAFE_MUTEX, in which
295  case the checking of mutex_locks will not work until
296  the pthread_self thread specific variable is initialized.
297 
298  This function may called multiple times for a thread, for example
299  if one uses my_init() followed by mysql_server_init().
300 
301  RETURN
302  0 ok
303  1 Fatal error; mysys/dbug functions can't be used
304 */
305 
306 my_bool my_thread_init(void)
307 {
308  struct st_my_thread_var *tmp;
309  my_bool error=0;
310 
311 #ifdef EXTRA_DEBUG_THREADS
312  fprintf(stderr,"my_thread_init(): thread_id: 0x%lx\n",
313  (ulong) pthread_self());
314 #endif
315 
316  if (my_pthread_getspecific(struct st_my_thread_var *,THR_KEY_mysys))
317  {
318 #ifdef EXTRA_DEBUG_THREADS
319  fprintf(stderr,"my_thread_init() called more than once in thread 0x%lx\n",
320  (long) pthread_self());
321 #endif
322  goto end;
323  }
324 
325 #ifdef _MSC_VER
326  install_sigabrt_handler();
327 #endif
328 
329  if (!(tmp= (struct st_my_thread_var *) calloc(1, sizeof(*tmp))))
330  {
331  error= 1;
332  goto end;
333  }
334  pthread_setspecific(THR_KEY_mysys,tmp);
335  tmp->pthread_self= pthread_self();
336  mysql_mutex_init(key_my_thread_var_mutex, &tmp->mutex, MY_MUTEX_INIT_FAST);
337  mysql_cond_init(key_my_thread_var_suspend, &tmp->suspend, NULL);
338 
339  tmp->stack_ends_here= (char*)&tmp +
340  STACK_DIRECTION * (long)my_thread_stack_size;
341 
342  mysql_mutex_lock(&THR_LOCK_threads);
343  tmp->id= ++thread_id;
344  ++THR_thread_count;
345  mysql_mutex_unlock(&THR_LOCK_threads);
346  tmp->init= 1;
347 #ifndef DBUG_OFF
348  /* Generate unique name for thread */
349  (void) my_thread_name();
350 #endif
351 
352 end:
353  return error;
354 }
355 
356 
357 /*
358  Deallocate memory used by the thread for book-keeping
359 
360  SYNOPSIS
361  my_thread_end()
362 
363  NOTE
364  This may be called multiple times for a thread.
365  This happens for example when one calls 'mysql_server_init()'
366  mysql_server_end() and then ends with a mysql_end().
367 */
368 
369 void my_thread_end(void)
370 {
371  struct st_my_thread_var *tmp;
372  tmp= my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
373 
374 #ifdef EXTRA_DEBUG_THREADS
375  fprintf(stderr,"my_thread_end(): tmp: 0x%lx pthread_self: 0x%lx thread_id: %ld\n",
376  (long) tmp, (long) pthread_self(), tmp ? (long) tmp->id : 0L);
377 #endif
378 
379 #ifdef HAVE_PSI_INTERFACE
380  /*
381  Remove the instrumentation for this thread.
382  This must be done before trashing st_my_thread_var,
383  because the LF_HASH depends on it.
384  */
385  PSI_THREAD_CALL(delete_current_thread)();
386 #endif
387 
388  if (tmp && tmp->init)
389  {
390 #if !defined(DBUG_OFF)
391  /* tmp->dbug is allocated inside DBUG library */
392  if (tmp->dbug)
393  {
394  DBUG_POP();
395  free(tmp->dbug);
396  tmp->dbug=0;
397  }
398 #endif
399 #if !defined(__bsdi__) && !defined(__OpenBSD__)
400  /* bsdi and openbsd 3.5 dumps core here */
401  mysql_cond_destroy(&tmp->suspend);
402 #endif
403  mysql_mutex_destroy(&tmp->mutex);
404  free(tmp);
405 
406  /*
407  Decrement counter for number of running threads. We are using this
408  in my_thread_global_end() to wait until all threads have called
409  my_thread_end and thus freed all memory they have allocated in
410  my_thread_init() and DBUG_xxxx
411  */
412  mysql_mutex_lock(&THR_LOCK_threads);
413  DBUG_ASSERT(THR_thread_count != 0);
414  if (--THR_thread_count == 0)
415  mysql_cond_signal(&THR_COND_threads);
416  mysql_mutex_unlock(&THR_LOCK_threads);
417  }
418  pthread_setspecific(THR_KEY_mysys,0);
419 }
420 
421 struct st_my_thread_var *_my_thread_var(void)
422 {
423  return my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
424 }
425 
426 int set_mysys_var(struct st_my_thread_var *mysys_var)
427 {
428  return my_pthread_setspecific_ptr(THR_KEY_mysys, mysys_var);
429 }
430 
431 /****************************************************************************
432  Get name of current thread.
433 ****************************************************************************/
434 
435 my_thread_id my_thread_dbug_id()
436 {
437  return my_thread_var->id;
438 }
439 
440 #ifdef DBUG_OFF
441 const char *my_thread_name(void)
442 {
443  return "no_name";
444 }
445 
446 #else
447 
448 const char *my_thread_name(void)
449 {
450  char name_buff[100];
451  struct st_my_thread_var *tmp=my_thread_var;
452  if (!tmp->name[0])
453  {
454  my_thread_id id= my_thread_dbug_id();
455  sprintf(name_buff,"T@%lu", (ulong) id);
456  strmake(tmp->name,name_buff,THREAD_NAME_SIZE);
457  }
458  return tmp->name;
459 }
460 
461 /* Return pointer to DBUG for holding current state */
462 
463 extern void **my_thread_var_dbug()
464 {
465  struct st_my_thread_var *tmp=
466  my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
467  return tmp && tmp->init ? &tmp->dbug : 0;
468 }
469 #endif /* DBUG_OFF */
470 
471 
472 static uint get_thread_lib(void)
473 {
474 #ifdef _CS_GNU_LIBPTHREAD_VERSION
475  char buff[64];
476 
477  confstr(_CS_GNU_LIBPTHREAD_VERSION, buff, sizeof(buff));
478 
479  if (!strncasecmp(buff, "NPTL", 4))
480  return THD_LIB_NPTL;
481  if (!strncasecmp(buff, "linuxthreads", 12))
482  return THD_LIB_LT;
483 #endif
484  return THD_LIB_OTHER;
485 }
486 
487 #ifdef _WIN32
488 /*
489  In Visual Studio 2005 and later, default SIGABRT handler will overwrite
490  any unhandled exception filter set by the application and will try to
491  call JIT debugger. This is not what we want, this we calling __debugbreak
492  to stop in debugger, if process is being debugged or to generate
493  EXCEPTION_BREAKPOINT and then handle_segfault will do its magic.
494 */
495 
496 #if (_MSC_VER >= 1400)
497 static void my_sigabrt_handler(int sig)
498 {
499  __debugbreak();
500 }
501 #endif /*_MSC_VER >=1400 */
502 
503 static void install_sigabrt_handler(void)
504 {
505 #if (_MSC_VER >=1400)
506  /*abort() should not override our exception filter*/
507  _set_abort_behavior(0,_CALL_REPORTFAULT);
508  signal(SIGABRT,my_sigabrt_handler);
509 #endif /* _MSC_VER >=1400 */
510 }
511 #endif
512