]> git.saurik.com Git - wxWidgets.git/blob - src/common/threadinfo.cpp
Make storing non-trivial data in wxThreadSpecificInfo possible.
[wxWidgets.git] / src / common / threadinfo.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/threadinfo.cpp
3 // Purpose: declaration of wxThreadSpecificInfo: thread-specific information
4 // Author: Vaclav Slavik
5 // Created: 2013-09-14
6 // Copyright: (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if defined(__BORLANDC__)
14 #pragma hdrstop
15 #endif
16
17 #include "wx/private/threadinfo.h"
18
19 #if wxUSE_THREADS
20
21 #include "wx/tls.h"
22 #include "wx/thread.h"
23 #include "wx/sharedptr.h"
24 #include "wx/vector.h"
25
26 namespace
27 {
28
29 // All thread info objects are stored in a global list so that they are
30 // freed when global objects are destroyed and no memory leaks are reported.
31 //
32 // TODO: This could be made more efficient by freeing g_thisThreadInfo when
33 // wxThread terminates.
34 wxCriticalSection g_csAllThreadInfos;
35 wxVector< wxSharedPtr<wxThreadSpecificInfo> > g_allThreadInfos;
36
37 // Pointer to currenct thread's instance
38 wxTLS_TYPE(wxThreadSpecificInfo*) g_thisThreadInfo;
39
40 } // anonymous namespace
41
42
43 wxThreadSpecificInfo& wxThreadSpecificInfo::Get()
44 {
45 if ( !wxTLS_VALUE(g_thisThreadInfo) )
46 {
47 wxTLS_VALUE(g_thisThreadInfo) = new wxThreadSpecificInfo;
48 wxCriticalSectionLocker lock(g_csAllThreadInfos);
49 g_allThreadInfos.push_back(
50 wxSharedPtr<wxThreadSpecificInfo>(wxTLS_VALUE(g_thisThreadInfo)));
51 }
52 return *wxTLS_VALUE(g_thisThreadInfo);
53 }
54
55 #else // !wxUSE_THREADS
56
57 wxThreadSpecificInfo& wxThreadSpecificInfo::Get()
58 {
59 static wxThreadSpecificInfo s_instance;
60 return s_instance;
61 }
62
63 #endif // wxUSE_THREADS/wxUSE_THREADS