chanegd wxTlsValue to be pointer-like instead of value-like which doesn't work for UDTs; use __thread keyword with mingw32 >= 4.3 too; use library-based thread-specific variables support in wxString cache now that it is fixed to work there; finally added a unit test for TLS stuff

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55361 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-08-29 23:28:42 +00:00
parent b18f47d002
commit 8b73c5318c
17 changed files with 320 additions and 83 deletions

View File

@@ -11,9 +11,6 @@
#ifndef _WX_MSW_TLS_H_
#define _WX_MSW_TLS_H_
#include "wx/log.h"
#include "wx/intl.h"
#include "wx/msw/wrapwin.h"
// ----------------------------------------------------------------------------
@@ -27,8 +24,6 @@ public:
wxTlsKey()
{
m_slot = ::TlsAlloc();
if ( m_slot == TLS_OUT_OF_INDEXES )
wxLogError("Creating TLS key failed");
}
// return true if the key was successfully allocated
@@ -43,25 +38,14 @@ public:
// change the key value, return true if ok
bool Set(void *value)
{
if ( !::TlsSetValue(m_slot, value) )
{
wxLogSysError(_("Failed to set TLS value"));
return false;
}
return true;
return ::TlsSetValue(m_slot, value) != 0;
}
// free the key
~wxTlsKey()
{
if ( IsOk() )
{
if ( !::TlsFree(m_slot) )
{
wxLogDebug("TlsFree() failed: %08x", ::GetLastError());
}
}
::TlsFree(m_slot);
}
private:

View File

