~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_thr_init.cc

  • Committer: Monty Taylor
  • Date: 2009-04-25 20:45:19 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425204519-lgrl7mz2r66v0jby
Blackhole.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
  thread variables.
19
19
*/
20
20
 
21
 
#include "config.h"
22
 
 
23
 
#include "drizzled/internal/my_sys.h"
24
 
#include "drizzled/internal/my_pthread.h"
25
 
#include "drizzled/internal/m_string.h"
26
 
 
27
 
#include <cstdio>
 
21
#include "mysys_priv.h"
 
22
#include <mysys/my_pthread.h>
 
23
#include <mystrings/m_string.h>
 
24
 
 
25
#include <stdio.h>
28
26
#include <signal.h>
29
27
 
30
28
#if TIME_WITH_SYS_TIME
38
36
# endif
39
37
#endif
40
38
 
41
 
namespace drizzled
42
 
{
43
 
namespace internal
44
 
{
45
 
 
46
39
uint32_t thd_lib_detected= 0;
47
40
 
48
41
pthread_key_t THR_KEY_mysys;
 
42
pthread_mutex_t THR_LOCK_open;
49
43
pthread_mutex_t THR_LOCK_lock;
50
44
pthread_mutex_t THR_LOCK_threads;
51
45
pthread_cond_t  THR_COND_threads;
52
46
uint32_t            THR_thread_count= 0;
53
 
static uint32_t my_thread_end_wait_time= 5;
 
47
uint32_t                my_thread_end_wait_time= 5;
54
48
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
55
49
pthread_mutexattr_t my_fast_mutexattr;
56
50
#endif
 
51
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
 
52
pthread_mutexattr_t my_errorcheck_mutexattr;
 
53
#endif
 
54
 
 
55
#ifdef TARGET_OS_LINUX
 
56
 
 
57
/*
 
58
  Dummy thread spawned in my_thread_global_init() below to avoid
 
59
  race conditions in NPTL pthread_exit code.
 
60
*/
 
61
 
 
62
extern "C"
 
63
void *
 
64
nptl_pthread_exit_hack_handler(void *arg __attribute((unused)))
 
65
{
 
66
  /* Do nothing! */
 
67
  pthread_exit(0);
 
68
  return 0;
 
69
}
 
70
 
 
71
#endif /* TARGET_OS_LINUX */
 
72
 
57
73
 
58
74
static uint32_t get_thread_lib(void);
59
75
 
93
109
  pthread_mutexattr_settype(&my_fast_mutexattr,
94
110
                            PTHREAD_MUTEX_ADAPTIVE_NP);
95
111
#endif
 
112
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
 
113
  /*
 
114
    Set mutex type to "errorcheck"
 
115
  */
 
116
  pthread_mutexattr_init(&my_errorcheck_mutexattr);
 
117
  pthread_mutexattr_settype(&my_errorcheck_mutexattr,
 
118
                            PTHREAD_MUTEX_ERRORCHECK);
 
119
#endif
96
120
 
 
121
  pthread_mutex_init(&THR_LOCK_open,MY_MUTEX_INIT_FAST);
97
122
  pthread_mutex_init(&THR_LOCK_lock,MY_MUTEX_INIT_FAST);
98
123
  pthread_mutex_init(&THR_LOCK_threads,MY_MUTEX_INIT_FAST);
99
124
  pthread_cond_init(&THR_COND_threads, NULL);
138
163
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
139
164
  pthread_mutexattr_destroy(&my_fast_mutexattr);
140
165
#endif
 
166
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
 
167
  pthread_mutexattr_destroy(&my_errorcheck_mutexattr);
 
168
#endif
 
169
  pthread_mutex_destroy(&THR_LOCK_open);
141
170
  pthread_mutex_destroy(&THR_LOCK_lock);
142
171
  if (all_threads_killed)
143
172
  {
146
175
  }
147
176
}
148
177
 
149
 
static uint64_t thread_id= 0;
 
178
static my_thread_id thread_id= 0;
150
179
 
151
180
/*
152
181
  Allocate thread specific memory for the thread, used by mysys
161
190
 
162
191
bool my_thread_init(void)
163
192
{
 
193
  struct st_my_thread_var *tmp;
164
194
  bool error=0;
165
 
  st_my_thread_var *tmp= NULL;
166
195
 
167
196
#ifdef EXTRA_DEBUG_THREADS
168
197
  fprintf(stderr,"my_thread_init(): thread_id: 0x%lx\n",
177
206
#endif
178
207
    goto end;
179
208
  }
180
 
  tmp= static_cast<st_my_thread_var *>(calloc(1, sizeof(*tmp)));
181
 
  if (tmp == NULL)
 
209
  if (!(tmp= (struct st_my_thread_var *) calloc(1, sizeof(*tmp))))
182
210
  {
183
211
    error= 1;
184
212
    goto end;
213
241
 
214
242
void my_thread_end(void)
215
243
{
216
 
  st_my_thread_var *tmp=
217
 
    static_cast<st_my_thread_var *>(pthread_getspecific(THR_KEY_mysys));
 
244
  struct st_my_thread_var *tmp;
 
245
  tmp= (struct st_my_thread_var *)pthread_getspecific(THR_KEY_mysys);
218
246
 
219
247
#ifdef EXTRA_DEBUG_THREADS
220
248
  fprintf(stderr,"my_thread_end(): tmp: 0x%lx  pthread_self: 0x%lx  thread_id: %ld\n",
227
255
    pthread_cond_destroy(&tmp->suspend);
228
256
#endif
229
257
    pthread_mutex_destroy(&tmp->mutex);
230
 
    free(tmp);
 
258
    tmp->init= 0;
231
259
 
232
260
    /*
233
261
      Decrement counter for number of running threads. We are using this
239
267
    assert(THR_thread_count != 0);
240
268
    if (--THR_thread_count == 0)
241
269
      pthread_cond_signal(&THR_COND_threads);
242
 
    pthread_mutex_unlock(&THR_LOCK_threads);
 
270
   pthread_mutex_unlock(&THR_LOCK_threads);
243
271
  }
244
272
}
245
273
 
249
277
  return tmp;
250
278
}
251
279
 
 
280
 
 
281
/****************************************************************************
 
282
  Get name of current thread.
 
283
****************************************************************************/
 
284
 
 
285
my_thread_id my_thread_dbug_id()
 
286
{
 
287
  return my_thread_var->id;
 
288
}
 
289
 
252
290
static uint32_t get_thread_lib(void)
253
291
{
254
292
#ifdef _CS_GNU_LIBPTHREAD_VERSION
263
301
#endif
264
302
  return THD_LIB_OTHER;
265
303
}
266
 
 
267
 
} /* namespace internal */
268
 
} /* namespace drizzled */