Commit 2bc1930c authored by Vladislav Vaintroub's avatar Vladislav Vaintroub

Windows improvements : manual backport of

htttp://lists.mysql.com/commits/50957?f=plain
      
Always use TLS functions instead of __declspec(thread) to access 
thread local storage variables.
The change removes the necessity to recomplile the same source
files twice -  with USE_TLS for DLLs and without USE_TLS for EXEs.
Real benefit of this change is better readability and maintainability
of TLS functions within MySQL.
              
There is a performance loss using TlsXXX functions compared to __declspec 
but the difference is negligible in practice. In a sysbench-like benchmark 
I ran with with TlsGetValue, pthread_[get|set]_specific was called 600000000 
times and took 0.17sec of total 35min CPU time, or 0.008%.
parent 28a7d504
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
INCLUDE("${PROJECT_SOURCE_DIR}/win/mysql_manifest.cmake") INCLUDE("${PROJECT_SOURCE_DIR}/win/mysql_manifest.cmake")
# We use the "mysqlclient_notls" library here just as safety, in case # We use the "mysqlclient" library here just as safety, in case
# any of the clients here would go beyond the client API and access the # any of the clients here would go beyond the client API and access the
# Thread Local Storage directly. # Thread Local Storage directly.
...@@ -30,27 +30,27 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ...@@ -30,27 +30,27 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/strings) ${CMAKE_SOURCE_DIR}/strings)
ADD_EXECUTABLE(mysql completion_hash.cc mysql.cc readline.cc sql_string.cc ../mysys/my_conio.c) ADD_EXECUTABLE(mysql completion_hash.cc mysql.cc readline.cc sql_string.cc ../mysys/my_conio.c)
TARGET_LINK_LIBRARIES(mysql mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(mysql mysqlclient wsock32)
ADD_EXECUTABLE(mysqltest mysqltest.cc) ADD_EXECUTABLE(mysqltest mysqltest.cc)
SET_SOURCE_FILES_PROPERTIES(mysqltest.cc PROPERTIES COMPILE_FLAGS "-DTHREADS") SET_SOURCE_FILES_PROPERTIES(mysqltest.cc PROPERTIES COMPILE_FLAGS "-DTHREADS")
TARGET_LINK_LIBRARIES(mysqltest mysqlclient mysys regex wsock32 dbug) TARGET_LINK_LIBRARIES(mysqltest mysqlclient mysys regex wsock32 dbug)
ADD_EXECUTABLE(mysqlcheck mysqlcheck.c) ADD_EXECUTABLE(mysqlcheck mysqlcheck.c)
TARGET_LINK_LIBRARIES(mysqlcheck mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(mysqlcheck mysqlclient wsock32)
ADD_EXECUTABLE(mysqldump mysqldump.c ../sql-common/my_user.c ../mysys/mf_getdate.c) ADD_EXECUTABLE(mysqldump mysqldump.c ../sql-common/my_user.c ../mysys/mf_getdate.c)
TARGET_LINK_LIBRARIES(mysqldump mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(mysqldump mysqlclient wsock32)
ADD_EXECUTABLE(mysqlimport mysqlimport.c) ADD_EXECUTABLE(mysqlimport mysqlimport.c)
TARGET_LINK_LIBRARIES(mysqlimport mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(mysqlimport mysqlclient wsock32)
ADD_EXECUTABLE(mysql_upgrade mysql_upgrade.c ../mysys/my_getpagesize.c) ADD_EXECUTABLE(mysql_upgrade mysql_upgrade.c ../mysys/my_getpagesize.c)
TARGET_LINK_LIBRARIES(mysql_upgrade mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(mysql_upgrade mysqlclient wsock32)
ADD_DEPENDENCIES(mysql_upgrade GenFixPrivs) ADD_DEPENDENCIES(mysql_upgrade GenFixPrivs)
ADD_EXECUTABLE(mysqlshow mysqlshow.c) ADD_EXECUTABLE(mysqlshow mysqlshow.c)
TARGET_LINK_LIBRARIES(mysqlshow mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(mysqlshow mysqlclient wsock32)
ADD_EXECUTABLE(mysqlbinlog mysqlbinlog.cc ADD_EXECUTABLE(mysqlbinlog mysqlbinlog.cc
../mysys/mf_tempdir.c ../mysys/mf_tempdir.c
...@@ -59,10 +59,10 @@ ADD_EXECUTABLE(mysqlbinlog mysqlbinlog.cc ...@@ -59,10 +59,10 @@ ADD_EXECUTABLE(mysqlbinlog mysqlbinlog.cc
../mysys/my_bitmap.c ../mysys/my_bitmap.c
../mysys/my_vle.c ../mysys/my_vle.c
../mysys/base64.c) ../mysys/base64.c)
TARGET_LINK_LIBRARIES(mysqlbinlog mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(mysqlbinlog mysqlclient wsock32)
ADD_EXECUTABLE(mysqladmin mysqladmin.cc) ADD_EXECUTABLE(mysqladmin mysqladmin.cc)
TARGET_LINK_LIBRARIES(mysqladmin mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(mysqladmin mysqlclient wsock32)
ADD_EXECUTABLE(mysqlslap mysqlslap.c) ADD_EXECUTABLE(mysqlslap mysqlslap.c)
SET_SOURCE_FILES_PROPERTIES(mysqlslap.c PROPERTIES COMPILE_FLAGS "-DTHREADS") SET_SOURCE_FILES_PROPERTIES(mysqlslap.c PROPERTIES COMPILE_FLAGS "-DTHREADS")
......
...@@ -100,7 +100,6 @@ struct timespec { ...@@ -100,7 +100,6 @@ struct timespec {
} }
void win_pthread_init(void); void win_pthread_init(void);
int win_pthread_setspecific(void *A,void *B,uint length);
int win_pthread_mutex_trylock(pthread_mutex_t *mutex); int win_pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_create(pthread_t *,pthread_attr_t *,pthread_handler,void *); int pthread_create(pthread_t *,pthread_attr_t *,pthread_handler,void *);
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
...@@ -126,33 +125,16 @@ void pthread_exit(void *a); /* was #define pthread_exit(A) ExitThread(A)*/ ...@@ -126,33 +125,16 @@ void pthread_exit(void *a); /* was #define pthread_exit(A) ExitThread(A)*/
#define _REENTRANT 1 #define _REENTRANT 1
#define HAVE_PTHREAD_ATTR_SETSTACKSIZE 1 #define HAVE_PTHREAD_ATTR_SETSTACKSIZE 1
/*
Windows has two ways to use thread local storage. The most efficient
is using __declspec(thread), but that does not work properly when
used in a .dll that is loaded at runtime, after program load. So for
libmysql.dll and libmysqld.dll we define USE_TLS in order to use the
TlsXxx() API instead, which works in all cases.
*/
#ifdef USE_TLS /* For LIBMYSQL.DLL */
#undef SAFE_MUTEX /* This will cause conflicts */ #undef SAFE_MUTEX /* This will cause conflicts */
#define pthread_key(T,V) DWORD V #define pthread_key(T,V) DWORD V
#define pthread_key_create(A,B) ((*A=TlsAlloc())==0xFFFFFFFF) #define pthread_key_create(A,B) ((*A=TlsAlloc())==0xFFFFFFFF)
#define pthread_key_delete(A) TlsFree(A) #define pthread_key_delete(A) TlsFree(A)
#define my_pthread_setspecific_ptr(T,V) (!TlsSetValue((T),(V)))
#define pthread_setspecific(A,B) (!TlsSetValue((A),(B)))
#define pthread_getspecific(A) (TlsGetValue(A)) #define pthread_getspecific(A) (TlsGetValue(A))
#define my_pthread_getspecific(T,A) ((T) TlsGetValue(A)) #define my_pthread_getspecific(T,A) ((T) TlsGetValue(A))
#define my_pthread_getspecific_ptr(T,V) ((T) TlsGetValue(V)) #define my_pthread_getspecific_ptr(T,V) ((T) TlsGetValue(V))
#define my_pthread_setspecific_ptr(T,V) (!TlsSetValue((T),(V)))
#define pthread_setspecific(A,B) (!TlsSetValue((A),(B)))
#else
#define pthread_key(T,V) __declspec(thread) T V
#define pthread_key_create(A,B) pthread_dummy(0)
#define pthread_key_delete(A) pthread_dummy(0)
#define pthread_getspecific(A) (&(A))
#define my_pthread_getspecific(T,A) (&(A))
#define my_pthread_getspecific_ptr(T,V) (V)
#define my_pthread_setspecific_ptr(T,V) ((T)=(V),0)
#define pthread_setspecific(A,B) win_pthread_setspecific(&(A),(B),sizeof(A))
#endif /* USE_TLS */
#define pthread_equal(A,B) ((A) == (B)) #define pthread_equal(A,B) ((A) == (B))
#define pthread_mutex_init(A,B) (InitializeCriticalSection(A),0) #define pthread_mutex_init(A,B) (InitializeCriticalSection(A),0)
......
...@@ -100,29 +100,13 @@ SET(CLIENT_SOURCES ../mysys/array.c ../strings/bchange.c ../strings/bmove.c ...@@ -100,29 +100,13 @@ SET(CLIENT_SOURCES ../mysys/array.c ../strings/bchange.c ../strings/bmove.c
../vio/viossl.c ../vio/viosslfactories.c ../strings/xml.c ../mysys/mf_qsort.c ../vio/viossl.c ../vio/viosslfactories.c ../strings/xml.c ../mysys/mf_qsort.c
../mysys/my_getsystime.c ../mysys/my_sync.c ../mysys/my_winerr.c ../mysys/my_winfile.c ${LIB_SOURCES}) ../mysys/my_getsystime.c ../mysys/my_sync.c ../mysys/my_winerr.c ../mysys/my_winfile.c ${LIB_SOURCES})
# Need to set USE_TLS for building the DLL, since __declspec(thread)
# approach to thread local storage does not work properly in DLLs.
#
# The static library might be used to form another DLL, as is the case
# with the ODBC driver, so it has to be compiled with USE_TLS as well.
#
# We create a third library without USE_TLS for internal use. We can't
# be sure that some client application part of this build doesn't go
# beond the documented API, and try access the Thread Local Storage.
# The "_notls" means no Tls*() functions used, i.e. "static" TLS.
ADD_LIBRARY(mysqlclient STATIC ${CLIENT_SOURCES}) ADD_LIBRARY(mysqlclient STATIC ${CLIENT_SOURCES})
ADD_DEPENDENCIES(mysqlclient GenError) ADD_DEPENDENCIES(mysqlclient GenError)
TARGET_LINK_LIBRARIES(mysqlclient) TARGET_LINK_LIBRARIES(mysqlclient)
ADD_LIBRARY(mysqlclient_notls STATIC ${CLIENT_SOURCES})
ADD_DEPENDENCIES(mysqlclient_notls GenError)
TARGET_LINK_LIBRARIES(mysqlclient_notls)
ADD_LIBRARY(libmysql SHARED ${CLIENT_SOURCES} dll.c libmysql.def) ADD_LIBRARY(libmysql SHARED ${CLIENT_SOURCES} dll.c libmysql.def)
IF(WIN32)
SET_TARGET_PROPERTIES(libmysql mysqlclient PROPERTIES COMPILE_FLAGS "-DUSE_TLS")
ENDIF(WIN32)
ADD_DEPENDENCIES(libmysql GenError) ADD_DEPENDENCIES(libmysql GenError)
TARGET_LINK_LIBRARIES(libmysql wsock32) TARGET_LINK_LIBRARIES(libmysql wsock32)
......
...@@ -16,12 +16,6 @@ ...@@ -16,12 +16,6 @@
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DSAFEMALLOC -DSAFE_MUTEX") SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DSAFEMALLOC -DSAFE_MUTEX")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DSAFEMALLOC -DSAFE_MUTEX") SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DSAFEMALLOC -DSAFE_MUTEX")
# Need to set USE_TLS, since __declspec(thread) approach to thread local
# storage does not work properly in DLLs.
IF(WIN32)
ADD_DEFINITIONS(-DUSE_TLS)
ENDIF(WIN32)
ADD_DEFINITIONS(-DMYSQL_SERVER -DEMBEDDED_LIBRARY -DHAVE_DLOPEN) ADD_DEFINITIONS(-DMYSQL_SERVER -DEMBEDDED_LIBRARY -DHAVE_DLOPEN)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include
......
...@@ -20,9 +20,6 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ...@@ -20,9 +20,6 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/extra/yassl/include) ${CMAKE_SOURCE_DIR}/extra/yassl/include)
# Currently does not work with DBUG, there are missing symbols reported. # Currently does not work with DBUG, there are missing symbols reported.
IF(WIN32)
ADD_DEFINITIONS(-DUSE_TLS)
ENDIF(WIN32)
ADD_DEFINITIONS(-DEMBEDDED_LIBRARY) ADD_DEFINITIONS(-DEMBEDDED_LIBRARY)
......
...@@ -23,11 +23,7 @@ ...@@ -23,11 +23,7 @@
#include <signal.h> #include <signal.h>
#ifdef THREAD #ifdef THREAD
#ifdef USE_TLS
pthread_key(struct st_my_thread_var*, THR_KEY_mysys); pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
#else
pthread_key(struct st_my_thread_var, THR_KEY_mysys);
#endif /* USE_TLS */
pthread_mutex_t THR_LOCK_malloc,THR_LOCK_open, pthread_mutex_t THR_LOCK_malloc,THR_LOCK_open,
THR_LOCK_lock,THR_LOCK_isam,THR_LOCK_myisam,THR_LOCK_heap, THR_LOCK_lock,THR_LOCK_isam,THR_LOCK_myisam,THR_LOCK_heap,
THR_LOCK_net, THR_LOCK_charset, THR_LOCK_threads, THR_LOCK_time; THR_LOCK_net, THR_LOCK_charset, THR_LOCK_threads, THR_LOCK_time;
...@@ -258,7 +254,6 @@ my_bool my_thread_init(void) ...@@ -258,7 +254,6 @@ my_bool my_thread_init(void)
(ulong) pthread_self()); (ulong) pthread_self());
#endif #endif
#if !defined(__WIN__) || defined(USE_TLS)
if (my_pthread_getspecific(struct st_my_thread_var *,THR_KEY_mysys)) if (my_pthread_getspecific(struct st_my_thread_var *,THR_KEY_mysys))
{ {
#ifdef EXTRA_DEBUG_THREADS #ifdef EXTRA_DEBUG_THREADS
...@@ -273,15 +268,6 @@ my_bool my_thread_init(void) ...@@ -273,15 +268,6 @@ my_bool my_thread_init(void)
goto end; goto end;
} }
pthread_setspecific(THR_KEY_mysys,tmp); pthread_setspecific(THR_KEY_mysys,tmp);
#else /* defined(__WIN__) && !(defined(USE_TLS) */
/*
Skip initialization if the thread specific variable is already initialized
*/
if (THR_KEY_mysys.id)
goto end;
tmp= &THR_KEY_mysys;
#endif
#if defined(__WIN__) && defined(EMBEDDED_LIBRARY) #if defined(__WIN__) && defined(EMBEDDED_LIBRARY)
tmp->pthread_self= (pthread_t) getpid(); tmp->pthread_self= (pthread_t) getpid();
#else #else
...@@ -342,11 +328,7 @@ void my_thread_end(void) ...@@ -342,11 +328,7 @@ void my_thread_end(void)
pthread_cond_destroy(&tmp->suspend); pthread_cond_destroy(&tmp->suspend);
#endif #endif
pthread_mutex_destroy(&tmp->mutex); pthread_mutex_destroy(&tmp->mutex);
#if !defined(__WIN__) || defined(USE_TLS)
free(tmp); free(tmp);
#else
tmp->init= 0;
#endif
/* /*
Decrement counter for number of running threads. We are using this Decrement counter for number of running threads. We are using this
...@@ -360,10 +342,7 @@ void my_thread_end(void) ...@@ -360,10 +342,7 @@ void my_thread_end(void)
pthread_cond_signal(&THR_COND_threads); pthread_cond_signal(&THR_COND_threads);
pthread_mutex_unlock(&THR_LOCK_threads); pthread_mutex_unlock(&THR_LOCK_threads);
} }
/* The following free has to be done, even if my_thread_var() is 0 */
#if !defined(__WIN__) || defined(USE_TLS)
pthread_setspecific(THR_KEY_mysys,0); pthread_setspecific(THR_KEY_mysys,0);
#endif
} }
struct st_my_thread_var *_my_thread_var(void) struct st_my_thread_var *_my_thread_var(void)
......
...@@ -129,12 +129,5 @@ void pthread_exit(void *a) ...@@ -129,12 +129,5 @@ void pthread_exit(void *a)
_endthread(); _endthread();
} }
/* This is neaded to get the macro pthread_setspecific to work */
int win_pthread_setspecific(void *a,void *b,uint length)
{
memcpy(a,b,length);
return 0;
}
#endif #endif
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# About "mysqlclient_notls", see note in "client/CMakeLists.txt"
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DSAFEMALLOC -DSAFE_MUTEX") SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DSAFEMALLOC -DSAFE_MUTEX")
ADD_DEFINITIONS("-DMYSQL_CLIENT") ADD_DEFINITIONS("-DMYSQL_CLIENT")
...@@ -21,7 +20,7 @@ ADD_DEFINITIONS("-DMYSQL_CLIENT") ...@@ -21,7 +20,7 @@ ADD_DEFINITIONS("-DMYSQL_CLIENT")
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
ADD_EXECUTABLE(mysql_client_test mysql_client_test.c ../mysys/my_memmem.c) ADD_EXECUTABLE(mysql_client_test mysql_client_test.c ../mysys/my_memmem.c)
TARGET_LINK_LIBRARIES(mysql_client_test mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(mysql_client_test mysqlclient wsock32)
ADD_EXECUTABLE(bug25714 bug25714.c) ADD_EXECUTABLE(bug25714 bug25714.c)
TARGET_LINK_LIBRARIES(bug25714 mysqlclient_notls wsock32) TARGET_LINK_LIBRARIES(bug25714 mysqlclient wsock32)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment