1 #ifndef SimTK_SimTKCOMMON_THREAD_LOCAL_H_ 2 #define SimTK_SimTKCOMMON_THREAD_LOCAL_H_ 35 static pthread_mutex_t
keyLock = PTHREAD_MUTEX_INITIALIZER;
42 T* t =
reinterpret_cast<T*
>(value);
47 pthread_mutex_lock(&keyLock);
48 pthread_key_t key = instanceMap[value];
49 instanceMap.erase(value);
50 if (keyInstances.find(key) != keyInstances.end())
51 keyInstances.find(key)->second.erase(value);
52 pthread_mutex_unlock(&keyLock);
89 ThreadLocal(
const T& defaultValue) : defaultValue(defaultValue) {
96 pthread_key_delete(key);
101 pthread_mutex_lock(&keyLock);
102 std::set<void*>& instances = keyInstances[key];
103 for (std::set<void*>::const_iterator iter = instances.begin(); iter != instances.end(); ++iter) {
104 instanceMap.erase(*iter);
107 keyInstances.erase(key);
108 pthread_mutex_unlock(&keyLock);
114 T* value =
reinterpret_cast<T*
>(pthread_getspecific(key));
116 return createValue();
122 const T&
get()
const {
123 T* value =
reinterpret_cast<T*
>(pthread_getspecific(key));
125 return createValue();
130 pthread_key_create(&key, cleanUpThreadLocalStorage<T>);
131 pthread_mutex_lock(&keyLock);
132 keyInstances[key] = std::set<void*>();
133 pthread_mutex_unlock(&keyLock);
135 T& createValue()
const {
136 T* value =
new T(defaultValue);
137 pthread_setspecific(key, value);
138 pthread_mutex_lock(&keyLock);
139 instanceMap[value] = key;
140 keyInstances[key].insert(value);
141 pthread_mutex_unlock(&keyLock);
150 #endif // SimTK_SimTKCOMMON_THREAD_LOCAL_H_ This class represents a "thread local" variable: one which has a different value on each thread...
Definition: ThreadLocal.h:76
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
T & upd()
Get a reference to the value for the current thread.
Definition: ThreadLocal.h:113
ThreadLocal(const T &defaultValue)
Create a new ThreadLocal variable.
Definition: ThreadLocal.h:89
static std::map< void *, pthread_key_t > instanceMap
Definition: ThreadLocal.h:33
static void cleanUpThreadLocalStorage(void *value)
Definition: ThreadLocal.h:38
~ThreadLocal()
Definition: ThreadLocal.h:92
static pthread_mutex_t keyLock
Definition: ThreadLocal.h:35
static std::map< pthread_key_t, std::set< void * > > keyInstances
Definition: ThreadLocal.h:34
ThreadLocal()
Create a new ThreadLocal variable.
Definition: ThreadLocal.h:81