Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwineventnotifier.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
7#include "qthread.h"
8#include <QPointer>
9
11
12/*!
13 \class QWinEventNotifier
14 \inmodule QtCore
15 \since 5.0
16 \brief The QWinEventNotifier class provides support for the Windows Wait functions.
17
18 The QWinEventNotifier class makes it possible to use the wait
19 functions on windows in a asynchronous manner. With this class,
20 you can register a HANDLE to an event and get notification when
21 that event becomes signalled. The state of the event is not modified
22 in the process so if it is a manual reset event you will need to
23 reset it after the notification.
24
25 Once you have created a event object using Windows API such as
26 CreateEvent() or OpenEvent(), you can create an event notifier to
27 monitor the event handle. If the event notifier is enabled, it will
28 emit the activated() signal whenever the corresponding event object
29 is signalled.
30
31 The setEnabled() function allows you to disable as well as enable the
32 event notifier. It is generally advisable to explicitly enable or
33 disable the event notifier. A disabled notifier does nothing when the
34 event object is signalled (the same effect as not creating the
35 event notifier). Use the isEnabled() function to determine the
36 notifier's current status.
37
38 Finally, you can use the setHandle() function to register a new event
39 object, and the handle() function to retrieve the event handle.
40
41 \b{Further information:}
42 Although the class is called QWinEventNotifier, it can be used for
43 certain other objects which are so-called synchronization
44 objects, such as Processes, Threads, Waitable timers.
45
46 \warning This class is only available on Windows.
47*/
48
49/*!
50 \fn void QWinEventNotifier::activated(HANDLE hEvent)
51
52 This signal is emitted whenever the event notifier is enabled and
53 the corresponding HANDLE is signalled.
54
55 The state of the event is not modified in the process, so if it is a
56 manual reset event, you will need to reset it after the notification.
57
58 The object is passed in the \a hEvent parameter.
59
60 \sa handle()
61*/
62
63/*!
64 Constructs an event notifier with the given \a parent.
65*/
66
67QWinEventNotifier::QWinEventNotifier(QObject *parent)
68 : QObject(*new QWinEventNotifierPrivate, parent)
69{}
70
71/*!
72 Constructs an event notifier with the given \a parent. It enables
73 the notifier, and watches for the event \a hEvent.
74
75 The notifier is enabled by default, i.e. it emits the activated() signal
76 whenever the corresponding event is signalled. However, it is generally
77 advisable to explicitly enable or disable the event notifier.
78
79 \sa setEnabled(), isEnabled()
80*/
81
82QWinEventNotifier::QWinEventNotifier(HANDLE hEvent, QObject *parent)
83 : QObject(*new QWinEventNotifierPrivate(hEvent, false), parent)
84{
85 setEnabled(true);
86}
87
88/*!
89 Destroys this notifier.
90*/
91
92QWinEventNotifier::~QWinEventNotifier()
93{
94 setEnabled(false);
95}
96
97/*!
98 Register the HANDLE \a hEvent. The old HANDLE will be automatically
99 unregistered.
100
101 \b Note: The notifier will be disabled as a side effect and needs
102 to be re-enabled.
103
104 \sa handle(), setEnabled()
105*/
106
107void QWinEventNotifier::setHandle(HANDLE hEvent)
108{
109 Q_D(QWinEventNotifier);
110 setEnabled(false);
111 d->handleToEvent = hEvent;
112}
113
114/*!
115 Returns the HANDLE that has been registered in the notifier.
116
117 \sa setHandle()
118*/
119
120HANDLE QWinEventNotifier::handle() const
121{
122 Q_D(const QWinEventNotifier);
123 return d->handleToEvent;
124}
125
126/*!
127 Returns \c true if the notifier is enabled; otherwise returns \c false.
128
129 \sa setEnabled()
130*/
131
132bool QWinEventNotifier::isEnabled() const
133{
134 Q_D(const QWinEventNotifier);
135 return d->enabled;
136}
137
138/*!
139 If \a enable is true, the notifier is enabled; otherwise the notifier
140 is disabled.
141
142 \sa isEnabled(), activated()
143*/
144
145void QWinEventNotifier::setEnabled(bool enable)
146{
147 Q_D(QWinEventNotifier);
148 if (d->enabled == enable) // no change
149 return;
150 d->enabled = enable;
151
152 if (Q_UNLIKELY(thread() != QThread::currentThread())) {
153 qWarning("QWinEventNotifier: Event notifiers cannot be enabled or disabled from another thread");
154 return;
155 }
156
157 if (enable) {
158 // It is possible that the notifier was disabled after an event was already
159 // posted. In that case we set a state that indicates that such an obsolete
160 // event shall be ignored.
161 d->winEventActPosted.testAndSetRelaxed(QWinEventNotifierPrivate::Posted,
162 QWinEventNotifierPrivate::IgnorePosted);
163 // The notifier can't be registered, if 'enabled' flag was false.
164 // The code in the else branch ensures that.
165 Q_ASSERT(!d->registered);
166 SetThreadpoolWait(d->waitObject, d->handleToEvent, NULL);
167 d->registered = true;
168 } else if (d->registered) {
169 // Stop waiting for an event. However, there may be a callback queued
170 // already after the call.
171 SetThreadpoolWait(d->waitObject, NULL, NULL);
172 // So, to avoid a race condition after a possible call to
173 // setEnabled(true), wait for a possibly outstanding callback
174 // to complete.
175 WaitForThreadpoolWaitCallbacks(d->waitObject, TRUE);
176 d->registered = false;
177 }
178}
179
180/*!
181 \reimp
182*/
183
184bool QWinEventNotifier::event(QEvent * e)
185{
186 Q_D(QWinEventNotifier);
187
188 switch (e->type()) {
189 case QEvent::ThreadChange:
190 if (d->enabled) {
191 QMetaObject::invokeMethod(this, "setEnabled", Qt::QueuedConnection,
192 Q_ARG(bool, true));
193 setEnabled(false);
194 }
195 break;
196 case QEvent::WinEventAct:
197 // Emit notification, but only if the event has not been invalidated
198 // since by the notifier being disabled, even if it was re-enabled
199 // again.
200 if (d->winEventActPosted.fetchAndStoreRelaxed(QWinEventNotifierPrivate::NotPosted)
201 == QWinEventNotifierPrivate::Posted && d->enabled) {
202 // Clear the flag, as the wait object is implicitly unregistered
203 // when the callback is queued.
204 d->registered = false;
205
206 QPointer<QWinEventNotifier> alive(this);
207 emit activated(d->handleToEvent, QPrivateSignal());
208
209 if (alive && d->enabled && !d->registered) {
210 SetThreadpoolWait(d->waitObject, d->handleToEvent, NULL);
211 d->registered = true;
212 }
213 }
214 return true;
215 default:
216 break;
217 }
218 return QObject::event(e);
219}
220
221QWinEventNotifierPrivate::QWinEventNotifierPrivate(HANDLE h, bool e)
222 : handleToEvent(h), enabled(e), registered(false)
223{
224 waitObject = CreateThreadpoolWait(waitCallback, this, NULL);
225 if (waitObject == NULL)
226 qErrnoWarning("QWinEventNotifier:: CreateThreadpollWait failed.");
227}
228
229QWinEventNotifierPrivate::~QWinEventNotifierPrivate()
230{
231 CloseThreadpoolWait(waitObject);
232}
233
234void QWinEventNotifierPrivate::waitCallback(PTP_CALLBACK_INSTANCE instance, PVOID context,
235 PTP_WAIT wait, TP_WAIT_RESULT waitResult)
236{
237 Q_UNUSED(instance);
238 Q_UNUSED(wait);
239 Q_UNUSED(waitResult);
240 QWinEventNotifierPrivate *nd = reinterpret_cast<QWinEventNotifierPrivate *>(context);
241
242 // Do not post an event, if an event is already in the message queue. Note
243 // that an event that was previously invalidated will be reactivated.
244 if (nd->winEventActPosted.fetchAndStoreRelaxed(QWinEventNotifierPrivate::Posted)
245 == QWinEventNotifierPrivate::NotPosted) {
246 QCoreApplication::postEvent(nd->q_func(), new QEvent(QEvent::WinEventAct));
247 }
248}
249
250QT_END_NAMESPACE
251
252#include "moc_qwineventnotifier.cpp"