~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_thr_init.cc

  • Committer: jay
  • Date: 2008-12-23 00:18:10 UTC
  • Revision ID: jay@piggy.tangent.org-20081223001810-026ibij22q2842k1
Had a --regex-replace by accident. Should have been --replace_column call.  Only showed up in make test, not running single test, because InnoDB key numbers were different with multiple test running.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
*/
20
20
 
21
21
#include "mysys_priv.h"
 
22
#include <mysys/my_pthread.h>
22
23
#include <mystrings/m_string.h>
 
24
 
 
25
#include <stdio.h>
23
26
#include <signal.h>
24
27
 
 
28
#if TIME_WITH_SYS_TIME
 
29
# include <sys/time.h>
 
30
# include <time.h>
 
31
#else
 
32
# if HAVE_SYS_TIME_H
 
33
#  include <sys/time.h>
 
34
# else
 
35
#  include <time.h>
 
36
# endif
 
37
#endif
 
38
 
25
39
uint32_t thd_lib_detected= 0;
26
40
 
27
41
#ifdef USE_TLS
28
 
pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
 
42
pthread_key_t THR_KEY_mysys;
29
43
#else
30
 
pthread_key(struct st_my_thread_var, THR_KEY_mysys);
 
44
pthread_key_t THR_KEY_mysys;
31
45
#endif /* USE_TLS */
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;
 
46
pthread_mutex_t THR_LOCK_open;
 
47
pthread_mutex_t THR_LOCK_lock;
 
48
pthread_mutex_t THR_LOCK_charset;
 
49
pthread_mutex_t THR_LOCK_threads;
 
50
pthread_mutex_t THR_LOCK_time;
35
51
pthread_cond_t  THR_COND_threads;
36
52
uint32_t            THR_thread_count= 0;
37
53
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;
40
 
#endif
41
 
#ifndef HAVE_GETHOSTBYNAME_R
42
 
pthread_mutex_t LOCK_gethostbyname_r;
43
 
#endif
44
54
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
45
55
pthread_mutexattr_t my_fast_mutexattr;
46
56
#endif
55
65
  race conditions in NPTL pthread_exit code.
56
66
*/
57
67
 
58
 
static pthread_handler_t
 
68
extern "C"
 
69
void *
59
70
nptl_pthread_exit_hack_handler(void *arg __attribute((unused)))
60
71
{
61
72
  /* Do nothing! */
90
101
    return 1;
91
102
  }
92
103
 
93
 
#ifdef TARGET_OS_LINUX
94
 
  /*
95
 
    BUG#24507: Race conditions inside current NPTL pthread_exit()
96
 
    implementation.
97
 
 
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.
102
 
 
103
 
    TODO: Remove this code when fixed versions of glibc6 are in common
104
 
    use.
105
 
  */
106
 
  if (thd_lib_detected == THD_LIB_NPTL)
107
 
  {
108
 
    pthread_t       dummy_thread;
109
 
    pthread_attr_t  dummy_thread_attr;
110
 
 
111
 
    pthread_attr_init(&dummy_thread_attr);
112
 
    pthread_attr_setdetachstate(&dummy_thread_attr, PTHREAD_CREATE_DETACHED);
113
 
 
114
 
    pthread_create(&dummy_thread,&dummy_thread_attr,
115
 
                   nptl_pthread_exit_hack_handler, NULL);
116
 
  }
117
 
