Files
TCPlayerbotCore/dep/include/zthread/ThreadLocal.h
T

384 lines
11 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/*
* Copyright (c) 2005, Eric Crahen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __ZTTHREADLOCAL_H__
#define __ZTTHREADLOCAL_H__
#include "zthread/ThreadLocalImpl.h"
namespace ZThread {
/**
* @class ThreadLocal
*
* @author Eric Crahen <http://www.code-foo.com>
* @date <2003-07-27T11:18:21-0400>
* @version 2.3.0
*
2009-02-09 08:16:34 -05:00
* Provides access to store and retrieve value types to and from a thread local
2008-10-02 16:23:55 -05:00
* storage context. A thread local storage context consists of the calling thread
* a specific ThreadLocal object. Since this context is specific to each thread
2009-02-09 08:16:34 -05:00
* whenever a value is stored in a ThreadLocal that is accessible from multiple
* threads, it can only be retrieved by the thread that stored it.
2008-10-02 16:23:55 -05:00
*
* The first time a thread accesses the value associated with a thread local storage
2009-02-09 08:16:34 -05:00
* context, a value is created. That value is either an initial value (determined by
2008-10-02 16:23:55 -05:00
* InitialValueT) or an inherited value (determined by ChildValueT).
*
* - If a threads parent had no value associated with a ThreadLocal when the thread was created,
* then the InitialValueT functor is used to create an initial value.
*
2009-02-09 08:16:34 -05:00
* - If a threads parent did have a value associated with a ThreadLocal when the thread was
2008-10-02 16:23:55 -05:00
* created, then the childValueT functor is used to create an initial value.
*
* Not all ThreadLocal's support the inheritance of values from parent threads. The default
* behavoir is to create values through the InitialValueT functor for all thread when
2009-02-09 08:16:34 -05:00
* they first access a thread local storage context.
*
2008-10-02 16:23:55 -05:00
* - Inheritance is enabled automatically when a user supplies a ChildValueT functor other
* than the default one supplied.
*
2009-02-09 08:16:34 -05:00
* - Inheritance can be controlled explicitly by the user through a third functor,
2008-10-02 16:23:55 -05:00
* InheritableValueT.
*
* <h2>Examples</h2>
*
* - <a href="#ex1">Default initial value</a>
* - <a href="#ex2">User-specified initial value</a>
* - <a href="#ex3">User-specified inherited value</a>
*
* <h2><a name="ex1">Default initial value</a></h2>
* A ThreadLocal that does not inherit, and uses the default value
2009-02-09 08:16:34 -05:00
* for an int as its initial value.
2008-10-02 16:23:55 -05:00
*
2009-02-09 08:16:34 -05:00
* @code
2008-10-02 16:23:55 -05:00
*
* #include "zthread/ThreadLocal.h"
* #include "zthread/Thread.h"
* #include <iostream>
*
* using namespace ZThread;
*
* class aRunnable : public Runnable {
* ThreadLocal<int> localValue;
* public:
* void run() {
2009-02-09 08:16:34 -05:00
* std::cout << localValue.get() << std::endl;
2008-10-02 16:23:55 -05:00
* }
* };
*
* int main() {
*
* // Create a shared task to display ThreadLocal values
* Task task(new aRunnable);
*
* Thread t0(task); t0.wait();
* Thread t1(task); t1.wait();
* Thread t2(task); t2.wait();
*
* // Output:
*
* // 0
* // 0
* // 0
*
* return 0;
*
* }
*
* @endcode
*
* <h2><a name="ex2">User-specified initial value</a></h2>
* A ThreadLocal that does not inherit, and uses a custom initial value.
*
2009-02-09 08:16:34 -05:00
* @code
2008-10-02 16:23:55 -05:00
*
* #include "zthread/ThreadLocal.h"
* #include "zthread/Thread.h"
* #include <iostream>
*
* using namespace ZThread;
*
* struct anInitialValueFn {
2009-02-09 08:16:34 -05:00
* int operator()() {
2008-10-02 16:23:55 -05:00
* static int next = 100;
* int val = next; next += 100;
2009-02-09 08:16:34 -05:00
* return val;
2008-10-02 16:23:55 -05:00
* }
* };
*
* class aRunnable : public Runnable {
* ThreadLocal<int, anInitialValueFn> localValue;
* public:
* void run() {
2009-02-09 08:16:34 -05:00
* std::cout << localValue.get() << std::endl;
2008-10-02 16:23:55 -05:00
* }
* };
*
* int main() {
*
* // Create a shared task to display ThreadLocal values
* Task task(new aRunnable);
*
* Thread t0(task); t0.wait();
* Thread t1(task); t1.wait();
* Thread t2(task); t2.wait();
*
* // Output:
*
* // 100
* // 200
* // 300
*
* return 0;
*
* }
*
* @endcode
*
* <h2><a name="ex3">User-specified inherited value</a></h2>
2009-02-09 08:16:34 -05:00
* A ThreadLocal that does inherit and modify child values.
2008-10-02 16:23:55 -05:00
* (The default initial value functor is used)
*
2009-02-09 08:16:34 -05:00
* @code
2008-10-02 16:23:55 -05:00
*
* #include "zthread/ThreadLocal.h"
* #include "zthread/Thread.h"
* #include <iostream>
*
* using namespace ZThread;
*
* struct anInheritedValueFn {
2009-02-09 08:16:34 -05:00
* int operator()(int val) {
* return val + 100;
2008-10-02 16:23:55 -05:00
* }
* };
*
2009-02-09 08:16:34 -05:00
* // This Runnable associates no ThreadLocal value in the main thread; so
2008-10-02 16:23:55 -05:00
* // none of the child threads have anything to inherit.
* class aRunnable : public Runnable {
* ThreadLocal<int, ThreadLocalImpl::InitialValueFn<int>, anInheritedValueFn> localValue;
* public:
* void run() {
2009-02-09 08:16:34 -05:00
* std::cout << localValue.get() << std::endl;
2008-10-02 16:23:55 -05:00
* }
* };
*
* // This Runnable associates a ThreadLocal value in the main thread which
* // is inherited when the child threads are created.
* class anotherRunnable : public Runnable {
* ThreadLocal<int, ThreadLocalImpl::InitialValueFn<int>, anInheritedValueFn> localValue;
* public:
2009-02-09 08:16:34 -05:00
* anotherRunnable() {
2008-10-02 16:23:55 -05:00
* localValue.set(100);
* }
* void run() {
2009-02-09 08:16:34 -05:00
* std::cout << localValue.get() << std::endl;
2008-10-02 16:23:55 -05:00
* }
* };
*
* int main() {
*
* // Create a shared task to display ThreadLocal values
* Task task(new aRunnable);
*
* Thread t0(task); t0.wait();
* Thread t1(task); t1.wait();
* Thread t2(task); t2.wait();
*
* // Output:
*
* // 0
* // 0
* // 0
*
* task = Task(new anotherRunnable);
*
* Thread t10(task); t10.wait();
* Thread t11(task); t11.wait();
* Thread t12(task); t12.wait();
*
* // Output:
*
* // 200
* // 200
* // 200
*
* return 0;
*
* }
*
* @endcode
*
* <h2>Parameters</h2>
*
* <em>InitialValueT</em>
*
* This template parameter should indicate the functor used to set
* the initial value. It should support the following operator:
*
* <code>
* // required operator
2009-02-09 08:16:34 -05:00
* T operator()
2008-10-02 16:23:55 -05:00
*
* // supported expression
* InitialValueT()()
* </code>
*
*
* <em>ChildValueT</em>
*
* This template parameter should indicate the functor used to set
* the value that will be inherited by thread whose parent have associated
2009-02-09 08:16:34 -05:00
* a value with the ThreadLocal's context at the time they are created.
2008-10-02 16:23:55 -05:00
* It should support the following operator:
*
* <code>
* // required operator
2009-02-09 08:16:34 -05:00
* T operator(const T& parentValue)
2008-10-02 16:23:55 -05:00
*
* // supported expression
* ChildValueT()(parentValue)
* </code>
*
*
* <em>InheritableValueT</em>
*
* This template parameter should indicate the functor, used to determine
* wheather or not this ThreadLocal will allow values from a parent threads
* context to be inherited by child threads when they are created.
* It should support the following operator:
*
* <code>
* // required operator
2009-02-09 08:16:34 -05:00
* bool operator(const T& childValueFunctor)
2008-10-02 16:23:55 -05:00
*
* // supported expression
* InheritableValueT()( ChildValueT() )
* </code>
*
*/
template <
2009-02-09 08:16:34 -05:00
typename T,
2008-10-02 16:23:55 -05:00
typename InitialValueT = ThreadLocalImpl::InitialValueFn<T>,
2009-02-09 08:16:34 -05:00
typename ChildValueT = ThreadLocalImpl::UniqueChildValueFn,
2008-10-02 16:23:55 -05:00
typename InheritableValueT = ThreadLocalImpl::InheritableValueFn
>
class ThreadLocal : private ThreadLocalImpl {
typedef ThreadLocalImpl::ValuePtr ValuePtr;
class Value : public ThreadLocalImpl::Value {
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
T value;
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
public:
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
Value() : value( InitialValueT()() ) { }
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
Value(const Value& v) : value( ChildValueT()(v.value) ) { }
2009-02-09 08:16:34 -05:00
virtual ~Value() { }
2008-10-02 16:23:55 -05:00
operator T() { return value; }
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
const Value& operator=(const T& v) { value = v; }
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
virtual bool isInheritable() const {
return InheritableValueT()( ChildValueT() );
}
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
virtual ValuePtr clone() const {
return ValuePtr( new Value(*this) );
}
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
};
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
static ValuePtr createValue() {
return ValuePtr( new Value );
}
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
public:
/**
2009-02-09 08:16:34 -05:00
* Get the value associated with the context (this ThreadLocal and
2008-10-02 16:23:55 -05:00
* the calling thread) of the invoker. If no value is currently
* associated, then an intial value is created and associated; that value
* is returned.
*
* @return <em>T</em> associated value.
*
* @post If no value has been associated with the invoking context
* then an inital value will be associated. That value is
2009-02-09 08:16:34 -05:00
* created by the <em>InitialValueT</em> functor.
2008-10-02 16:23:55 -05:00
*/
2009-02-09 08:16:34 -05:00
T get() const {
2008-10-02 16:23:55 -05:00
return (T)reinterpret_cast<Value&>( *value(&createValue) );
}
2009-02-09 08:16:34 -05:00
2008-10-02 16:23:55 -05:00
/**
2009-02-09 08:16:34 -05:00
* Replace the value associated with the context (this ThreadLocal and
2008-10-02 16:23:55 -05:00
* the calling thread) of the invoker. If no value is currently
* associated, then an intial value is first created and subsequently
* replaced by the new value.
*
* @param v value of type <em>T</em> to associate.
*
* @post If no value has been associated with the invoking context
* then an inital value will first be associated. That value is
2009-02-09 08:16:34 -05:00
* created by the <em>InitialValueT</em> functor and then
* replaced with the new value.
2008-10-02 16:23:55 -05:00
*/
void set(T v) const {
reinterpret_cast<Value&>( *value(&createValue) ) = v;
}
/**
2009-02-09 08:16:34 -05:00
* Remove any value current associated with this ThreadLocal.
*
* @post Upon thier next invocation the get() and set() functions will behave as
* if no value has been associated with this ThreadLocal and an
2008-10-02 16:23:55 -05:00
* initial value will be generated.
*/
void clear() const {
ThreadLocalImpl::clear();
}
/**
2009-02-09 08:16:34 -05:00
* Remove any value current associated with <em>any</em> ThreadLocal.
*
* @post Upon thier next invocation the get() and set() functions will behave as
* if no value has been associated with <em>any</em> ThreadLocal and new
2008-10-02 16:23:55 -05:00
* initial values will be generated.
*/
static void clearAll() {
ThreadLocalImpl::clearAll();
}
};
} // namespace ZThread
#endif // __ZTTHREADLOCAL_H__
2009-02-17 20:07:49 -06:00