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_9613
17 #define TY_THREADING_9613
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 
224 class OThreadPool : public std::vector<OSlaveThread*>, public QWaitCondition, public QMutex
225 {
226 public:
228  OThreadPool(unsigned int slaves);
229 
231  virtual ~OThreadPool();
232 
237  virtual void push(OTask* task);
238 
243  unsigned int getTotalCount() const;
244 
249  unsigned int getCount() const;
250 
255  void begin(unsigned int count);
256 
257  void startPool();
258 
263  bool end();
264 
265 protected:
267  void stop();
268 
270  std::queue<LPOTask> _tasks;
271 
273  unsigned int _totalCount;
274 
276  unsigned int _counter;
277 
278  friend class OSlaveThread;
279 };
280 
281 #endif // TY_THREADING_9613
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.
Definition: threading.h:118
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
~OSlaveThread()
Destroy the slave thread; wait for the end of the thread.
Definition: threading.cpp:25
bool _bToEnd
Definition: threading.h:140
OSlaveThread(OThreadPool *pool)
Build a slave thread for a threads collection.
Definition: threading.cpp:20
void run()
Run a waiting task.
Definition: threading.cpp:30
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
Return true if the task has been cancelled, false otherwise.
Definition: threading.cpp:89
void reset()
Reset the task status (_running=false and _completed=false)
Definition: threading.cpp:95
bool _running
Running flag.
Definition: threading.h:209
bool isCompleted() const
Return true if the task is completed, false otherwise.
Definition: threading.cpp:83
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:77
bool _completed
Completed flag.
Definition: threading.h:212
virtual ~OTask()
Destructor : waits for the end of the task to destroy it.
Definition: threading.cpp:69
OTask()
Default constructor.
Definition: threading.cpp:67
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
Return the total number of tasks.
Definition: threading.cpp:153
void begin(unsigned int count)
Begin solver.
Definition: threading.cpp:165
OThreadPool(unsigned int slaves)
Build a threads collection and allocate "slaves" thread.
Definition: threading.cpp:101
void startPool()
Definition: threading.cpp:173
virtual ~OThreadPool()
Destructor.
Definition: threading.cpp:111
bool end()
End solver.
Definition: threading.cpp:183
unsigned int _counter
Total number of ended tasks.
Definition: threading.h:276
std::queue< LPOTask > _tasks
Tasks queue.
Definition: threading.h:270
unsigned int getCount() const
Return the counter.
Definition: threading.cpp:159
void stop()
Cancel the pending tasks.
Definition: threading.cpp:198
virtual void push(OTask *task)
Add a task to the queue.
Definition: threading.cpp:142