]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/unix/tls.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / include / wx / unix / tls.h
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/unix/tls.h
3// Purpose: Pthreads implementation of wxTlsValue<>
4// Author: Vadim Zeitlin
5// Created: 2008-08-08
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
13#include <pthread.h>
14
15// ----------------------------------------------------------------------------
16// wxTlsKey is a helper class encapsulating the TLS value index
17// ----------------------------------------------------------------------------
18
19class wxTlsKey
20{
21public:
22 // ctor allocates a new key and possibly registering a destructor function
23 // for it
24 wxTlsKey(wxTlsDestructorFunction destructor)
25 {
26 m_destructor = destructor;
27 if ( pthread_key_create(&m_key, destructor) != 0 )
28 m_key = 0;
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 {
43 void *old = Get();
44 if ( old )
45 m_destructor(old);
46
47 return pthread_setspecific(m_key, value) == 0;
48 }
49
50 // free the key
51 ~wxTlsKey()
52 {
53 if ( IsOk() )
54 pthread_key_delete(m_key);
55 }
56
57private:
58 wxTlsDestructorFunction m_destructor;
59 pthread_key_t m_key;
60
61 wxDECLARE_NO_COPY_CLASS(wxTlsKey);
62};
63
64#endif // _WX_UNIX_TLS_H_
65