Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
thread.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_THREAD_H_)
21 #define __MITSUBA_CORE_THREAD_H_
22 
23 #include <mitsuba/mitsuba.h>
24 #include <boost/scoped_ptr.hpp>
25 
27 
28 /**
29  * \headerfile mitsuba/core/thread.h mitsuba/mitsuba.h
30  * \brief Cross-platform thread implementation
31  * \ingroup libcore
32  * \ingroup libpython
33  */
34 class MTS_EXPORT_CORE Thread : public Object {
35 public:
36  /// Possible priority values for \ref Thread::setPriority()
38  EIdlePriority = 0,
44  ERealtimePriority
45  };
46 
47  /**
48  * \brief Create a new thread object
49  * \param name An identifying name of this thread
50  * (will be shown in debug messages)
51  * \remark Note that it is currently not possible to
52  * construct Thread instances from Python
53  */
54  Thread(const std::string &name);
55 
56  /**
57  * \brief Set the thread priority
58  *
59  * This does not always work -- for instance, Linux
60  * requires root privileges for this operation.
61  *
62  * \return \c true upon success.
63  */
64  bool setPriority(EThreadPriority priority);
65 
66  /// Return the thread priority
67  EThreadPriority getPriority() const;
68 
69  /**
70  * \brief Set the core affinity
71  *
72  * This function provides a hint to the operating system
73  * scheduler that the thread should preferably run
74  * on the specified processor core. By default, the parameter
75  * is set to -1, which means that there is no affinity.
76  */
77  void setCoreAffinity(int core);
78 
79  /// Return the core affinity
80  int getCoreAffinity() const;
81 
82  /**
83  * \brief Specify whether or not this thread is critical
84  *
85  * When an thread marked critical crashes from an uncaught
86  * exception, the whole process is brought down.
87  * The default is \c false.
88  */
89  void setCritical(bool critical);
90 
91  /// Return the value of the critical flag
92  bool getCritical() const;
93 
94  /// Return the thread ID
95  static int getID();
96 
97  /// Return the name of this thread
98  const std::string &getName() const;
99 
100  /// Set the name of this thread
101  void setName(const std::string &name);
102 
103  /// Return the parent thread
104  Thread *getParent();
105 
106  /// Return the parent thread (const version)
107  const Thread *getParent() const;
108 
109  /// Set the logger instance used to process log messages from this thread
110  void setLogger(Logger *logger);
111 
112  /// Return the thread's logger instance
113  Logger *getLogger();
114 
115  /// Set the thread's file resolver
116  void setFileResolver(FileResolver *fresolver);
117 
118  /// Return the thread's file resolver
119  FileResolver *getFileResolver();
120 
121  /// Return the current thread
122  static Thread *getThread();
123 
124  /// Is this thread still running?
125  bool isRunning() const;
126 
127  /// Start the thread
128  void start();
129 
130  /**
131  * \brief Detach the thread and release resources
132  *
133  * After a call to this function, \ref join()
134  * cannot be used anymore. This releases resources, which
135  * would otherwise be held until a call to \ref join().
136  */
137  void detach();
138 
139  /// Wait until the thread finishes
140  void join();
141 
142  /// Return a string representation
143  virtual std::string toString() const;
144 
145  /// Sleep for a certain amount of time
146  static void sleep(unsigned int ms);
147 
148  /// Initialize the threading system
149  static void staticInitialization();
150 
151  /// Shut down the threading system
152  static void staticShutdown();
153 
154  /// Initialize Mitsuba's threading system for simultaneous use of OpenMP
155  static void initializeOpenMP(size_t threadCount);
156 
157  /**
158  * \brief Register an unmanaged thread with Mitsuba (i.e. one that
159  * doesn't derive from \c mitsuba::Thread)
160  *
161  * Should be called from the thread in question. The function returns
162  * a Mitsuba handle to the thread
163  */
164  static Thread *registerUnmanagedThread(const std::string &name);
165 
166  /**
167  * \brief Register a thread crash handler
168  *
169  * A crash handler is called whenever a thread fails with an uncaught
170  * exception. This can be used to implement more useful error messages
171  * in certain circumstances
172  */
173  static void registerCrashHandler(bool (*handler)(void));
174 
176 protected:
177  /// Virtual destructor
178  virtual ~Thread();
179 
180  /// Thread dispatch function
181  static void dispatch(Thread *thread);
182 
183  /**
184  * Exit the thread, should be called from
185  * inside the thread
186  */
187  void exit();
188 
189  /// Yield to another processor
190  void yield();
191 
192  /// The thread's run method
193  virtual void run() = 0;
194 private:
195  struct ThreadPrivate;
196  boost::scoped_ptr<ThreadPrivate> d;
197 };
198 
199 #if defined(MTS_OPENMP)
200 #if defined(__OSX__)
201 /// Variant of \c omp_get_max_threads that works on OSX
203 
204 /// Variant of \c omp_get_thread_num that works on OSX
206 #else
207 #define mts_omp_get_max_threads omp_get_max_threads
208 #define mts_omp_get_thread_num omp_get_thread_num
209 #endif
210 #else
211 #define mts_omp_get_max_threads() 1
212 #define mts_omp_get_thread_num() 0
213 #endif
214 
216 
217 #endif /* __MITSUBA_CORE_THREAD_H_ */
Definition: thread.h:42
static void staticInitialization()
Initializes the built-in reference count debugger (if enabled)
#define MTS_EXPORT_CORE
Definition: getopt.h:29
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
Definition: thread.h:39
Definition: thread.h:43
File resolution helper.
Definition: fresolver.h:41
#define mts_omp_get_max_threads()
Definition: thread.h:211
EThreadPriority
Possible priority values for Thread::setPriority()
Definition: thread.h:37
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Definition: thread.h:41
Cross-platform thread implementation.
Definition: thread.h:34
#define mts_omp_get_thread_num()
Definition: thread.h:212
Responsible for processing log messages.
Definition: logger.h:116
Parent of all Mitsuba classes.
Definition: object.h:38
virtual std::string toString() const
Return a human-readable string representation of the object&#39;s contents.
Definition: thread.h:40
static void staticShutdown()
Free the memory taken by staticInitialization()
#define MTS_NAMESPACE_END
Definition: platform.h:138