]>
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 "wx/intl.h" | |
15 | #include "wx/log.h" | |
16 | ||
17 | #include <pthread.h> | |
18 | ||
19 | // ---------------------------------------------------------------------------- | |
20 | // wxTlsKey is a helper class encapsulating the TLS value index | |
21 | // ---------------------------------------------------------------------------- | |
22 | ||
23 | class wxTlsKey | |
24 | { | |
25 | public: | |
26 | // ctor allocates a new key | |
27 | wxTlsKey() | |
28 | { | |
29 | int rc = pthread_key_create(&m_key, NULL); | |
30 | if ( rc ) | |
31 | wxLogSysError(_("Creating TLS key failed"), rc); | |
32 | } | |
33 | ||
34 | // return true if the key was successfully allocated | |
35 | bool IsOk() const { return m_key != 0; } | |
36 | ||
37 | // get the key value, there is no error return | |
38 | void *Get() const | |
39 | { | |
40 | return pthread_getspecific(m_key); | |
41 | } | |
42 | ||
43 | // change the key value, return true if ok | |
44 | bool Set(void *value) | |
45 | { | |
46 | int rc = pthread_setspecific(m_key, value); | |
47 | if ( rc ) | |
48 | { | |
49 | wxLogSysError(_("Failed to set TLS value")); | |
50 | return false; | |
51 | } | |
52 | ||
53 | return true; | |
54 | } | |
55 | ||
56 | // free the key | |
57 | ~wxTlsKey() | |
58 | { | |
59 | if ( IsOk() ) | |
60 | pthread_key_delete(m_key); | |
61 | } | |
62 | ||
63 | private: | |
64 | pthread_key_t m_key; | |
65 | ||
66 | DECLARE_NO_COPY_CLASS(wxTlsKey) | |
67 | }; | |
68 | ||
69 | #endif // _WX_UNIX_TLS_H_ | |
70 |