]> git.saurik.com Git - wxWidgets.git/blame - include/wx/os2/tls.h
Fix wxPropertyGrid::GetPropertyRect when the last item is collapsed.
[wxWidgets.git] / include / wx / os2 / tls.h
CommitLineData
f35f85a9
SN
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/os2/tls.h
3// Purpose: OS/2 implementation of wxTlsValue<>
4// Author: Stefan Neis
5// Created: 2008-08-30
f35f85a9
SN
6// Copyright: (c) 2008 Stefan Neis
7// Licence: wxWindows licence
8///////////////////////////////////////////////////////////////////////////////
9
fcc9c05e
SN
10#ifndef _WX_OS2_TLS_H_
11#define _WX_OS2_TLS_H_
f35f85a9
SN
12
13#include "wx/os2/private.h"
49e714e1
VS
14#include "wx/thread.h"
15#include "wx/vector.h"
f35f85a9
SN
16
17// ----------------------------------------------------------------------------
18// wxTlsKey is a helper class encapsulating a TLS slot
19// ----------------------------------------------------------------------------
20
21class wxTlsKey
22{
23public:
24 // ctor allocates a new key
49e714e1 25 wxTlsKey(wxTlsDestructorFunction destructor)
f35f85a9 26 {
49e714e1 27 m_destructor = destructor;
f35f85a9 28 APIRET rc = ::DosAllocThreadLocalMemory(1, &m_slot);
2c6dbc21
VZ
29 if (rc != NO_ERROR)
30 m_slot = NULL;
f35f85a9
SN
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 {
49e714e1
VS
45 void *old = Get();
46
f35f85a9 47 m_slot = (ULONG*)value;
49e714e1
VS
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
2c6dbc21 75 return true;
f35f85a9
SN
76 }
77
78 // free the key
79 ~wxTlsKey()
80 {
49e714e1
VS
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);
f35f85a9
SN
104 }
105
106private:
49e714e1 107 wxTlsDestructorFunction m_destructor;
f35f85a9
SN
108 ULONG* m_slot;
109
49e714e1
VS
110 wxVector<void*> m_allValues;
111 wxCriticalSection m_csAllValues;
112
c0c133e1 113 wxDECLARE_NO_COPY_CLASS(wxTlsKey);
f35f85a9
SN
114};
115
fcc9c05e 116#endif // _WX_OS2_TLS_H_
f35f85a9 117