#endif /* TARGET_OS_LINUX */
118
 
 
119
104
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
120
105
  /*
121
106
    Set mutex type to "fast" a.k.a "adaptive"
139
124
                            PTHREAD_MUTEX_ERRORCHECK);
140
125
#endif
141
126
 
142
 
  pthread_mutex_init(&THR_LOCK_malloc,MY_MUTEX_INIT_FAST);
143
127
  pthread_mutex_init(&THR_LOCK_open,MY_MUTEX_INIT_FAST);
144
128
  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
129
  pthread_mutex_init(&THR_LOCK_charset,MY_MUTEX_INIT_FAST);
150
130
  pthread_mutex_init(&THR_LOCK_threads,MY_MUTEX_INIT_FAST);
151
131
  pthread_mutex_init(&THR_LOCK_time,MY_MUTEX_INIT_FAST);
152
132
  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);
155
 
#endif
156
 
#ifndef HAVE_GETHOSTBYNAME_R
157
 
  pthread_mutex_init(&LOCK_gethostbyname_r,MY_MUTEX_INIT_SLOW);
158
 
#endif
159
133
  if (my_thread_init())
160
134
  {
161
135
    my_thread_global_end();                     /* Clean up */
202
176
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
203
177
  pthread_mutexattr_destroy(&my_errorcheck_mutexattr);
204
178
#endif
205
 
  pthread_mutex_destroy(&THR_LOCK_malloc);
206
179
  pthread_mutex_destroy(&THR_LOCK_open);
207
180
  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
181
  pthread_mutex_destroy(&THR_LOCK_time);
213
182
  pthread_mutex_destroy(&THR_LOCK_charset);
214
183
  if (all_threads_killed)
216
185
    pthread_mutex_destroy(&THR_LOCK_threads);
217
186
    pthread_cond_destroy(&THR_COND_threads);
218
187
  }
219
 
#if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
220
 
  pthread_mutex_destroy(&LOCK_localtime_r);
221
 
#endif
222
 
#ifndef HAVE_GETHOSTBYNAME_R
223
 
  pthread_mutex_destroy(&LOCK_gethostbyname_r);
224
 
#endif
225
188
}
226
189
 
227
190
static my_thread_id thread_id= 0;
251
214
#ifdef EXTRA_DEBUG_THREADS
252
215
  fprintf(stderr,"my_thread_init(): thread_id: 0x%lx\n",
253
216
          (uint32_t) pthread_self());
254
 
#endif  
 
217
#endif
255
218
 
256
219
  if (pthread_getspecific(THR_KEY_mysys))
257
220
  {
258
221
#ifdef EXTRA_DEBUG_THREADS
259
222
    fprintf(stderr,"my_thread_init() called more than once in thread 0x%lx\n",
260
223
            (long) pthread_self());
261
 
#endif    
 
224
#endif
262
225
    goto end;
263
226
  }
264
227
  if (!(tmp= (struct st_my_thread_var *) calloc(1, sizeof(*tmp))))
302
265
#ifdef EXTRA_DEBUG_THREADS
303
266
  fprintf(stderr,"my_thread_end(): tmp: 0x%lx  pthread_self: 0x%lx  thread_id: %ld\n",
304
267
          (long) tmp, (long) pthread_self(), tmp ? (long) tmp->id : 0L);
305
 
#endif  
 
268
#endif
306
269
  if (tmp && tmp->init)
307
270
  {
308
271
#if !defined(__bsdi__) && !defined(__OpenBSD__)
320
283
      Decrement counter for number of running threads. We are using this
321
284
      in my_thread_global_end() to wait until all threads have called
322
285
      my_thread_end and thus freed all memory they have allocated in
323
 
      my_thread_init() 
 
286
      my_thread_init()
324
287
    */
325
288
    pthread_mutex_lock(&THR_LOCK_threads);
326
289
    assert(THR_thread_count != 0);
342
305
  if (!tmp)
343
306
  {
344
307
    my_thread_init();
345
 
    tmp=my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
 
308
    tmp=(struct st_my_thread_var*)pthread_getspecific(THR_KEY_mysys);
346
309
  }
347
310
#endif
348
311
  return tmp;
362
325
{
363
326
#ifdef _CS_GNU_LIBPTHREAD_VERSION
364
327
  char buff[64];
365
 
    
 
328
 
366
329
  confstr(_CS_GNU_LIBPTHREAD_VERSION, buff, sizeof(buff));
367
330
 
368
331
  if (!strncasecmp(buff, "NPTL", 4))