GemaCoreLib
The GeMA Core library
gmThreadManager.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
24 #ifndef _GEMA_THREAD_MANAGER_H_
25 #define _GEMA_THREAD_MANAGER_H_
26 
27 #include "gmCoreConfig.h"
28 #include "gmLog.h"
29 
30 #include <assert.h>
31 
32 #include <QWaitCondition>
33 #include <QMutex>
34 #include <QQueue>
35 
36 class QThread;
37 class GmSimulationData;
38 class GmThread;
39 class GmInternalThread;
40 class LuaEnv;
41 
42 template <class T, bool Align> class GmTLS; // We can not include gmThreadLocalStorage.h without creating a circular reference
43 
50 #ifdef Q_OS_WIN
51 #define GmThreadLocal __declspec(thread)
52 #else
53 #define GmThreadLocal __thread
54 #endif
55 
56 // Global variable for thread id optimized management. Defined in the .cpp file.
58 
61 {
65 };
66 
69 {
70 public:
72  GmThreadTask() : _taskIndex(-1) {}
73 
75  virtual ~GmThreadTask() {}
76 
80  virtual GmThreadTaskResult run(const GmThread* thread) = 0;
81 
83  int taskIndex() const { return _taskIndex; }
84 
86  void setTaskIndex(int index) { _taskIndex = index; }
87 
88 private:
89  int _taskIndex;
90 };
91 
92 
95 {
96 public:
98  ~GmThreadManager();
99 
100  void setWorkerThreadLuaEnv(LuaEnv* mainEnv, const QList<LuaEnv*>& envList);
101 
102  LuaEnv* threadLuaEnv(int tid) const;
103 
104  void addTask(GmThreadTask* task, int tid = 0);
105  GmThreadTaskResult runTasks (int nthreads);
106  void execTasks(int nthreads);
107 
119  const QVector<int>& taskAffinity() const { return _taskAffinity; }
120 
121  int numTasks() const;
122 
124  bool cancelRequested() const { return _cancelRequested; }
125 
127  bool cancelPending() const { return _cancelRequested || _abortRequested; }
128 
129 
144  void setCancelFlag(bool mode = true) { _cancelRequested = mode; }
145 
147  const GmLogCategory& logger() { return _logger; }
148 
153  static int maxWorkerThreads() { assert(_maxNumThreads >= 0); return _maxNumThreads; }
154 
156  static int maxProcThreads() { assert(_maxProcThreads > 0); return _maxProcThreads; }
157 
159  static int numProcCores() { assert(_numProcCores > 0); return _numProcCores; }
160 
166  static int currentId() { return GmThreadManagerThreadId; }
167 
169  static bool inMainThread() { return currentId() == 0; }
170 
171 private:
172  friend class GmThread;
173 
174  int clearTaskQueues();
175  int numTasksInQueues() const;
176 
177 #ifdef ENABLE_TESTS
178  // Allows test function to have access to the ThreadManager logger
179  friend GMC_API_EXPORT void TEST_GmThreadManager(const GmLogCategory& logger, GmLogLevel level);
180 #endif
181 
183 
184  static int _maxNumThreads;
185  static int _maxProcThreads;
186  static int _numProcCores;
187 
191  int _numTasks;
195 
196  int _active;
199 
202 
204 
206 };
207 
208 
218 {
219 public:
220  GmThread(GmThreadManager* manager, int tid);
221  GmThread(GmThreadManager* manager);
222 
223  ~GmThread();
224 
226  int id() const { return _tid; }
227 
229  GmThreadManager* threadManager() const { return _tm; }
230 
232  QThread* thread() const { return _thread; }
233 
235  bool cancelPending() const { return _tm->cancelPending(); }
236 
238  inline LuaEnv* luaEnv() const { return _tm->threadLuaEnv(_tid); }
239 
240 private:
241  friend class GmThreadManager;
242  friend class GmInternalThread;
243 
244  void start();
245  bool wait(unsigned long time = ULONG_MAX);
246  void run();
247 
248  int _tid;
252 };
253 
254 
255 #endif
Our thread wrapper, specialized for running tasks from the thread manager queue.
Definition: gmThreadManager.h:217
bool _abortRequested
Flag for aborting tasks due to another task failure.
Definition: gmThreadManager.h:198
bool cancelRequested() const
Do we have a pending request to cancel the simulation?
Definition: gmThreadManager.h:124
virtual ~GmThreadTask()
Virtual destructor.
Definition: gmThreadManager.h:75
#define GmThreadLocal
Macro used to declare that a global / static variable has a per thread value (Thread Local Storage)
Definition: gmThreadManager.h:53
GmTLS< LuaEnv *, false > * _luaEnv
The per thread Lua environment. Must be a pointer to avoid include order problems with GmTLS.
Definition: gmThreadManager.h:205
GmTLS< GmTaskQueue, false > * _taskQueues
The task queues. Queue 0 is a global queue shared by all threads, while other queues can be used to m...
Definition: gmThreadManager.h:190
const GmLogCategory & logger()
Returns the thread manager logger.
Definition: gmThreadManager.h:147
LuaEnv * luaEnv() const
Returns the Lua environment associated with this thread.
Definition: gmThreadManager.h:238
Declaration of usefull configuration definitions for the Core library.
static int _numProcCores
The number of processor cores (physical processors - no hyper threading)
Definition: gmThreadManager.h:186
GmThreadTaskResult
Possible results for running a task.
Definition: gmThreadManager.h:60
The thread finished its execution without errors.
Definition: gmThreadManager.h:62
static int numProcCores()
Returns the number of processor cores (physical processors - no hyper threading)
Definition: gmThreadManager.h:159
int _active
Flag for controlling if runTasks is active, and how many threads should be used (prevents a thread to...
Definition: gmThreadManager.h:196
int _numTasks
The number of task added to the queues before runTasks()
Definition: gmThreadManager.h:191
A class that works together with GmThreadManager to provide thread local storage.
Definition: gmThreadLocalStorage.h:131
bool cancelPending() const
Do we have a pending request to cancel this thread work or any other thread has aborted?
Definition: gmThreadManager.h:235
void start(QThread::Priority priority)
Auxiliar class used to store the complete set of simulation data.
Definition: gmSimulationData.h:51
GmThreadManager * threadManager() const
Returns a pointer to the thread manager.
Definition: gmThreadManager.h:229
GmLogCategory _logger
Basic logger object for thread manager messages.
Definition: gmThreadManager.h:203
int _maxAffinityId
The biggest tid requested in a call to addTask()
Definition: gmThreadManager.h:194
bool _cancelRequested
Flag for thread cancelation due to an external request / thread manager deletion.
Definition: gmThreadManager.h:197
QVector< int > _taskAffinity
The vector storing the id of the thread used to execute each task.
Definition: gmThreadManager.h:193
QThread * thread() const
Returns the pointer to the associated Qt thread.
Definition: gmThreadManager.h:232
int _npendingTasks
The number of still pending tasks.
Definition: gmThreadManager.h:192
The thread aborted.
Definition: gmThreadManager.h:64
QMutex _taskMutex
The mutex controlling access to thread tasks.
Definition: gmThreadManager.h:200
static int currentId()
Returns the id of the current thread, which MUST be either the main thread or a thread created by the...
Definition: gmThreadManager.h:166
QWaitCondition _idleCond
The condition variable used to wake up a thread waiting for jobs.
Definition: gmThreadManager.h:251
Interface for a task executed by a thread manager thread.
Definition: gmThreadManager.h:68
bool cancelPending() const
Do we have a pending request to cancel the simulation or any other thread has aborted?
Definition: gmThreadManager.h:127
GmThreadLocal int GmThreadManagerThreadId
Local storage for thread ids. More efficient than using QThread::currentThread() for then accessing t...
Definition: gmThreadManager.cpp:50
QQueue< GmThreadTask * > GmTaskQueue
Type for a task queue.
Definition: gmThreadManager.h:182
void setTaskIndex(int index)
Updates the task index inside the task manager for this task.
Definition: gmThreadManager.h:86
GmThread * _mainThread
A pointer to a GmThread wrapper for the main thread.
Definition: gmThreadManager.h:188
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
QThread * _thread
The thread.
Definition: gmThreadManager.h:249
GmLogLevel
Available log levels list.
Definition: gmLog.h:36
bool wait(unsigned long time)
Class representing a category with multiple logging levels.
Definition: gmLog.h:58
static bool inMainThread()
Is the current thread the main thread? Equivalent to comparing the currentId() with 0.
Definition: gmThreadManager.h:169
int id() const
Returns the thread id (a value between 1 and the maximum number of threads)
Definition: gmThreadManager.h:226
int _tid
Our thread id.
Definition: gmThreadManager.h:248
int taskIndex() const
Returns the task index (the order in which the task was added to the thread manager)
Definition: gmThreadManager.h:83
Thread manager used for handling parallel executions.
Definition: gmThreadManager.h:94
GmThreadTask()
Default constructor. Task index MUST be set by a call to setTaskIndex()
Definition: gmThreadManager.h:72
void setCancelFlag(bool mode=true)
Informs the thread manager that the user wants to cancel the simulation. Can be called from the serve...
Definition: gmThreadManager.h:144
GmThreadManager * _tm
The thread manager.
Definition: gmThreadManager.h:250
static int maxProcThreads()
Returns the maximum number of concurrent threads supported by the processor.
Definition: gmThreadManager.h:156
The internal thread class, inheriting from QThread, used for calling the GmThread::run() method.
Definition: gmThreadManager.cpp:53
const QVector< int > & taskAffinity() const
Returns a vector with size equal to the number of tasks executed in the last call to runTasks() stori...
Definition: gmThreadManager.h:119
static int _maxNumThreads
The maximum number of threads that can be used.
Definition: gmThreadManager.h:184
QWaitCondition _managerCond
The condition variable used to block the manager until tasks are finished.
Definition: gmThreadManager.h:201
QList< GmThread * > _threadList
A list with all the allocated threads.
Definition: gmThreadManager.h:189
The thread was externally canceled.
Definition: gmThreadManager.h:63
static int maxWorkerThreads()
Returns the maximum number of allowed working threads.
Definition: gmThreadManager.h:153
static int _maxProcThreads
The maximum number of supported concurrent threads (logical processors)
Definition: gmThreadManager.h:185
Declaration of support functions and macros for information logging.