Simbody  3.6
ThreadLocal.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_THREAD_LOCAL_H_
2 #define SimTK_SimTKCOMMON_THREAD_LOCAL_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2008-15 Stanford University and the Authors. *
13  * Authors: Peter Eastman *
14  * Contributors: Michael Sherman *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
27 #include <pthread.h>
28 #include <map>
29 #include <set>
30 
31 namespace SimTK {
32 
62 template <class T>
63 class ThreadLocal {
64 public:
69  initialize();
70  }
71 
79  explicit ThreadLocal(const T& defaultValue) : m_defaultValue(defaultValue) {
80  initialize();
81  }
82 
87  pthread_key_delete(m_key);
88  }
89 
92  T& upd() {
93  T* value = reinterpret_cast<T*>(pthread_getspecific(m_key));
94  if (!value)
95  value = createValue();
96  return *value;
97  }
98 
101  const T& get() const {
102  T* value = reinterpret_cast<T*>(pthread_getspecific(m_key));
103  if (!value)
104  value = createValue();
105  return *value;
106  }
107 
108 private:
109  // This is a destructor for an object of type T that was allocated for this
110  // thread's exclusive use. This will be called at thread termination, from
111  // the same thread that allocated this object via the createValue()
112  // method below.
113  static void cleanUpThreadLocalStorage(void* value) {
114  T* t = reinterpret_cast<T*>(value);
115  delete t;
116  }
117 
118  void initialize() {
119  pthread_key_create(&m_key, cleanUpThreadLocalStorage);
120  }
121 
122  T* createValue() const {
123  T* value = new T(m_defaultValue);
124  pthread_setspecific(m_key, value);
125  return value;
126  }
127 
128  pthread_key_t m_key;
129  T m_defaultValue;
130 };
131 
132 } // namespace SimTK
133 
134 #endif // SimTK_SimTKCOMMON_THREAD_LOCAL_H_
(Deprecated) This class represents a "thread local" variable: one which may have a different value on...
Definition: ThreadLocal.h:63
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 writable reference to the value of type T that was allocated for the current thread&#39;s exclusive...
Definition: ThreadLocal.h:92
ThreadLocal(const T &defaultValue)
Create a new ThreadLocal<T> object and provide a default value of type T to be used to initialize the...
Definition: ThreadLocal.h:79
~ThreadLocal()
Destructor deletes the thread local object but does not delete the individual thread-allocated object...
Definition: ThreadLocal.h:86
ThreadLocal()
Create a new ThreadLocal<T> object.
Definition: ThreadLocal.h:68