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