Code_TYMPAN  4.4.0
Industrial site acoustic simulation
threading.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2014> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 
92 class OMutexLocker
93 {
94 public:
96  OMutexLocker(QMutex& mutex);
97  OMutexLocker(const QMutex& mutex);
98 
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:
136 
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 
212  bool _completed;
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
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
Class used as RAII object (Resource Acquisition Is Initialization)
Definition: threading.h:93
QMutex * _mutex
Definition: threading.h:103
~OMutexLocker()
Destructor.
OMutexLocker(QMutex &mutex)
Constructors.
OMutexLocker(const QMutex &mutex)
This class defines a thread for running tasks in a threads collection. Slave thread for the threads c...
Definition: threading.h:132
~OSlaveThread()
Destroy the slave thread; wait for the end of the thread.
bool _bToEnd
Definition: threading.h:140
OSlaveThread(OThreadPool *pool)
Build a slave thread for a threads collection.
OThreadPool * _pool
Pointer on the parent threads collection.
Definition: threading.h:144
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
virtual ~OTask()
Destructor : waits for the end of the task to destroy it.
void reset()
bool _running
Running flag.
Definition: threading.h:209
bool isCompleted() const
bool isRunning() const
bool _completed
Completed flag.
Definition: threading.h:212
virtual void main()=0
OTask()
Default constructor.
bool _canceled
Cancel flag.
Definition: threading.h:215
Slave threads collection.
Definition: threading.h:225
unsigned int _totalCount
Total number of tasks to run.
Definition: threading.h:273
unsigned int getTotalCount() const
void begin(unsigned int count)
OThreadPool(unsigned int slaves)
Build a threads collection and allocate "slaves" thread.
void startPool()
unsigned int _counter
Total number of ended tasks.
Definition: threading.h:276
virtual void push(OTask *task)
std::queue< LPOTask > _tasks
Tasks queue.
Definition: threading.h:270
unsigned int getCount() const
void stop()
Cancel the pending tasks.
virtual ~OThreadPool()
Destructor.