1
///////////////////////////////////////////////////////////////////////////////
3
// Purpose: Win32 implementation of wxTlsValue<>
4
// Author: Vadim Zeitlin
6
// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7
// Licence: wxWindows licence
8
///////////////////////////////////////////////////////////////////////////////
10
#ifndef _WX_MSW_TLS_H_
11
#define _WX_MSW_TLS_H_
13
#include "wx/msw/wrapwin.h"
14
#include "wx/thread.h"
15
#include "wx/vector.h"
17
// ----------------------------------------------------------------------------
18
// wxTlsKey is a helper class encapsulating a TLS slot
19
// ----------------------------------------------------------------------------
24
// ctor allocates a new key
25
wxTlsKey(wxTlsDestructorFunction destructor)
27
m_destructor = destructor;
28
m_slot = ::TlsAlloc();
31
// return true if the key was successfully allocated
32
bool IsOk() const { return m_slot != TLS_OUT_OF_INDEXES; }
34
// get the key value, there is no error return
37
return ::TlsGetValue(m_slot);
40
// change the key value, return true if ok
45
if ( ::TlsSetValue(m_slot, value) == 0 )
51
// update m_allValues list of all values - remove old, add new
52
wxCriticalSectionLocker lock(m_csAllValues);
55
for ( wxVector<void*>::iterator i = m_allValues.begin();
56
i != m_allValues.end();
68
wxFAIL_MSG( "previous wxTlsKey value not recorded in m_allValues" );
72
m_allValues.push_back(value);
83
// Win32 API doesn't have the equivalent of pthread's destructor, so we
84
// have to keep track of all allocated values and destroy them manually;
85
// ideally we'd do that at thread exit time, but since we could only
86
// do that with wxThread and not otherwise created threads, we do it
89
// TODO: We should still call destructors for wxTlsKey used in the
90
// thread from wxThread's thread shutdown code, *in addition*
91
// to doing it in ~wxTlsKey.
93
// NB: No need to lock m_csAllValues, by the time this code is called,
94
// no other thread can be using this key.
95
for ( wxVector<void*>::iterator i = m_allValues.begin();
96
i != m_allValues.end();
106
wxTlsDestructorFunction m_destructor;
109
wxVector<void*> m_allValues;
110
wxCriticalSection m_csAllValues;
112
wxDECLARE_NO_COPY_CLASS(wxTlsKey);
115
#endif // _WX_MSW_TLS_H_