2001-12-06 13:10:51 +01:00
|
|
|
/* Copyright (C) 2000 MySQL AB
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
2000-07-31 21:29:14 +02:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-12-06 13:10:51 +01:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
/*
|
2002-06-11 10:20:31 +02:00
|
|
|
Functions to handle initializating and allocationg of all mysys & debug
|
|
|
|
thread variables.
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mysys_priv.h"
|
|
|
|
#include <m_string.h>
|
|
|
|
|
|
|
|
#ifdef THREAD
|
|
|
|
#ifdef USE_TLS
|
|
|
|
pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
|
|
|
|
#else
|
|
|
|
pthread_key(struct st_my_thread_var, THR_KEY_mysys);
|
2001-03-24 19:15:14 +01:00
|
|
|
#endif /* USE_TLS */
|
2003-11-18 12:47:27 +01:00
|
|
|
pthread_mutex_t THR_LOCK_malloc,THR_LOCK_open,
|
2000-07-31 21:29:14 +02:00
|
|
|
THR_LOCK_lock,THR_LOCK_isam,THR_LOCK_myisam,THR_LOCK_heap,
|
2006-11-29 21:14:08 +01:00
|
|
|
THR_LOCK_net, THR_LOCK_charset, THR_LOCK_threads;
|
|
|
|
pthread_cond_t THR_COND_threads;
|
|
|
|
uint THR_thread_count= 0;
|
|
|
|
uint my_thread_end_wait_time= 5;
|
2003-08-11 21:43:01 +02:00
|
|
|
#if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_t LOCK_localtime_r;
|
|
|
|
#endif
|
2002-05-16 15:32:51 +02:00
|
|
|
#ifndef HAVE_GETHOSTBYNAME_R
|
|
|
|
pthread_mutex_t LOCK_gethostbyname_r;
|
|
|
|
#endif
|
2001-03-24 19:15:14 +01:00
|
|
|
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
|
|
|
pthread_mutexattr_t my_fast_mutexattr;
|
|
|
|
#endif
|
2005-04-27 13:29:37 +02:00
|
|
|
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
|
|
|
|
pthread_mutexattr_t my_errorcheck_mutexattr;
|
|
|
|
#endif
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2003-12-11 05:24:08 +01:00
|
|
|
/*
|
|
|
|
initialize thread environment
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
my_thread_global_init()
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 ok
|
|
|
|
1 error (Couldn't create THR_KEY_mysys)
|
|
|
|
*/
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
my_bool my_thread_global_init(void)
|
|
|
|
{
|
2003-12-11 05:24:08 +01:00
|
|
|
if (pthread_key_create(&THR_KEY_mysys,0))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr,"Can't initialize threads: error %d\n",errno);
|
2003-12-11 05:24:08 +01:00
|
|
|
return 1;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2005-04-27 09:59:12 +02:00
|
|
|
|
2001-03-24 19:15:14 +01:00
|
|
|
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
2004-10-22 17:44:51 +02:00
|
|
|
/*
|
2005-04-27 09:59:12 +02:00
|
|
|
Set mutex type to "fast" a.k.a "adaptive"
|
|
|
|
|
2005-05-06 10:39:30 +02:00
|
|
|
In this case the thread may steal the mutex from some other thread
|
|
|
|
that is waiting for the same mutex. This will save us some
|
|
|
|
context switches but may cause a thread to 'starve forever' while
|
|
|
|
waiting for the mutex (not likely if the code within the mutex is
|
|
|
|
short).
|
2004-10-22 17:44:51 +02:00
|
|
|
*/
|
2005-04-27 09:59:12 +02:00
|
|
|
pthread_mutexattr_init(&my_fast_mutexattr);
|
|
|
|
pthread_mutexattr_settype(&my_fast_mutexattr,
|
|
|
|
PTHREAD_MUTEX_ADAPTIVE_NP);
|
2001-03-24 19:15:14 +01:00
|
|
|
#endif
|
2005-04-27 13:29:37 +02:00
|
|
|
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
|
|
|
|
/*
|
2006-11-29 21:14:08 +01:00
|
|
|
Set mutex type to "errorcheck"
|
2005-04-27 13:29:37 +02:00
|
|
|
*/
|
|
|
|
pthread_mutexattr_init(&my_errorcheck_mutexattr);
|
|
|
|
pthread_mutexattr_settype(&my_errorcheck_mutexattr,
|
|
|
|
PTHREAD_MUTEX_ERRORCHECK);
|
|
|
|
#endif
|
2001-03-24 19:15:14 +01:00
|
|
|
|
|
|
|
pthread_mutex_init(&THR_LOCK_malloc,MY_MUTEX_INIT_FAST);
|
|
|
|
pthread_mutex_init(&THR_LOCK_open,MY_MUTEX_INIT_FAST);
|
|
|
|
pthread_mutex_init(&THR_LOCK_lock,MY_MUTEX_INIT_FAST);
|
|
|
|
pthread_mutex_init(&THR_LOCK_isam,MY_MUTEX_INIT_SLOW);
|
|
|
|
pthread_mutex_init(&THR_LOCK_myisam,MY_MUTEX_INIT_SLOW);
|
|
|
|
pthread_mutex_init(&THR_LOCK_heap,MY_MUTEX_INIT_FAST);
|
|
|
|
pthread_mutex_init(&THR_LOCK_net,MY_MUTEX_INIT_FAST);
|
|
|
|
pthread_mutex_init(&THR_LOCK_charset,MY_MUTEX_INIT_FAST);
|
2006-11-29 21:14:08 +01:00
|
|
|
pthread_mutex_init(&THR_LOCK_threads,MY_MUTEX_INIT_FAST);
|
|
|
|
pthread_cond_init (&THR_COND_threads, NULL);
|
2001-08-22 00:45:07 +02:00
|
|
|
#if defined( __WIN__) || defined(OS2)
|
2000-09-21 00:58:38 +02:00
|
|
|
win_pthread_init();
|
2000-07-31 21:29:14 +02:00
|
|
|
#endif
|
2003-08-11 21:43:01 +02:00
|
|
|
#if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
|
2001-03-24 19:15:14 +01:00
|
|
|
pthread_mutex_init(&LOCK_localtime_r,MY_MUTEX_INIT_SLOW);
|
2002-05-16 15:32:51 +02:00
|
|
|
#endif
|
|
|
|
#ifndef HAVE_GETHOSTBYNAME_R
|
|
|
|
pthread_mutex_init(&LOCK_gethostbyname_r,MY_MUTEX_INIT_SLOW);
|
2000-07-31 21:29:14 +02:00
|
|
|
#endif
|
2003-12-11 05:24:08 +01:00
|
|
|
if (my_thread_init())
|
|
|
|
{
|
|
|
|
my_thread_global_end(); /* Clean up */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
void my_thread_global_end(void)
|
|
|
|
{
|
2006-11-29 21:14:08 +01:00
|
|
|
struct timespec abstime;
|
|
|
|
set_timespec(abstime, my_thread_end_wait_time);
|
|
|
|
my_bool all_threads_killed= 1;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&THR_LOCK_threads);
|
|
|
|
while (THR_thread_count)
|
|
|
|
{
|
|
|
|
int error= pthread_cond_timedwait(&THR_COND_threads, &THR_LOCK_threads,
|
|
|
|
&abstime);
|
|
|
|
if (error == ETIMEDOUT || error == ETIME)
|
|
|
|
{
|
|
|
|
if (THR_thread_count)
|
|
|
|
fprintf(stderr,"error in my_thread_global_end(): %d threads didn't exit\n",
|
|
|
|
THR_thread_count);
|
|
|
|
all_threads_killed= 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&THR_LOCK_threads);
|
|
|
|
|
2003-12-11 05:24:08 +01:00
|
|
|
pthread_key_delete(THR_KEY_mysys);
|
2001-03-24 19:15:14 +01:00
|
|
|
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
|
|
|
pthread_mutexattr_destroy(&my_fast_mutexattr);
|
2005-04-27 13:29:37 +02:00
|
|
|
#endif
|
|
|
|
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
|
|
|
|
pthread_mutexattr_destroy(&my_errorcheck_mutexattr);
|
2003-01-28 07:38:28 +01:00
|
|
|
#endif
|
|
|
|
pthread_mutex_destroy(&THR_LOCK_malloc);
|
|
|
|
pthread_mutex_destroy(&THR_LOCK_open);
|
|
|
|
pthread_mutex_destroy(&THR_LOCK_lock);
|
|
|
|
pthread_mutex_destroy(&THR_LOCK_isam);
|
|
|
|
pthread_mutex_destroy(&THR_LOCK_myisam);
|
|
|
|
pthread_mutex_destroy(&THR_LOCK_heap);
|
|
|
|
pthread_mutex_destroy(&THR_LOCK_net);
|
|
|
|
pthread_mutex_destroy(&THR_LOCK_charset);
|
2006-11-29 21:14:08 +01:00
|
|
|
if (all_threads_killed)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&THR_LOCK_threads);
|
|
|
|
pthread_cond_destroy (&THR_COND_threads);
|
|
|
|
}
|
2003-08-11 21:43:01 +02:00
|
|
|
#if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
|
2003-01-28 07:38:28 +01:00
|
|
|
pthread_mutex_destroy(&LOCK_localtime_r);
|
2001-03-24 19:15:14 +01:00
|
|
|
#endif
|
2002-05-16 15:32:51 +02:00
|
|
|
#ifndef HAVE_GETHOSTBYNAME_R
|
|
|
|
pthread_mutex_destroy(&LOCK_gethostbyname_r);
|
|
|
|
#endif
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static long thread_id=0;
|
|
|
|
|
2000-09-20 18:37:07 +02:00
|
|
|
/*
|
2000-09-21 00:58:38 +02:00
|
|
|
We can't use mutex_locks here if we are using windows as
|
2000-09-20 18:37:07 +02:00
|
|
|
we may have compiled the program with SAFE_MUTEX, in which
|
|
|
|
case the checking of mutex_locks will not work until
|
|
|
|
the pthread_self thread specific variable is initialized.
|
|
|
|
*/
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
my_bool my_thread_init(void)
|
|
|
|
{
|
|
|
|
struct st_my_thread_var *tmp;
|
2002-04-29 11:24:14 +02:00
|
|
|
my_bool error=0;
|
|
|
|
|
2002-04-24 14:23:32 +02:00
|
|
|
#ifdef EXTRA_DEBUG_THREADS
|
2002-03-27 06:19:23 +01:00
|
|
|
fprintf(stderr,"my_thread_init(): thread_id=%ld\n",pthread_self());
|
|
|
|
#endif
|
2002-04-02 16:54:57 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
#if !defined(__WIN__) || defined(USE_TLS)
|
|
|
|
if (my_pthread_getspecific(struct st_my_thread_var *,THR_KEY_mysys))
|
|
|
|
{
|
2002-06-16 16:06:12 +02:00
|
|
|
#ifdef EXTRA_DEBUG_THREADS
|
2002-03-27 06:19:23 +01:00
|
|
|
fprintf(stderr,"my_thread_init() called more than once in thread %ld\n",
|
2002-04-29 11:24:14 +02:00
|
|
|
pthread_self());
|
2002-03-27 06:19:23 +01:00
|
|
|
#endif
|
2002-04-29 11:24:14 +02:00
|
|
|
goto end;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2002-04-29 11:24:14 +02:00
|
|
|
if (!(tmp= (struct st_my_thread_var *) calloc(1, sizeof(*tmp))))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2002-04-29 11:24:14 +02:00
|
|
|
error= 1;
|
|
|
|
goto end;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
pthread_setspecific(THR_KEY_mysys,tmp);
|
|
|
|
|
2006-11-29 21:14:08 +01:00
|
|
|
#else /* defined(__WIN__) && !(defined(USE_TLS) */
|
2002-04-02 16:54:57 +02:00
|
|
|
/*
|
|
|
|
Skip initialization if the thread specific variable is already initialized
|
|
|
|
*/
|
|
|
|
if (THR_KEY_mysys.id)
|
|
|
|
goto end;
|
2000-07-31 21:29:14 +02:00
|
|
|
tmp= &THR_KEY_mysys;
|
|
|
|
#endif
|
2003-12-05 18:45:19 +01:00
|
|
|
#if defined(__WIN__) && defined(EMBEDDED_LIBRARY)
|
|
|
|
tmp->thread_self= (pthread_t)getpid();
|
|
|
|
#endif
|
2001-03-24 19:15:14 +01:00
|
|
|
pthread_mutex_init(&tmp->mutex,MY_MUTEX_INIT_FAST);
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_cond_init(&tmp->suspend, NULL);
|
2003-10-16 19:55:15 +02:00
|
|
|
tmp->init= 1;
|
2002-04-02 16:54:57 +02:00
|
|
|
|
2006-11-29 21:14:08 +01:00
|
|
|
pthread_mutex_lock(&THR_LOCK_threads);
|
|
|
|
tmp->id= ++thread_id;
|
|
|
|
++THR_thread_count;
|
|
|
|
pthread_mutex_unlock(&THR_LOCK_threads);
|
2002-04-02 16:54:57 +02:00
|
|
|
end:
|
2002-04-29 11:24:14 +02:00
|
|
|
return error;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
void my_thread_end(void)
|
|
|
|
{
|
2003-10-16 19:55:15 +02:00
|
|
|
struct st_my_thread_var *tmp;
|
|
|
|
tmp= my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
|
|
|
|
|
2002-04-24 14:23:32 +02:00
|
|
|
#ifdef EXTRA_DEBUG_THREADS
|
2002-03-27 06:19:23 +01:00
|
|
|
fprintf(stderr,"my_thread_end(): tmp=%p,thread_id=%ld\n",
|
|
|
|
tmp,pthread_self());
|
|
|
|
#endif
|
2003-10-16 19:55:15 +02:00
|
|
|
if (tmp && tmp->init)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
#if !defined(DBUG_OFF)
|
2002-04-02 16:54:57 +02:00
|
|
|
/* tmp->dbug is allocated inside DBUG library */
|
2000-07-31 21:29:14 +02:00
|
|
|
if (tmp->dbug)
|
|
|
|
{
|
|
|
|
free(tmp->dbug);
|
|
|
|
tmp->dbug=0;
|
|
|
|
}
|
|
|
|
#endif
|
2004-04-09 06:12:41 +02:00
|
|
|
#if !defined(__bsdi__) && !defined(__OpenBSD__) || defined(HAVE_mit_thread)
|
|
|
|
/* bsdi and openbsd 3.5 dumps core here */
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_cond_destroy(&tmp->suspend);
|
|
|
|
#endif
|
|
|
|
pthread_mutex_destroy(&tmp->mutex);
|
2001-08-22 00:45:07 +02:00
|
|
|
#if (!defined(__WIN__) && !defined(OS2)) || defined(USE_TLS)
|
2002-04-02 16:54:57 +02:00
|
|
|
free(tmp);
|
2003-10-16 19:55:15 +02:00
|
|
|
#else
|
|
|
|
tmp->init= 0;
|
2000-07-31 21:29:14 +02:00
|
|
|
#endif
|
|
|
|
}
|
2002-04-02 16:54:57 +02:00
|
|
|
/* The following free has to be done, even if my_thread_var() is 0 */
|
|
|
|
#if (!defined(__WIN__) && !defined(OS2)) || defined(USE_TLS)
|
|
|
|
pthread_setspecific(THR_KEY_mysys,0);
|
|
|
|
#endif
|
2006-11-29 21:14:08 +01:00
|
|
|
pthread_mutex_lock(&THR_LOCK_threads);
|
|
|
|
if (--THR_thread_count == 0)
|
|
|
|
pthread_cond_signal(&THR_COND_threads);
|
|
|
|
pthread_mutex_unlock(&THR_LOCK_threads);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct st_my_thread_var *_my_thread_var(void)
|
|
|
|
{
|
|
|
|
struct st_my_thread_var *tmp=
|
|
|
|
my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
|
|
|
|
#if defined(USE_TLS)
|
|
|
|
/* This can only happen in a .DLL */
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
my_thread_init();
|
|
|
|
tmp=my_pthread_getspecific(struct st_my_thread_var*,THR_KEY_mysys);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
/****************************************************************************
|
2002-06-11 10:20:31 +02:00
|
|
|
Get name of current thread.
|
2000-07-31 21:29:14 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define UNKNOWN_THREAD -1
|
|
|
|
|
|
|
|
long my_thread_id()
|
|
|
|
{
|
|
|
|
#if defined(HAVE_PTHREAD_GETSEQUENCE_NP)
|
|
|
|
return pthread_getsequence_np(pthread_self());
|
|
|
|
#elif (defined(__sun) || defined(__sgi) || defined(__linux__)) && !defined(HAVE_mit_thread)
|
|
|
|
return pthread_self();
|
|
|
|
#else
|
|
|
|
return my_thread_var->id;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DBUG_OFF
|
2001-03-24 19:15:14 +01:00
|
|
|
const char *my_thread_name(void)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
return "no_name";
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-03-24 19:15:14 +01:00
|
|
|
const char *my_thread_name(void)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
char name_buff[100];
|
|
|
|
struct st_my_thread_var *tmp=my_thread_var;
|
|
|
|
if (!tmp->name[0])
|
|
|
|
{
|
|
|
|
long id=my_thread_id();
|
|
|
|
sprintf(name_buff,"T@%ld", id);
|
|
|
|
strmake(tmp->name,name_buff,THREAD_NAME_SIZE);
|
|
|
|
}
|
|
|
|
return tmp->name;
|
|
|
|
}
|
|
|
|
#endif /* DBUG_OFF */
|
|
|
|
|
|
|
|
#endif /* THREAD */
|