21
21
#include "mysys_priv.h"
22
#include <mysys/my_pthread.h>
22
23
#include <mystrings/m_string.h>
23
26
#include <signal.h>
28
#if TIME_WITH_SYS_TIME
29
# include <sys/time.h>
33
# include <sys/time.h>
25
39
uint32_t thd_lib_detected= 0;
28
pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
30
pthread_key(struct st_my_thread_var, THR_KEY_mysys);
32
pthread_mutex_t THR_LOCK_malloc,THR_LOCK_open,
33
THR_LOCK_lock,THR_LOCK_isam,THR_LOCK_myisam,THR_LOCK_heap,
34
THR_LOCK_net, THR_LOCK_charset, THR_LOCK_threads, THR_LOCK_time;
41
pthread_key_t THR_KEY_mysys;
42
pthread_mutex_t THR_LOCK_open;
43
pthread_mutex_t THR_LOCK_lock;
44
pthread_mutex_t THR_LOCK_threads;
35
45
pthread_cond_t THR_COND_threads;
36
46
uint32_t THR_thread_count= 0;
37
47
uint32_t my_thread_end_wait_time= 5;
38
#if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
39
pthread_mutex_t LOCK_localtime_r;
41
#ifndef HAVE_GETHOSTBYNAME_R
42
pthread_mutex_t LOCK_gethostbyname_r;
44
48
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
45
49
pthread_mutexattr_t my_fast_mutexattr;
93
#ifdef TARGET_OS_LINUX
95
BUG#24507: Race conditions inside current NPTL pthread_exit()
98
To avoid a possible segmentation fault during concurrent
99
executions of pthread_exit(), a dummy thread is spawned which
100
initializes internal variables of pthread lib. See bug description
101
for a full explanation.
103
TODO: Remove this code when fixed versions of glibc6 are in common
106
if (thd_lib_detected == THD_LIB_NPTL)
108
pthread_t dummy_thread;
109
pthread_attr_t dummy_thread_attr;
111
pthread_attr_init(&dummy_thread_attr);
112
pthread_attr_setdetachstate(&dummy_thread_attr, PTHREAD_CREATE_DETACHED);
114
pthread_create(&dummy_thread,&dummy_thread_attr,
115
nptl_pthread_exit_hack_handler, NULL);
117
#endif /* TARGET_OS_LINUX */
119
98
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
121
100
Set mutex type to "fast" a.k.a "adaptive"
139
118
PTHREAD_MUTEX_ERRORCHECK);
142
pthread_mutex_init(&THR_LOCK_malloc,MY_MUTEX_INIT_FAST);
143
121
pthread_mutex_init(&THR_LOCK_open,MY_MUTEX_INIT_FAST);
144
122
pthread_mutex_init(&THR_LOCK_lock,MY_MUTEX_INIT_FAST);
145
pthread_mutex_init(&THR_LOCK_isam,MY_MUTEX_INIT_SLOW);
146
pthread_mutex_init(&THR_LOCK_myisam,MY_MUTEX_INIT_SLOW);
147
pthread_mutex_init(&THR_LOCK_heap,MY_MUTEX_INIT_FAST);
148
pthread_mutex_init(&THR_LOCK_net,MY_MUTEX_INIT_FAST);
149
pthread_mutex_init(&THR_LOCK_charset,MY_MUTEX_INIT_FAST);
150
123
pthread_mutex_init(&THR_LOCK_threads,MY_MUTEX_INIT_FAST);
151
pthread_mutex_init(&THR_LOCK_time,MY_MUTEX_INIT_FAST);
152
124
pthread_cond_init(&THR_COND_threads, NULL);
153
#if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
154
pthread_mutex_init(&LOCK_localtime_r,MY_MUTEX_INIT_SLOW);
156
#ifndef HAVE_GETHOSTBYNAME_R
157
pthread_mutex_init(&LOCK_gethostbyname_r,MY_MUTEX_INIT_SLOW);
159
125
if (my_thread_init())
161
127
my_thread_global_end(); /* Clean up */
202
166
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
203
167
pthread_mutexattr_destroy(&my_errorcheck_mutexattr);
205
pthread_mutex_destroy(&THR_LOCK_malloc);
206
169
pthread_mutex_destroy(&THR_LOCK_open);
207
170
pthread_mutex_destroy(&THR_LOCK_lock);
208
pthread_mutex_destroy(&THR_LOCK_isam);
209
pthread_mutex_destroy(&THR_LOCK_myisam);
210
pthread_mutex_destroy(&THR_LOCK_heap);
211
pthread_mutex_destroy(&THR_LOCK_net);
212
pthread_mutex_destroy(&THR_LOCK_time);
213
pthread_mutex_destroy(&THR_LOCK_charset);
214
171
if (all_threads_killed)
216
173
pthread_mutex_destroy(&THR_LOCK_threads);
217
174
pthread_cond_destroy(&THR_COND_threads);
219
#if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
220
pthread_mutex_destroy(&LOCK_localtime_r);
222
#ifndef HAVE_GETHOSTBYNAME_R
223
pthread_mutex_destroy(&LOCK_gethostbyname_r);
227
178
static my_thread_id thread_id= 0;
251
202
#ifdef EXTRA_DEBUG_THREADS
252
203
fprintf(stderr,"my_thread_init(): thread_id: 0x%lx\n",
253
204
(uint32_t) pthread_self());
256
207
if (pthread_getspecific(THR_KEY_mysys))
258
209
#ifdef EXTRA_DEBUG_THREADS
259
210
fprintf(stderr,"my_thread_init() called more than once in thread 0x%lx\n",
260
211
(long) pthread_self());
264
215
if (!(tmp= (struct st_my_thread_var *) calloc(1, sizeof(*tmp))))
302
253
#ifdef EXTRA_DEBUG_THREADS
303
254
fprintf(stderr,"my_thread_end(): tmp: 0x%lx pthread_self: 0x%lx thread_id: %ld\n",
304
255
(long) tmp, (long) pthread_self(), tmp ? (long) tmp->id : 0L);
306
257
if (tmp && tmp->init)
308
259
#if !defined(__bsdi__) && !defined(__OpenBSD__)
310
261
pthread_cond_destroy(&tmp->suspend);
312
263
pthread_mutex_destroy(&tmp->mutex);
320
267
Decrement counter for number of running threads. We are using this
321
268
in my_thread_global_end() to wait until all threads have called
322
269
my_thread_end and thus freed all memory they have allocated in
325
272
pthread_mutex_lock(&THR_LOCK_threads);
326
273
assert(THR_thread_count != 0);
328
275
pthread_cond_signal(&THR_COND_threads);
329
276
pthread_mutex_unlock(&THR_LOCK_threads);
331
/* The following free has to be done, even if my_thread_var() is 0 */
333
pthread_setspecific(THR_KEY_mysys,0);
337
280
struct st_my_thread_var *_my_thread_var(void)
339
282
struct st_my_thread_var *tmp= (struct st_my_thread_var*)pthread_getspecific(THR_KEY_mysys);
341
/* This can only happen in a .DLL */
345
tmp=my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
363
298
#ifdef _CS_GNU_LIBPTHREAD_VERSION
366
301
confstr(_CS_GNU_LIBPTHREAD_VERSION, buff, sizeof(buff));
368
303
if (!strncasecmp(buff, "NPTL", 4))