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