1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: OS/2 implementation of wxTlsValue<>
6 // Copyright: (c) 2008 Stefan Neis
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_OS2_TLS_H_
11 #define _WX_OS2_TLS_H_
13 #include "wx/os2/private.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 APIRET rc
= ::DosAllocThreadLocalMemory(1, &m_slot
);
33 // return true if the key was successfully allocated
34 bool IsOk() const { return m_slot
!= NULL
; }
36 // get the key value, there is no error return
39 return (void *)m_slot
;
42 // change the key value, return true if ok
47 m_slot
= (ULONG
*)value
;
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 and OS/2 API doesn't have the equivalent of pthread's
85 // destructor, so we have to keep track of all allocated values and
86 // destroy them manually; ideally we'd do that at thread exit time, but
87 // since we could only do that with wxThread and not otherwise created
88 // threads, we do it here.
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();
103 ::DosFreeThreadLocalMemory(m_slot
);
107 wxTlsDestructorFunction m_destructor
;
110 wxVector
<void*> m_allValues
;
111 wxCriticalSection m_csAllValues
;
113 wxDECLARE_NO_COPY_CLASS(wxTlsKey
);
116 #endif // _WX_OS2_TLS_H_