4
// $Id: //poco/1.2/Foundation/include/Poco/ThreadLocal.h#2 $
10
// Definition of the ThreadLocal template and related classes.
12
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
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:
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.
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.
39
#ifndef Foundation_ThreadLocal_INCLUDED
40
#define Foundation_ThreadLocal_INCLUDED
43
#include "Poco/Foundation.h"
50
class Foundation_API TLSAbstractSlot
51
/// This is the base class for all objects
52
/// that the ThreadLocalStorage class manages.
56
virtual ~TLSAbstractSlot();
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.
83
TLSSlot(const TLSSlot&);
84
TLSSlot& operator = (const TLSSlot&);
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.
99
~ThreadLocalStorage();
102
TLSAbstractSlot*& get(const void* key);
103
/// Returns the slot for the given key.
105
static ThreadLocalStorage& current();
106
/// Returns the TLS object for the current thread
107
/// (which may also be the main thread).
110
/// Clears the current thread's TLS object.
111
/// Does nothing in the main thread.
114
typedef std::map<const void*, TLSAbstractSlot*> TLSMap;
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
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.
137
typedef TLSSlot<C> Slot;
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.
162
/// Returns a reference to the underlying data object.
163
/// The reference can be used to modify the object.
165
TLSAbstractSlot*& p = ThreadLocalStorage::current().get(this);
166
if (!p) p = new Slot;
167
return static_cast<Slot*>(p)->value();
171
ThreadLocal(const ThreadLocal&);
172
ThreadLocal& operator = (const ThreadLocal&);
179
#endif // Foundation_ThreadLocal_INCLUDED