Commit | Line | Data |
---|---|---|
64a044d5 VZ |
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 | ||
64a044d5 VZ |
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: | |
8b73c531 | 23 | // ctor allocates a new key and possibly registering a destructor function |
49e714e1 VS |
24 | // for it |
25 | wxTlsKey(wxTlsDestructorFunction destructor) | |
64a044d5 | 26 | { |
49e714e1 | 27 | m_destructor = destructor; |
8b73c531 VZ |
28 | if ( pthread_key_create(&m_key, destructor) != 0 ) |
29 | m_key = 0; | |
64a044d5 VZ |
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 | { | |
49e714e1 VS |
44 | void *old = Get(); |
45 | if ( old ) | |
46 | m_destructor(old); | |
47 | ||
8b73c531 | 48 | return pthread_setspecific(m_key, value) == 0; |
64a044d5 VZ |
49 | } |
50 | ||
51 | // free the key | |
52 | ~wxTlsKey() | |
53 | { | |
54 | if ( IsOk() ) | |
55 | pthread_key_delete(m_key); | |
56 | } | |
57 | ||
58 | private: | |
49e714e1 | 59 | wxTlsDestructorFunction m_destructor; |
64a044d5 VZ |
60 | pthread_key_t m_key; |
61 | ||
c0c133e1 | 62 | wxDECLARE_NO_COPY_CLASS(wxTlsKey); |
64a044d5 VZ |
63 | }; |
64 | ||
65 | #endif // _WX_UNIX_TLS_H_ | |
66 |