1 | // |
---|
2 | // ThreadLocal.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/ThreadLocal.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Threading |
---|
8 | // Module: Thread |
---|
9 | // |
---|
10 | // Definition of the ThreadLocal template and related classes. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_ThreadLocal_INCLUDED |
---|
40 | #define Foundation_ThreadLocal_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include <map> |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | |
---|
49 | |
---|
50 | class Foundation_API TLSAbstractSlot |
---|
51 | /// This is the base class for all objects |
---|
52 | /// that the ThreadLocalStorage class manages. |
---|
53 | { |
---|
54 | public: |
---|
55 | TLSAbstractSlot(); |
---|
56 | virtual ~TLSAbstractSlot(); |
---|
57 | }; |
---|
58 | |
---|
59 | |
---|
60 | template <class C> |
---|
61 | class TLSSlot: public TLSAbstractSlot |
---|
62 | /// The Slot template wraps another class |
---|
63 | /// so that it can be stored in a ThreadLocalStorage |
---|
64 | /// object. This class is used internally, and you |
---|
65 | /// must not create instances of it yourself. |
---|
66 | { |
---|
67 | public: |
---|
68 | TLSSlot(): |
---|
69 | _value() |
---|
70 | { |
---|
71 | } |
---|
72 | |
---|
73 | ~TLSSlot() |
---|
74 | { |
---|
75 | } |
---|
76 | |
---|
77 | C& value() |
---|
78 | { |
---|
79 | return _value; |
---|
80 | } |
---|
81 | |
---|
82 | private: |
---|
83 | TLSSlot(const TLSSlot&); |
---|
84 | TLSSlot& operator = (const TLSSlot&); |
---|
85 | |
---|
86 | C _value; |
---|
87 | }; |
---|
88 | |
---|
89 | |
---|
90 | class Foundation_API ThreadLocalStorage |
---|
91 | /// This class manages the local storage for each thread. |
---|
92 | /// Never use this class directly, always use the |
---|
93 | /// ThreadLocal template for managing thread local storage. |
---|
94 | { |
---|
95 | public: |
---|
96 | ThreadLocalStorage(); |
---|
97 | /// Creates the TLS. |
---|
98 | |
---|
99 | ~ThreadLocalStorage(); |
---|
100 | /// Deletes the TLS. |
---|
101 | |
---|
102 | TLSAbstractSlot*& get(const void* key); |
---|
103 | /// Returns the slot for the given key. |
---|
104 | |
---|
105 | static ThreadLocalStorage& current(); |
---|
106 | /// Returns the TLS object for the current thread |
---|
107 | /// (which may also be the main thread). |
---|
108 | |
---|
109 | static void clear(); |
---|
110 | /// Clears the current thread's TLS object. |
---|
111 | /// Does nothing in the main thread. |
---|
112 | |
---|
113 | private: |
---|
114 | typedef std::map<const void*, TLSAbstractSlot*> TLSMap; |
---|
115 | |
---|
116 | TLSMap _map; |
---|
117 | |
---|
118 | friend class Thread; |
---|
119 | }; |
---|
120 | |
---|
121 | |
---|
122 | template <class C> |
---|
123 | class ThreadLocal |
---|
124 | /// This template is used to declare type safe thread |
---|
125 | /// local variables. It can basically be used like |
---|
126 | /// a smart pointer class with the special feature |
---|
127 | /// that it references a different object |
---|
128 | /// in every thread. The underlying object will |
---|
129 | /// be created when it is referenced for the first |
---|
130 | /// time. |
---|
131 | /// See the NestedDiagnosticContext class for an |
---|
132 | /// example how to use this template. |
---|
133 | /// Every thread only has access to its own |
---|
134 | /// thread local data. There is no way for a thread |
---|
135 | /// to access another thread's local data. |
---|
136 | { |
---|
137 | typedef TLSSlot<C> Slot; |
---|
138 | |
---|
139 | public: |
---|
140 | ThreadLocal() |
---|
141 | { |
---|
142 | } |
---|
143 | |
---|
144 | ~ThreadLocal() |
---|
145 | { |
---|
146 | } |
---|
147 | |
---|
148 | C* operator -> () |
---|
149 | { |
---|
150 | return &get(); |
---|
151 | } |
---|
152 | |
---|
153 | C& operator * () |
---|
154 | /// "Dereferences" the smart pointer and returns a reference |
---|
155 | /// to the underlying data object. The reference can be used |
---|
156 | /// to modify the object. |
---|
157 | { |
---|
158 | return get(); |
---|
159 | } |
---|
160 | |
---|
161 | C& get() |
---|
162 | /// Returns a reference to the underlying data object. |
---|
163 | /// The reference can be used to modify the object. |
---|
164 | { |
---|
165 | TLSAbstractSlot*& p = ThreadLocalStorage::current().get(this); |
---|
166 | if (!p) p = new Slot; |
---|
167 | return static_cast<Slot*>(p)->value(); |
---|
168 | } |
---|
169 | |
---|
170 | private: |
---|
171 | ThreadLocal(const ThreadLocal&); |
---|
172 | ThreadLocal& operator = (const ThreadLocal&); |
---|
173 | }; |
---|
174 | |
---|
175 | |
---|
176 | } // namespace Poco |
---|
177 | |
---|
178 | |
---|
179 | #endif // Foundation_ThreadLocal_INCLUDED |
---|