Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tls.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_TLS_H_)
21 #define __MITSUBA_CORE_TLS_H_
22 
23 #include <mitsuba/mitsuba.h>
24 #include <boost/scoped_ptr.hpp>
25 
27 
28 ///! \cond
29 namespace detail {
30  /* Some forward declarations pertaining to TLS management */
31 
32  extern MTS_EXPORT_CORE void initializeGlobalTLS();
33  extern MTS_EXPORT_CORE void destroyGlobalTLS();
34  extern MTS_EXPORT_CORE void initializeLocalTLS();
35  extern MTS_EXPORT_CORE void destroyLocalTLS();
36 
37  class MTS_EXPORT_CORE ThreadLocalBase {
38  public:
39  /// Functor to allocate memory for a TLS object
40  typedef void *(*ConstructFunctor)();
41  /// Functor to release memory of a TLS object
42  typedef void (*DestructFunctor)(void *);
43  /// Construct a new thread local storage object
44  ThreadLocalBase(const ConstructFunctor &constructFunctor,
45  const DestructFunctor &destructFfunctor);
46  /// Destroy the thread local storage object
47  ~ThreadLocalBase();
48  /// Return the data value associated with the current thread
49  void *get();
50  /// Return the data value associated with the current thread (const version)
51  const void *get() const;
52  /// Like the other \c get(), but also returns whether the TLS object existed before
53  void *get(bool &existed);
54  /// Like the other \c get(), but also returns whether the TLS object existed before (const version)
55  const void *get(bool &existed) const;
56  protected:
57  struct ThreadLocalPrivate;
58  mutable boost::scoped_ptr<ThreadLocalPrivate> d;
59  };
60 }; // namespace tls
61 ///! \endcond
62 
63 /**
64  * \headerfile mitsuba/core/tls.h mitsuba/mitsuba.h
65  * \brief Thin wrapper around boost thread local storage.
66  * Stores references to Object instances.
67  * \sa PrimitiveThreadLocal
68  * \ingroup libcore
69  *
70  * This class implements a reference counting thread local storage object which captures
71  * references to subclasses of \ref Object. In comparison to an API like <tt>boost::thread_specific_ptr</tt>
72  * it has a much nicer cleanup mechanism. Held references are destroyed when the owning thread dies \a or
73  * when the \c ThreadLocal instance is freed, whichever occurs first.
74  */
75 template <typename ValueType> class ThreadLocal {
76 public:
77  /// Construct a new thread local storage object
78  ThreadLocal() : m_base(&ThreadLocal::construct, &ThreadLocal::destruct) { }
79 
80  /// Update the data associated with the current thread
81  inline void set(ValueType *ptr) { ((ref<ValueType> *) m_base.get())->operator=(ptr); }
82 
83  /// Return a reference to the data associated with the current thread
84  inline ValueType *get() { return ((ref<ValueType> *) m_base.get())->get(); }
85 
86  /**
87  * \brief Return a reference to the data associated with the
88  * current thread (const version)
89  */
90  inline const ValueType *get() const { return ((const ref<ValueType> *) m_base.get())->get(); }
91 protected:
92  inline static void *construct() {
93  return new ref<ValueType>();
94  }
95 
96  inline static void destruct(void *data) {
97  delete static_cast<ref<ValueType> *>(data);
98  }
99 protected:
100  detail::ThreadLocalBase m_base;
101 };
102 
103 /**
104  * \headerfile mitsuba/core/tls.h mitsuba/mitsuba.h
105  * \brief Thin wrapper around posix thread local storage.
106  * Stores heap-allocated data other than Object instances.
107  * \sa ThreadLocal
108  * \ingroup libcore
109  *
110  * This class implements a thread local storage object for POD-style data structures.
111  * In comparison to an API like <tt>boost::thread_specific_ptr</tt> it has a much nicer
112  * cleanup mechanism. Held references are destroyed when the owning thread dies \a or
113  * when the \c PrimitiveThreadLocal instance is freed, whichever occurs first.
114  */
115 template <typename ValueType> class PrimitiveThreadLocal {
116 public:
117  /// Construct a new thread local storage object
119  &PrimitiveThreadLocal::destruct) { }
120 
121  /// Update the data associated with the current thread
122  inline void set(ValueType &value) {
123  get() = value;
124  }
125 
126  /// Return a reference to the data associated with the current thread
127  inline ValueType &get() {
128  return *((ValueType *) m_base.get());
129  }
130 
131  /**
132  * \brief Return a reference to the data associated with the
133  * current thread (const version)
134  */
135  inline const ValueType &get() const {
136  return *((const ValueType *) m_base.get());
137  }
138 protected:
139  inline static void *construct() {
140  return new ValueType();
141  }
142 
143  inline static void destruct(void *data) {
144  if (data)
145  delete static_cast<ValueType *>(data);
146  }
147 protected:
148  detail::ThreadLocalBase m_base;
149 };
150 
152 
153 #endif /* __MITSUBA_CORE_TLS_H_ */
void set(ValueType *ptr)
Update the data associated with the current thread.
Definition: tls.h:81
static void destruct(void *data)
Definition: tls.h:143
static void * construct()
Definition: tls.h:92
!
Definition: tls.h:75
detail::ThreadLocalBase m_base
Definition: tls.h:100
#define MTS_EXPORT_CORE
Definition: getopt.h:29
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
ThreadLocal()
Construct a new thread local storage object.
Definition: tls.h:78
detail::ThreadLocalBase m_base
Definition: tls.h:148
static void destruct(void *data)
Definition: tls.h:96
Reference counting helper.
Definition: ref.h:40
Thin wrapper around posix thread local storage. Stores heap-allocated data other than Object instance...
Definition: tls.h:115
PrimitiveThreadLocal()
Construct a new thread local storage object.
Definition: tls.h:118
#define MTS_NAMESPACE_END
Definition: platform.h:138
void set(ValueType &value)
Update the data associated with the current thread.
Definition: tls.h:122
static void * construct()
Definition: tls.h:139