Code_TYMPAN  4.4.0
Industrial site acoustic simulation
threading.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
16 #ifndef TY_THREADING
17 #define TY_THREADING
18 
19 #include <queue>
20 #include <vector>
21 
22 #include "Tympan/core/smartptr.h"
23 
24 // Useful threading defines
25 #define TY_AUTO_MUTEX_NAME _mutex
26 
27 // Activate QT threading support
28 #ifndef QT_THREAD_SUPPORT
29  #define QT_THREAD_SUPPORT 1
30 #endif // QT_THREAD_SUPPORT
31 
32 #include <QMutex>
33 #include <QWaitCondition>
34 #include <QThread>
35 
40 class OSleeper : public QThread
41 {
42 public:
43  static void sleep(unsigned long secs)
44  {
45  return QThread::sleep(secs);
46  }
47  static void msleep(unsigned long msecs)
48  {
49  return QThread::msleep(msecs);
50  }
51  static void usleep(unsigned long usecs)
52  {
53  return QThread::usleep(usecs);
54  }
55 };
56 
58 #define TY_AUTO_MUTEX mutable QMutex TY_AUTO_MUTEX_NAME;
59 #define TY_LOCK_AUTO_MUTEX TY_AUTO_MUTEX_NAME.lock();
60 #define TY_UNLOCK_AUTO_MUTEX TY_AUTO_MUTEX_NAME.unlock();
61 #define TY_OMUTEXLOCKER_AUTO_MUTEX OMutexLocker locker(TY_AUTO_MUTEX_NAME);
63 #define TY_MUTEX(name) mutable QMutex name;
64 #define TY_LOCK_MUTEX(name) name.lock();
65 #define TY_UNLOCK_MUTEX(name) name.unlock();
66 #define TY_OMUTEXLOCKER_MUTEX(name) OMutexLocker locker(name);
68 #define TY_STATIC_MUTEX(name) static QMutex name;
69 #define TY_STATIC_MUTEX_INSTANCE(name) QMutex name;
71 #define TY_AUTO_SHARED_MUTEX mutable QMutex* TY_AUTO_MUTEX_NAME;
72 #define TY_NEW_AUTO_SHARED_MUTEX TY_AUTO_MUTEX_NAME = new QMutex();
73 #define TY_DELETE_AUTO_SHARED_MUTEX delete TY_AUTO_MUTEX_NAME;
74 #define TY_LOCK_AUTO_SHARED_MUTEX TY_AUTO_MUTEX_NAME->lock();
75 #define TY_UNLOCK_AUTO_SHARED_MUTEX TY_AUTO_MUTEX_NAME->unlock();
76 #define TY_OMUTEXLOCKER_AUTO_SHARED_MUTEX OMutexLocker locker(*TY_AUTO_MUTEX_NAME);
78 #define TY_SHARED_MUTEX(name) mutable QMutex* name;
79 #define TY_NEW_SHARED_MUTEX(name) name = new QMutex();
80 #define TY_DELETE_SHARED_MUTEX(name) delete name;
81 #define TY_LOCK_SHARED_MUTEX(name) name->lock();
82 #define TY_UNLOCK_SHARED_MUTEX(name) name->unlock();
83 #define TY_OMUTEXLOCKER_SHARED_MUTEX(name) OMutexLocker locker(*name);
84 
93 {
94 public:
96  OMutexLocker(QMutex& mutex);
97  OMutexLocker(const QMutex& mutex);
98 
100  ~OMutexLocker();
101 
102 private:
103  QMutex* _mutex;
104 };
105 
106 inline OMutexLocker::OMutexLocker(QMutex& mutex)
107 {
108  _mutex = &mutex;
110 }
111 
112 inline OMutexLocker::OMutexLocker(const QMutex& mutex)
113 {
114  _mutex = const_cast<QMutex*>(&mutex);
116 }
117 
119 {
121 }
122 
123 class OThreadPool;
124 
131 class OSlaveThread : public QThread
132 {
133 public:
135  OSlaveThread(OThreadPool* pool);
136 
138  ~OSlaveThread();
139 
140  bool _bToEnd;
141 
142 protected:
145 
150  void run();
151 };
152 
167 class OTask : public IRefCount, public QWaitCondition, public QMutex
168 {
169 public:
171  OTask();
172 
174  virtual ~OTask();
175 
180  bool isRunning() const;
181 
186  bool isCompleted() const;
187 
192  bool isCanceled() const;
193 
199  virtual void main() = 0;
200 
205  void reset();
206 
207 protected:
209  bool _running;
210 
213 
215  bool _canceled;
216 
217  friend class OSlaveThread;
218  friend class OThreadPool;
219 };
220 
223 
258 class OThreadPool : public std::vector<OSlaveThread*>, public QWaitCondition, public QMutex
259 {
260 public:
262  OThreadPool(unsigned int slaves);
263 
265  virtual ~OThreadPool();
266 
271  virtual void push(OTask* task);
272 
277  unsigned int getTotalCount() const;
278 
283  unsigned int getCount() const;
284 
289  void begin(unsigned int count);
290 
291  void startPool();
292 
297  bool end();
298 
299 protected:
301  void stop();
302 
304  std::queue<LPOTask> _tasks;
305 
307  unsigned int _totalCount;
308 
310  unsigned int _counter;
311 
312  friend class OSlaveThread;
313 };
314 
315 #endif // TY_THREADING
Class used as RAII object (Resource Acquisition Is Initialization)
Definition: threading.h:93
~OMutexLocker()
Destructor.
Definition: threading.h:118
QMutex * _mutex
Definition: threading.h:103
OMutexLocker(QMutex &mutex)
Constructors.
Definition: threading.h:106
This class defines a thread for running tasks in a threads collection. Slave thread for the threads c...
Definition: threading.h:132
OThreadPool * _pool
Pointer on the parent threads collection.
Definition: threading.h:144
~OSlaveThread()
Destroy the slave thread; wait for the end of the thread.
Definition: threading.cpp:23
bool _bToEnd
Definition: threading.h:140
OSlaveThread(OThreadPool *pool)
Build a slave thread for a threads collection.
Definition: threading.cpp:18
void run()
Run a waiting task.
Definition: threading.cpp:28
Access to sleep protected methods of the QThread class.
Definition: threading.h:41
static void usleep(unsigned long usecs)
Definition: threading.h:51
static void msleep(unsigned long msecs)
Definition: threading.h:47
static void sleep(unsigned long secs)
Definition: threading.h:43
Task of a threads collection.
Definition: threading.h:168
bool isCanceled() const
Return true if the task has been cancelled, false otherwise.
Definition: threading.cpp:87
void reset()
Reset the task status (_running=false and _completed=false)
Definition: threading.cpp:93
bool _running
Running flag.
Definition: threading.h:209
bool isCompleted() const
Return true if the task is completed, false otherwise.
Definition: threading.cpp:81
virtual void main()=0
Main routine of the task. This pure virtual method should be overloaded into an inherited class to de...
bool isRunning() const
Return true if the task is running, false otherwise.
Definition: threading.cpp:75
bool _completed
Completed flag.
Definition: threading.h:212
virtual ~OTask()
Destructor : waits for the end of the task to destroy it.
Definition: threading.cpp:67
OTask()
Default constructor.
Definition: threading.cpp:65
bool _canceled
Cancel flag.
Definition: threading.h:215
Slave threads collection.
Definition: threading.h:259
unsigned int _totalCount
Total number of tasks to run.
Definition: threading.h:307
std::queue< LPOTask > _tasks
Tasks queue.
Definition: threading.h:304
unsigned int getTotalCount() const
Return the total number of tasks.
Definition: threading.cpp:151
void begin(unsigned int count)
Begin solver.
Definition: threading.cpp:163
OThreadPool(unsigned int slaves)
Build a threads collection and allocate "slaves" thread.
Definition: threading.cpp:99
void startPool()
Definition: threading.cpp:171
virtual ~OThreadPool()
Destructor.
Definition: threading.cpp:109
bool end()
End solver.
Definition: threading.cpp:181
unsigned int _counter
Total number of ended tasks.
Definition: threading.h:310
unsigned int getCount() const
Return the counter.
Definition: threading.cpp:157
void stop()
Cancel the pending tasks.
Definition: threading.cpp:196
virtual void push(OTask *task)
Add a task to the queue.
Definition: threading.cpp:140
SmartPtr< OTask > LPOTask
Smart Pointer sur OTask.
Definition: threading.h:222
#define TY_LOCK_SHARED_MUTEX(name)
Definition: threading.h:81
#define TY_UNLOCK_SHARED_MUTEX(name)
Definition: threading.h:82