~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_thr_init.c

  • Committer: Stewart Smith
  • Date: 2008-06-30 06:46:40 UTC
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: stewart@flamingspork.com-20080630064640-1tbyi1e8j4duba45
no embedded server, stop testing for it in tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
*/
20
20
 
21
21
#include "mysys_priv.h"
22
 
#include <mystrings/m_string.h>
 
22
#include <m_string.h>
23
23
#include <signal.h>
24
24
 
25
 
uint32_t thd_lib_detected= 0;
 
25
uint thd_lib_detected= 0;
26
26
 
 
27
#ifdef THREAD
27
28
#ifdef USE_TLS
28
29
pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
29
30
#else
33
34
                THR_LOCK_lock,THR_LOCK_isam,THR_LOCK_myisam,THR_LOCK_heap,
34
35
                THR_LOCK_net, THR_LOCK_charset, THR_LOCK_threads, THR_LOCK_time;
35
36
pthread_cond_t  THR_COND_threads;
36
 
uint32_t            THR_thread_count= 0;
37
 
uint32_t                my_thread_end_wait_time= 5;
 
37
uint            THR_thread_count= 0;
 
38
uint            my_thread_end_wait_time= 5;
38
39
#if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
39
40
pthread_mutex_t LOCK_localtime_r;
40
41
#endif
66
67
#endif /* TARGET_OS_LINUX */
67
68
 
68
69
 
69
 
static uint32_t get_thread_lib(void);
 
70
static uint get_thread_lib(void);
70
71
 
71
72
/*
72
73
  initialize thread environment
79
80
    1  error (Couldn't create THR_KEY_mysys)
80
81
*/
81
82
 
82
 
bool my_thread_global_init(void)
 
83
my_bool my_thread_global_init(void)
83
84
{
84
85
  int pth_ret;
85
86
  thd_lib_detected= get_thread_lib();
168
169
void my_thread_global_end(void)
169
170
{
170
171
  struct timespec abstime;
171
 
  bool all_threads_killed= 1;
 
172
  my_bool all_threads_killed= 1;
172
173
 
173
174
  set_timespec(abstime, my_thread_end_wait_time);
174
175
  pthread_mutex_lock(&THR_LOCK_threads);
227
228
static my_thread_id thread_id= 0;
228
229
 
229
230
/*
230
 
  Allocate thread specific memory for the thread, used by mysys
 
231
  Allocate thread specific memory for the thread, used by mysys and dbug
231
232
 
232
233
  SYNOPSIS
233
234
    my_thread_init()
238
239
    case the checking of mutex_locks will not work until
239
240
    the pthread_self thread specific variable is initialized.
240
241
 
 
242
   This function may called multiple times for a thread, for example
 
243
   if one uses my_init() followed by mysql_server_init().
 
244
 
241
245
  RETURN
242
246
    0  ok
243
247
    1  Fatal error; mysys/dbug functions can't be used
244
248
*/
245
249
 
246
 
bool my_thread_init(void)
 
250
my_bool my_thread_init(void)
247
251
{
248
252
  struct st_my_thread_var *tmp;
249
 
  bool error=0;
 
253
  my_bool error=0;
250
254
 
251
255
#ifdef EXTRA_DEBUG_THREADS
252
256
  fprintf(stderr,"my_thread_init(): thread_id: 0x%lx\n",
253
 
          (uint32_t) pthread_self());
 
257
          (ulong) pthread_self());
254
258
#endif  
255
259
 
256
260
  if (pthread_getspecific(THR_KEY_mysys))
276
280
  tmp->id= ++thread_id;
277
281
  ++THR_thread_count;
278
282
  pthread_mutex_unlock(&THR_LOCK_threads);
 
283
#ifndef DBUG_OFF
 
284
  /* Generate unique name for thread */
 
285
  (void) my_thread_name();
 
286
#endif
279
287
 
280
288
end:
281
289
  return error;
305
313
#endif  
306
314
  if (tmp && tmp->init)
307
315
  {
 
316
#if !defined(DBUG_OFF)
 
317
    /* tmp->dbug is allocated inside DBUG library */
 
318
    if (tmp->dbug)
 
319
    {
 
320
      DBUG_POP();
 
321
      free(tmp->dbug);
 
322
      tmp->dbug=0;
 
323
    }
 
324
#endif
308
325
#if !defined(__bsdi__) && !defined(__OpenBSD__)
309
326
 /* bsdi and openbsd 3.5 dumps core here */
310
327
    pthread_cond_destroy(&tmp->suspend);
320
337
      Decrement counter for number of running threads. We are using this
321
338
      in my_thread_global_end() to wait until all threads have called
322
339
      my_thread_end and thus freed all memory they have allocated in
323
 
      my_thread_init() 
 
340
      my_thread_init() and DBUG_xxxx
324
341
    */
325
342
    pthread_mutex_lock(&THR_LOCK_threads);
326
 
    assert(THR_thread_count != 0);
 
343
    DBUG_ASSERT(THR_thread_count != 0);
327
344
    if (--THR_thread_count == 0)
328
345
      pthread_cond_signal(&THR_COND_threads);
329
346
   pthread_mutex_unlock(&THR_LOCK_threads);
358
375
  return my_thread_var->id;
359
376
}
360
377
 
361
 
static uint32_t get_thread_lib(void)
 
378
#ifdef DBUG_OFF
 
379
const char *my_thread_name(void)
 
380
{
 
381
  return "no_name";
 
382
}
 
383
 
 
384
#else
 
385
 
 
386
const char *my_thread_name(void)
 
387
{
 
388
  char name_buff[100];
 
389
  struct st_my_thread_var *tmp=my_thread_var;
 
390
  if (!tmp->name[0])
 
391
  {
 
392
    my_thread_id id= my_thread_dbug_id();
 
393
    sprintf(name_buff,"T@%lu", (ulong) id);
 
394
    strmake(tmp->name,name_buff,THREAD_NAME_SIZE);
 
395
  }
 
396
  return tmp->name;
 
397
}
 
398
#endif /* DBUG_OFF */
 
399
 
 
400
 
 
401
static uint get_thread_lib(void)
362
402
{
363
403
#ifdef _CS_GNU_LIBPTHREAD_VERSION
364
404
  char buff[64];
372
412
#endif
373
413
  return THD_LIB_OTHER;
374
414
}
 
415
 
 
416
#endif /* THREAD */