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