summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStaale Smedseng <staale.smedseng@sun.com>2009-12-12 19:11:25 +0100
committerStaale Smedseng <staale.smedseng@sun.com>2009-12-12 19:11:25 +0100
commit8969530cffde0c7a466cf3aa8c4b7462d0ace2d2 (patch)
treee11d1534328b91e686c5a4590990a39cf3e6eeb6
parent0654e74a708247a461953726b3f4d3e18b78b6d4 (diff)
downloadmariadb-git-8969530cffde0c7a466cf3aa8c4b7462d0ace2d2.tar.gz
Bug #45058 init_available_charsets uses double checked locking
As documented in the bug report, the double checked locking pattern has inherent issues, and cannot guarantee correct initialization. This patch replaces the logic in init_available_charsets() with the use of pthread_once(3). A wrapper function, my_pthread_once(), is introduced and is used in lieu of direct calls to init_available_charsets(). Related defines MY_PTHREAD_ONCE_* are also introduced. For the Windows platform, the implementation in lp:sysbench is ported. For single-thread use, a simple define calls the function and sets the pthread_once control variable. Charset initialization is modified to use my_pthread_once(). include/my_no_pthread.h: Dummy my_pthread_once() for single thread use. include/my_pthread.h: Declaration for new function my_pthread_once(). mysys/charset.c: Logic in init_available_charsets() is simplified. Using my_pthread_once() for all calls to this func. mysys/my_winthread.c: Windows implementation of my_pthread_once().
-rw-r--r--include/my_no_pthread.h8
-rw-r--r--include/my_pthread.h10
-rw-r--r--include/my_sys.h1
-rw-r--r--libmysql/libmysql.c1
-rw-r--r--mysys/charset.c77
-rw-r--r--mysys/my_init.c1
-rw-r--r--mysys/my_winthread.c32
-rw-r--r--netware/libmysqlmain.c4
-rw-r--r--sql/mysqld.cc1
9 files changed, 77 insertions, 58 deletions
diff --git a/include/my_no_pthread.h b/include/my_no_pthread.h
index b11dbff42f0..511fac407d5 100644
--- a/include/my_no_pthread.h
+++ b/include/my_no_pthread.h
@@ -47,4 +47,12 @@
#define rw_unlock(A)
#define rwlock_destroy(A)
+typedef int my_pthread_once_t;
+#define MY_PTHREAD_ONCE_INIT 0
+#define MY_PTHREAD_ONCE_DONE 1
+
+#define my_pthread_once(C,F) do { \
+ if (*(C) != MY_PTHREAD_ONCE_DONE) { F(); *(C)= MY_PTHREAD_ONCE_DONE; } \
+ } while(0)
+
#endif
diff --git a/include/my_pthread.h b/include/my_pthread.h
index 151cb34faff..e9256610ea7 100644
--- a/include/my_pthread.h
+++ b/include/my_pthread.h
@@ -69,6 +69,11 @@ typedef int pthread_mutexattr_t;
#define pthread_handler_t EXTERNC void * __cdecl
typedef void * (__cdecl *pthread_handler)(void *);
+typedef volatile LONG my_pthread_once_t;
+#define MY_PTHREAD_ONCE_INIT 0
+#define MY_PTHREAD_ONCE_INPROGRESS 1
+#define MY_PTHREAD_ONCE_DONE 2
+
/*
Struct and macros to be used in combination with the
windows implementation of pthread_cond_timedwait
@@ -114,6 +119,7 @@ int pthread_attr_init(pthread_attr_t *connect_att);
int pthread_attr_setstacksize(pthread_attr_t *connect_att,DWORD stack);
int pthread_attr_setprio(pthread_attr_t *connect_att,int priority);
int pthread_attr_destroy(pthread_attr_t *connect_att);
+int my_pthread_once(my_pthread_once_t *once_control,void (*init_routine)(void));
struct tm *localtime_r(const time_t *timep,struct tm *tmp);
struct tm *gmtime_r(const time_t *timep,struct tm *tmp);
@@ -210,6 +216,10 @@ extern int my_pthread_getprio(pthread_t thread_id);
#define pthread_handler_t EXTERNC void *
typedef void *(* pthread_handler)(void *);
+#define my_pthread_once_t pthread_once_t
+#define MY_PTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
+#define my_pthread_once(C,F) pthread_once(C,F)
+
/* Test first for RTS or FSU threads */
#if defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM)
diff --git a/include/my_sys.h b/include/my_sys.h
index b4aac3a17bd..a4ff5c30de7 100644
--- a/include/my_sys.h
+++ b/include/my_sys.h
@@ -951,7 +951,6 @@ extern my_bool resolve_collation(const char *cl_name,
CHARSET_INFO *default_cl,
CHARSET_INFO **cl);
-extern void free_charsets(void);
extern char *get_charsets_dir(char *buf);
extern my_bool my_charset_same(CHARSET_INFO *cs1, CHARSET_INFO *cs2);
extern my_bool init_compiled_charsets(myf flags);
diff --git a/libmysql/libmysql.c b/libmysql/libmysql.c
index 1264f2765ba..36b1d9cac72 100644
--- a/libmysql/libmysql.c
+++ b/libmysql/libmysql.c
@@ -211,7 +211,6 @@ void STDCALL mysql_server_end()
}
else
{
- free_charsets();
mysql_thread_end();
}
diff --git a/mysys/charset.c b/mysys/charset.c
index b23ab084e90..d59be4ab6c7 100644
--- a/mysys/charset.c
+++ b/mysys/charset.c
@@ -321,7 +321,6 @@ static int add_collation(CHARSET_INFO *cs)
#define MY_CHARSET_INDEX "Index.xml"
const char *charsets_dir= NULL;
-static int charset_initialized=0;
static my_bool my_read_charset_file(const char *filename, myf myflags)
@@ -399,63 +398,37 @@ static void *cs_alloc(size_t size)
}
-#ifdef __NETWARE__
-my_bool STDCALL init_available_charsets(myf myflags)
-#else
-static my_bool init_available_charsets(myf myflags)
-#endif
+static my_pthread_once_t charsets_initialized= MY_PTHREAD_ONCE_INIT;
+
+static void init_available_charsets(void)
{
char fname[FN_REFLEN + sizeof(MY_CHARSET_INDEX)];
- my_bool error=FALSE;
- /*
- We have to use charset_initialized to not lock on THR_LOCK_charset
- inside get_internal_charset...
- */
- if (!charset_initialized)
+ CHARSET_INFO **cs;
+
+ bzero(&all_charsets,sizeof(all_charsets));
+ init_compiled_charsets(MYF(0));
+
+ /* Copy compiled charsets */
+ for (cs=all_charsets;
+ cs < all_charsets+array_elements(all_charsets)-1 ;
+ cs++)
{
- CHARSET_INFO **cs;
- /*
- To make things thread safe we are not allowing other threads to interfere
- while we may changing the cs_info_table
- */
- pthread_mutex_lock(&THR_LOCK_charset);
- if (!charset_initialized)
+ if (*cs)
{
- bzero(&all_charsets,sizeof(all_charsets));
- init_compiled_charsets(myflags);
-
- /* Copy compiled charsets */
- for (cs=all_charsets;
- cs < all_charsets+array_elements(all_charsets)-1 ;
- cs++)
- {
- if (*cs)
- {
- if (cs[0]->ctype)
- if (init_state_maps(*cs))
- *cs= NULL;
- }
- }
-
- strmov(get_charsets_dir(fname), MY_CHARSET_INDEX);
- error= my_read_charset_file(fname,myflags);
- charset_initialized=1;
+ if (cs[0]->ctype)
+ if (init_state_maps(*cs))
+ *cs= NULL;
}
- pthread_mutex_unlock(&THR_LOCK_charset);
}
- return error;
-}
-
-
-void free_charsets(void)
-{
- charset_initialized=0;
+
+ strmov(get_charsets_dir(fname), MY_CHARSET_INDEX);
+ my_read_charset_file(fname, MYF(0));
}
uint get_collation_number(const char *name)
{
- init_available_charsets(MYF(0));
+ my_pthread_once(&charsets_initialized, init_available_charsets);
return get_collation_number_internal(name);
}
@@ -463,7 +436,7 @@ uint get_collation_number(const char *name)
uint get_charset_number(const char *charset_name, uint cs_flags)
{
CHARSET_INFO **cs;
- init_available_charsets(MYF(0));
+ my_pthread_once(&charsets_initialized, init_available_charsets);
for (cs= all_charsets;
cs < all_charsets+array_elements(all_charsets)-1 ;
@@ -480,7 +453,7 @@ uint get_charset_number(const char *charset_name, uint cs_flags)
const char *get_charset_name(uint charset_number)
{
CHARSET_INFO *cs;
- init_available_charsets(MYF(0));
+ my_pthread_once(&charsets_initialized, init_available_charsets);
cs=all_charsets[charset_number];
if (cs && (cs->number == charset_number) && cs->name )
@@ -538,7 +511,7 @@ CHARSET_INFO *get_charset(uint cs_number, myf flags)
if (cs_number == default_charset_info->number)
return default_charset_info;
- (void) init_available_charsets(MYF(0)); /* If it isn't initialized */
+ my_pthread_once(&charsets_initialized, init_available_charsets);
if (!cs_number || cs_number >= array_elements(all_charsets)-1)
return NULL;
@@ -560,7 +533,7 @@ CHARSET_INFO *get_charset_by_name(const char *cs_name, myf flags)
{
uint cs_number;
CHARSET_INFO *cs;
- (void) init_available_charsets(MYF(0)); /* If it isn't initialized */
+ my_pthread_once(&charsets_initialized, init_available_charsets);
cs_number=get_collation_number(cs_name);
cs= cs_number ? get_internal_charset(cs_number,flags) : NULL;
@@ -585,7 +558,7 @@ CHARSET_INFO *get_charset_by_csname(const char *cs_name,
DBUG_ENTER("get_charset_by_csname");
DBUG_PRINT("enter",("name: '%s'", cs_name));
- (void) init_available_charsets(MYF(0)); /* If it isn't initialized */
+ my_pthread_once(&charsets_initialized, init_available_charsets);
cs_number= get_charset_number(cs_name, cs_flags);
cs= cs_number ? get_internal_charset(cs_number, flags) : NULL;
diff --git a/mysys/my_init.c b/mysys/my_init.c
index a60927be693..453c72f999f 100644
--- a/mysys/my_init.c
+++ b/mysys/my_init.c
@@ -165,7 +165,6 @@ void my_end(int infoflag)
my_print_open_files();
}
}
- free_charsets();
my_error_unregister_all();
my_once_free();
diff --git a/mysys/my_winthread.c b/mysys/my_winthread.c
index e94369bec32..ef2a20c2ddc 100644
--- a/mysys/my_winthread.c
+++ b/mysys/my_winthread.c
@@ -137,4 +137,36 @@ int win_pthread_setspecific(void *a,void *b,uint length)
return 0;
}
+
+/*
+ One time initialization. For simplicity, we assume initializer thread
+ does not exit within init_routine().
+*/
+int my_pthread_once(my_pthread_once_t *once_control,
+ void (*init_routine)(void))
+{
+ LONG state= InterlockedCompareExchange(once_control, MY_PTHREAD_ONCE_INPROGRESS,
+ MY_PTHREAD_ONCE_INIT);
+ switch(state)
+ {
+ case MY_PTHREAD_ONCE_INIT:
+ /* This is initializer thread */
+ (*init_routine)();
+ *once_control= MY_PTHREAD_ONCE_DONE;
+ break;
+
+ case MY_PTHREAD_ONCE_INPROGRESS:
+ /* init_routine in progress. Wait for its completion */
+ while(*once_control == MY_PTHREAD_ONCE_INPROGRESS)
+ {
+ Sleep(1);
+ }
+ break;
+ case MY_PTHREAD_ONCE_DONE:
+ /* Nothing to do */
+ break;
+ }
+ return 0;
+}
+
#endif
diff --git a/netware/libmysqlmain.c b/netware/libmysqlmain.c
index 03fdb832176..2b6ea9b7e27 100644
--- a/netware/libmysqlmain.c
+++ b/netware/libmysqlmain.c
@@ -18,7 +18,7 @@
#include "my_global.h"
-my_bool init_available_charsets(myf myflags);
+void init_available_charsets(void);
/* this function is required so that global memory is allocated against this
library nlm, and not against a paticular client */
@@ -31,7 +31,7 @@ int _NonAppStart(void *NLMHandle, void *errorScreen, const char *commandLine,
{
mysql_server_init(0, NULL, NULL);
- init_available_charsets(MYF(0));
+ init_available_charsets();
return 0;
}
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 9aff751ad2f..b145c5ace10 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -1287,7 +1287,6 @@ void clean_up(bool print_message)
lex_free(); /* Free some memory */
item_create_cleanup();
set_var_free();
- free_charsets();
if (!opt_noacl)
{
#ifdef HAVE_DLOPEN