KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
mutexevent.h
1/**
2 * MutexEvent
3 *
4 * A very useful object for multi-threaded applications which wish to be
5 * be event driven.
6 *
7 * It is based on the C# AutoResetEvent class which combines a mutex and
8 * a cond_var.
9 *
10 * The event driven thread call wait() with an optional timeout.
11 *
12 * The event generating thread calls set() and the waiting thread wakes up.
13 *
14 * Stephen Punak, May 15 2020
15 */
16#ifndef MUTEXEVENT_H
17#define MUTEXEVENT_H
18
19#include <QWaitCondition>
20#include <QMutex>
21#include "kanoopcommon.h"
22#include "timespan.h"
23
24/**
25 * @brief A mutex/condition-variable pair modelling the C# AutoResetEvent.
26 *
27 * One thread calls wait() and blocks until another thread calls set().
28 * The event auto-resets after each wakeup unless wakeAllWaiters is enabled,
29 * in which case all blocked waiters are woken simultaneously.
30 */
31class KANOOP_EXPORT MutexEvent
32{
33public:
34 /**
35 * @brief Construct a MutexEvent.
36 * @param wakeAllWaiters If true, set() will wake all waiting threads instead of just one
37 */
38 MutexEvent(bool wakeAllWaiters = false) :
39 _isset(false),
40 _isWaiting(false),
41 _debug(false),
42 _wakeAllWaiters(wakeAllWaiters) {}
43
44 /** @brief Virtual destructor. */
45 virtual ~MutexEvent() {}
46
47 /** @brief Signal the event, waking one (or all) waiting thread(s). */
48 void set();
49
50 /** @brief Reset the event to the unsignalled state. */
51 void clear();
52
53 /**
54 * @brief Block the calling thread until the event is set or the timeout elapses.
55 * @param msecs Maximum wait time in milliseconds; 0 means wait indefinitely
56 * @return true if the event was set, false if the timeout elapsed
57 */
58 bool wait(int msecs = 0);
59
60 /**
61 * @brief Block the calling thread until the event is set or the timeout elapses.
62 * @param timeout Maximum wait duration
63 * @return true if the event was set, false if the timeout elapsed
64 */
65 bool wait(const TimeSpan& timeout)
66 {
67 return wait(timeout.totalMilliseconds());
68 }
69
70 /**
71 * @brief Test whether a thread is currently blocked in wait().
72 * @return true if a thread is waiting
73 */
74 bool isWaiting() const { return _isWaiting; }
75
76 /**
77 * @brief Return the arbitrary data payload attached to this event.
78 * @return Stored QVariant data
79 */
80 QVariant data() const { return _data; }
81
82 /**
83 * @brief Attach arbitrary data to this event for use by the woken thread.
84 * @param value Data to store
85 */
86 void setData(const QVariant& value) { _data = value; }
87
88 /** @brief Enable verbose debug output for this event. */
89 void setDebug() { _debug = true; }
90
91 /**
92 * @brief Control whether set() wakes all waiting threads or just one.
93 * @param value If true, all waiters are woken; if false, only one is woken
94 */
95 void setWakeAllWaiters(bool value) { _wakeAllWaiters = value; }
96
97private:
98 QWaitCondition _condition;
99 QMutex _checkMutex;
100 bool _isset;
101 bool _isWaiting;
102 bool _debug;
103 bool _wakeAllWaiters;
104
105 QVariant _data;
106};
107
108#endif // MUTEXEVENT_H
MutexEvent.
Definition mutexevent.h:32
void setDebug()
Enable verbose debug output for this event.
Definition mutexevent.h:89
MutexEvent(bool wakeAllWaiters=false)
Construct a MutexEvent.
Definition mutexevent.h:38
bool wait(int msecs=0)
Block the calling thread until the event is set or the timeout elapses.
void setData(const QVariant &value)
Attach arbitrary data to this event for use by the woken thread.
Definition mutexevent.h:86
void clear()
Reset the event to the unsignalled state.
void set()
Signal the event, waking one (or all) waiting thread(s).
QVariant data() const
Return the arbitrary data payload attached to this event.
Definition mutexevent.h:80
bool wait(const TimeSpan &timeout)
Block the calling thread until the event is set or the timeout elapses.
Definition mutexevent.h:65
bool isWaiting() const
Test whether a thread is currently blocked in wait().
Definition mutexevent.h:74
virtual ~MutexEvent()
Virtual destructor.
Definition mutexevent.h:45
void setWakeAllWaiters(bool value)
Control whether set() wakes all waiting threads or just one.
Definition mutexevent.h:95
TimeSpan.
Definition timespan.h:42
double totalMilliseconds() const
totalMilliseconds