]> git.saurik.com Git - wxWidgets.git/blob - include/wx/os2/tls.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / include / wx / os2 / tls.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/os2/tls.h
3 // Purpose: OS/2 implementation of wxTlsValue<>
4 // Author: Stefan Neis
5 // Created: 2008-08-30
6 // Copyright: (c) 2008 Stefan Neis
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_OS2_TLS_H_
11 #define _WX_OS2_TLS_H_
12
13 #include "wx/os2/private.h"
14 #include "wx/thread.h"
15 #include "wx/vector.h"
16
17 // ----------------------------------------------------------------------------
18 // wxTlsKey is a helper class encapsulating a TLS slot
19 // ----------------------------------------------------------------------------
20
21 class wxTlsKey
22 {
23 public:
24 // ctor allocates a new key
25 wxTlsKey(wxTlsDestructorFunction destructor)
26 {
27 m_destructor = destructor;
28 APIRET rc = ::DosAllocThreadLocalMemory(1, &m_slot);
29 if (rc != NO_ERROR)
30 m_slot = NULL;
31 }
32
33 // return true if the key was successfully allocated
34 bool IsOk() const { return m_slot != NULL; }
35
36 // get the key value, there is no error return
37 void *Get() const
38 {
39 return (void *)m_slot;
40 }
41
42 // change the key value, return true if ok
43 bool Set(void *value)
44 {
45 void *old = Get();
46
47 m_slot = (ULONG*)value;
48
49 if ( old )
50 m_destructor(old);
51
52 // update m_allValues list of all values - remove old, add new
53 wxCriticalSectionLocker lock(m_csAllValues);
54 if ( old )
55 {
56 for ( wxVector<void*>::iterator i = m_allValues.begin();
57 i != m_allValues.end();
58 ++i )
59 {
60 if ( *i == old )
61 {
62 if ( value )
63 *i = value;
64 else
65 m_allValues.erase(i);
66 return true;
67 }
68 }
69 wxFAIL_MSG( "previous wxTlsKey value not recorded in m_allValues" );
70 }
71
72 if ( value )
73 m_allValues.push_back(value);
74
75 return true;
76 }
77
78 // free the key
79 ~wxTlsKey()
80 {
81 if ( !IsOk() )
82 return;
83
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.
89 //
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.
93 //
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();
98 ++i )
99 {
100 m_destructor(*i);
101 }
102
103 ::DosFreeThreadLocalMemory(m_slot);
104 }
105
106 private:
107 wxTlsDestructorFunction m_destructor;
108 ULONG* m_slot;
109
110 wxVector<void*> m_allValues;
111 wxCriticalSection m_csAllValues;
112
113 wxDECLARE_NO_COPY_CLASS(wxTlsKey);
114 };
115
116 #endif // _WX_OS2_TLS_H_
117