]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/tls.h
Don't use wxDC in header
[wxWidgets.git] / include / wx / unix / tls.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/tls.h
3 // Purpose: Pthreads implementation of wxTlsValue<>
4 // Author: Vadim Zeitlin
5 // Created: 2008-08-08
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_UNIX_TLS_H_
12 #define _WX_UNIX_TLS_H_
13
14 #include <pthread.h>
15
16 // ----------------------------------------------------------------------------
17 // wxTlsKey is a helper class encapsulating the TLS value index
18 // ----------------------------------------------------------------------------
19
20 class wxTlsKey
21 {
22 public:
23 // ctor allocates a new key and possibly registering a destructor function
24 // for it
25 wxTlsKey(wxTlsDestructorFunction destructor)
26 {
27 m_destructor = destructor;
28 if ( pthread_key_create(&m_key, destructor) != 0 )
29 m_key = 0;
30 }
31
32 // return true if the key was successfully allocated
33 bool IsOk() const { return m_key != 0; }
34
35 // get the key value, there is no error return
36 void *Get() const
37 {
38 return pthread_getspecific(m_key);
39 }
40
41 // change the key value, return true if ok
42 bool Set(void *value)
43 {
44 void *old = Get();
45 if ( old )
46 m_destructor(old);
47
48 return pthread_setspecific(m_key, value) == 0;
49 }
50
51 // free the key
52 ~wxTlsKey()
53 {
54 if ( IsOk() )
55 pthread_key_delete(m_key);
56 }
57
58 private:
59 wxTlsDestructorFunction m_destructor;
60 pthread_key_t m_key;
61
62 wxDECLARE_NO_COPY_CLASS(wxTlsKey);
63 };
64
65 #endif // _WX_UNIX_TLS_H_
66