2008-08-07 22:59:32 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/unix/tls.h
|
|
|
|
// Purpose: Pthreads implementation of wxTlsValue<>
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2008-08-08
|
|
|
|
// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_UNIX_TLS_H_
|
|
|
|
#define _WX_UNIX_TLS_H_
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxTlsKey is a helper class encapsulating the TLS value index
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class wxTlsKey
|
|
|
|
{
|
|
|
|
public:
|
2008-08-29 19:28:42 -04:00
|
|
|
// ctor allocates a new key and possibly registering a destructor function
|
2010-03-08 07:21:58 -05:00
|
|
|
// for it
|
|
|
|
wxTlsKey(wxTlsDestructorFunction destructor)
|
2008-08-07 22:59:32 -04:00
|
|
|
{
|
2010-03-08 07:21:58 -05:00
|
|
|
m_destructor = destructor;
|
2008-08-29 19:28:42 -04:00
|
|
|
if ( pthread_key_create(&m_key, destructor) != 0 )
|
|
|
|
m_key = 0;
|
2008-08-07 22:59:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// return true if the key was successfully allocated
|
|
|
|
bool IsOk() const { return m_key != 0; }
|
|
|
|
|
|
|
|
// get the key value, there is no error return
|
|
|
|
void *Get() const
|
|
|
|
{
|
|
|
|
return pthread_getspecific(m_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// change the key value, return true if ok
|
|
|
|
bool Set(void *value)
|
|
|
|
{
|
2010-03-08 07:21:58 -05:00
|
|
|
void *old = Get();
|
|
|
|
if ( old )
|
|
|
|
m_destructor(old);
|
|
|
|
|
2008-08-29 19:28:42 -04:00
|
|
|
return pthread_setspecific(m_key, value) == 0;
|
2008-08-07 22:59:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// free the key
|
|
|
|
~wxTlsKey()
|
|
|
|
{
|
|
|
|
if ( IsOk() )
|
|
|
|
pthread_key_delete(m_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-03-08 07:21:58 -05:00
|
|
|
wxTlsDestructorFunction m_destructor;
|
2008-08-07 22:59:32 -04:00
|
|
|
pthread_key_t m_key;
|
|
|
|
|
2009-02-08 06:45:59 -05:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxTlsKey);
|
2008-08-07 22:59:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _WX_UNIX_TLS_H_
|
|
|
|
|