]>
Commit | Line | Data |
---|---|---|
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 (notice that using destructor function is Pthreads-specific and | |
25 | // not supported in Win32 implementation) | |
26 | wxTlsKey(void (*destructor)(void *) = NULL) | |
27 | { | |
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 | return pthread_setspecific(m_key, value) == 0; | |
45 | } | |
46 | ||
47 | // free the key | |
48 | ~wxTlsKey() | |
49 | { | |
50 | if ( IsOk() ) | |
51 | pthread_key_delete(m_key); | |
52 | } | |
53 | ||
54 | private: | |
55 | pthread_key_t m_key; | |
56 | ||
57 | DECLARE_NO_COPY_CLASS(wxTlsKey) | |
58 | }; | |
59 | ||
60 | #endif // _WX_UNIX_TLS_H_ | |
61 |