1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: OS/2 implementation of wxTlsValue<>
7 // Copyright: (c) 2008 Stefan Neis
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_OS2_TLS_H_
12 #define _WX_OS2_TLS_H_
14 #include "wx/os2/private.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 APIRET rc
= ::DosAllocThreadLocalMemory(1, &m_slot
);
34 // return true if the key was successfully allocated
35 bool IsOk() const { return m_slot
!= NULL
; }
37 // get the key value, there is no error return
40 return (void *)m_slot
;
43 // change the key value, return true if ok
48 m_slot
= (ULONG
*)value
;
53 // update m_allValues list of all values - remove old, add new
54 wxCriticalSectionLocker
lock(m_csAllValues
);
57 for ( wxVector
<void*>::iterator i
= m_allValues
.begin();
58 i
!= m_allValues
.end();
70 wxFAIL_MSG( "previous wxTlsKey value not recorded in m_allValues" );
74 m_allValues
.push_back(value
);
85 // Win32 and OS/2 API doesn't have the equivalent of pthread's
86 // destructor, so we have to keep track of all allocated values and
87 // destroy them manually; ideally we'd do that at thread exit time, but
88 // since we could only do that with wxThread and not otherwise created
89 // threads, we do it here.
91 // TODO: We should still call destructors for wxTlsKey used in the
92 // thread from wxThread's thread shutdown code, *in addition*
93 // to doing it in ~wxTlsKey.
95 // NB: No need to lock m_csAllValues, by the time this code is called,
96 // no other thread can be using this key.
97 for ( wxVector
<void*>::iterator i
= m_allValues
.begin();
98 i
!= m_allValues
.end();
104 ::DosFreeThreadLocalMemory(m_slot
);
108 wxTlsDestructorFunction m_destructor
;
111 wxVector
<void*> m_allValues
;
112 wxCriticalSection m_csAllValues
;
114 wxDECLARE_NO_COPY_CLASS(wxTlsKey
);
117 #endif // _WX_OS2_TLS_H_