]>
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 | |
64a044d5 VZ |
6 | // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org> |
7 | // Licence: wxWindows licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifndef _WX_UNIX_TLS_H_ | |
11 | #define _WX_UNIX_TLS_H_ | |
12 | ||
64a044d5 VZ |
13 | #include <pthread.h> |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // wxTlsKey is a helper class encapsulating the TLS value index | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | class wxTlsKey | |
20 | { | |
21 | public: | |
8b73c531 | 22 | // ctor allocates a new key and possibly registering a destructor function |
49e714e1 VS |
23 | // for it |
24 | wxTlsKey(wxTlsDestructorFunction destructor) | |
64a044d5 | 25 | { |
49e714e1 | 26 | m_destructor = destructor; |
8b73c531 VZ |
27 | if ( pthread_key_create(&m_key, destructor) != 0 ) |
28 | m_key = 0; | |
64a044d5 VZ |
29 | } |
30 | ||
31 | // return true if the key was successfully allocated | |
32 | bool IsOk() const { return m_key != 0; } | |
33 | ||
34 | // get the key value, there is no error return | |
35 | void *Get() const | |
36 | { | |
37 | return pthread_getspecific(m_key); | |
38 | } | |
39 | ||
40 | // change the key value, return true if ok | |
41 | bool Set(void *value) | |
42 | { | |
49e714e1 VS |
43 | void *old = Get(); |
44 | if ( old ) | |
45 | m_destructor(old); | |
46 | ||
8b73c531 | 47 | return pthread_setspecific(m_key, value) == 0; |
64a044d5 VZ |
48 | } |
49 | ||
50 | // free the key | |
51 | ~wxTlsKey() | |
52 | { | |
53 | if ( IsOk() ) | |
54 | pthread_key_delete(m_key); | |
55 | } | |
56 | ||
57 | private: | |
49e714e1 | 58 | wxTlsDestructorFunction m_destructor; |
64a044d5 VZ |
59 | pthread_key_t m_key; |
60 | ||
c0c133e1 | 61 | wxDECLARE_NO_COPY_CLASS(wxTlsKey); |
64a044d5 VZ |
62 | }; |
63 | ||
64 | #endif // _WX_UNIX_TLS_H_ | |
65 |