@@ -75,12 +75,6 @@
#define wxUSE_STRING_POS_CACHE 0
#endif
#ifndef wxHAS_COMPILER_TLS
// FIXME: currently the code only works with compiler TLS support
#undef wxUSE_STRING_POS_CACHE
#define wxUSE_STRING_POS_CACHE 0
#endif
#if wxUSE_STRING_POS_CACHE
#include "wx/tls.h"
@@ -600,7 +594,26 @@ private:
unsigned lastUsed;
};
static wxTLS_TYPE(Cache) ms_cache;
// notice that we must use an accessor function and not a static variable
// because when the TLS variables support is implemented in the library (and
// not by the compiler), the global s_cache variable could be not yet
// initialized when a ctor of another global object is executed and if that
// ctor uses any wxString methods, bad things happen
//
// also note that for the same reason this function _is_ MT-safe: we know
// it's going to be called during the program startup (currently during
// globals initialization but even if they ever stop using wxString, it would
// still be called by wxInitialize()), i.e. before any threads are created
static Cache& GetCache()
{
static wxTLS_TYPE(Cache) s_cache;
return wxTLS_VALUE(s_cache);
}
static Cache::Element *GetCacheBegin() { return GetCache().cached; }
static Cache::Element *GetCacheEnd() { return GetCacheBegin() + Cache::SIZE; }
static unsigned& LastUsedCacheElement() { return GetCache().lastUsed; }
friend struct wxStrCacheDumper;
@@ -644,9 +657,7 @@ private:
// profiling seems to show a small but consistent gain if we use this
// simple loop instead of starting from the last used element (there are
// a lot of misses in this function...)
for ( Cache::Element *c = ms_cache.cached;
c != ms_cache.cached + Cache::SIZE;
c++ )
for ( Cache::Element *c = GetCacheBegin(); c != GetCacheEnd(); c++ )
{
if ( c->str == this )
return c;
@@ -660,9 +671,9 @@ private:
// its corresponding index in the byte string or not
Cache::Element *GetCacheElement() const
{
Cache::Element * const cacheBegin = ms_cache.cached;
Cache::Element * const cacheEnd = ms_cache.cached + Cache::SIZE;
Cache::Element * const cacheStart = cacheBegin + ms_cache.lastUsed;
Cache::Element * const cacheBegin = GetCacheBegin();
Cache::Element * const cacheEnd = GetCacheEnd();
Cache::Element * const cacheStart = cacheBegin + LastUsedCacheElement();
// check the last used first, this does no (measurable) harm for a miss
// but does help for simple loops addressing the same string all the time
@@ -685,7 +696,7 @@ private:
c->Reset();
// and remember the last used element
ms_cache.lastUsed = c - cacheBegin;
LastUsedCacheElement() = c - cacheBegin;
}
return c;

View File

@@ -17,9 +17,19 @@
// check for compiler support of thread-specific variables
// ----------------------------------------------------------------------------
#ifdef HAVE___THREAD_KEYWORD
// when not using threads at all, there is no need for thread-specific
// values to be really thread-specific
#if !wxUSE_THREADS
#define wxHAS_COMPILER_TLS
#define wxTHREAD_SPECIFIC_DECL
// __thread keyword is supported if configure detected it or when using mingw32
// >= 4.3 which is known to have it too
#elif defined(HAVE___THREAD_KEYWORD) || \
(defined(__MINGW32__) && wxCHECK_GCC_VERSION(4, 3))
#define wxHAS_COMPILER_TLS
#define wxTHREAD_SPECIFIC_DECL __thread
// MSVC has its own version which might be supported by some other Windows
// compilers, to be tested
#elif wxCHECK_VISUALC_VERSION(7)
#define wxHAS_COMPILER_TLS
#define wxTHREAD_SPECIFIC_DECL __declspec(thread)
@@ -31,6 +41,8 @@
#ifdef wxHAS_COMPILER_TLS
#define wxTLS_TYPE(T) wxTHREAD_SPECIFIC_DECL T
#define wxTLS_PTR(var) (&(var))
#define wxTLS_VALUE(var) (var)
#else // !wxHAS_COMPILER_TLS
#ifdef __WXMSW__
#include "wx/msw/tls.h"
@@ -41,23 +53,76 @@
#error Neither compiler nor OS support thread-specific variables.
#endif
// wxTlsValue<T> represents a thread-specific value of type T
#include <stdlib.h> // for calloc()
// wxTlsValue<T> represents a thread-specific value of type T but, unlike
// with native compiler thread-specific variables, it behaves like a
// (never NULL) pointer to T and so needs to be dereferenced before use
template <typename T>
class wxTlsValue
{
public:
typedef T ValueType;
wxTlsValue() { *this = static_cast<T>(0); }
wxTlsValue& operator=(T value)
// ctor doesn't do anything, the object is created on first access
//
// FIXME: the thread-specific values are currently not freed under
// Windows, resulting in memory leaks, this must be implemented
// there somehow (probably by keeping a list of all TLS objects
// and cleaning them up in wxThread cleanup)
wxTlsValue()
#ifdef __UNIX__
: m_key(free)
#endif
{
m_key.Set(wxUIntToPtr(value));
return *this;
}
operator T() const { return wxPtrToUInt(m_key.Get()); }
// dtor is only called in the main thread context and so is not enough
// to free memory allocated by us for the other threads, we use
// destructor function when using Pthreads for this (which is not
// called for the main thread as it doesn't call pthread_exit() but
// just to be safe we also reset the key anyhow) and simply leak the
// memory under Windows (see the FIXME above)
~wxTlsValue()
{
void * const value = m_key.Get();
if ( value)
{
free(value);
m_key.Set(NULL);
}
}
// access the object creating it on demand
ValueType *Get()
{
void *value = m_key.Get();
if ( !value )
{
// ValueType must be POD to be used in wxHAS_COMPILER_TLS case
// anyhow (at least gcc doesn't accept non-POD values being
// declared with __thread) so initialize it as a POD too
value = calloc(1, sizeof(ValueType));
if ( !m_key.Set(value) )
{
free(value);
// this will probably result in a crash in the caller but
// it's arguably better to crash immediately instead of
// slowly dying from out-of-memory errors which would
// happen as the next access to this object would allocate
// another ValueType instance and so on forever
value = NULL;
}
}
return static_cast<ValueType *>(value);
}
// pointer-like accessors
ValueType *operator->() { return Get(); }
ValueType& operator*() { return *Get(); }
private:
wxTlsKey m_key;
@@ -66,6 +131,8 @@
};
#define wxTLS_TYPE(T) wxTlsValue<T>
#define wxTLS_PTR(var) (var)
#define wxTLS_VALUE(var) (*(var))
#endif // wxHAS_COMPILER_TLS/!wxHAS_COMPILER_TLS
#endif // _WX_TLS_H_

View File

@@ -11,9 +11,6 @@
#ifndef _WX_UNIX_TLS_H_
#define _WX_UNIX_TLS_H_
#include "wx/intl.h"
#include "wx/log.h"
#include <pthread.h>
// ----------------------------------------------------------------------------
@@ -23,12 +20,13 @@
class wxTlsKey
{
public:
// ctor allocates a new key
wxTlsKey()
// ctor allocates a new key and possibly registering a destructor function
// for it (notice that using destructor function is Pthreads-specific and
// not supported in Win32 implementation)
wxTlsKey(void (*destructor)(void *) = NULL)
{
int rc = pthread_key_create(&m_key, NULL);
if ( rc )
wxLogSysError(_("Creating TLS key failed"), rc);
if ( pthread_key_create(&m_key, destructor) != 0 )
m_key = 0;
}
// return true if the key was successfully allocated
@@ -43,14 +41,7 @@ public:
// change the key value, return true if ok
bool Set(void *value)
{
int rc = pthread_setspecific(m_key, value);
if ( rc )
{
wxLogSysError(_("Failed to set TLS value"));
return false;
}
return true;
return pthread_setspecific(m_key, value) == 0;
}
// free the key