1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qplatformdefs.h"
#include "qcoreapplication.h"
#include "qpair.h"
#include "qsocketnotifier.h"
#include "qthread.h"
#include "qelapsedtimer.h"
#include "qeventdispatcher_unix_p.h"
#include <private/qthread_p.h>
#include <private/qcoreapplication_p.h>
#include <private/qcore_unix_p.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef QT_NO_EVENTFD
# include <sys/eventfd.h>
#endif
// VxWorks doesn't correctly set the _POSIX_... options
#if defined(Q_OS_VXWORKS)
# if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK <= 0)
# undef _POSIX_MONOTONIC_CLOCK
# define _POSIX_MONOTONIC_CLOCK 1
# endif
# include <pipeDrv.h>
# include <sys/time.h>
#endif
#if (_POSIX_MONOTONIC_CLOCK-0 <= 0) || defined(QT_BOOTSTRAPPED)
# include <sys/times.h>
#endif
QT_BEGIN_NAMESPACE
static const char *socketType(QSocketNotifier::Type type)
{
switch (type) {
case QSocketNotifier::Read:
return "Read";
case QSocketNotifier::Write:
return "Write";
case QSocketNotifier::Exception:
return "Exception";
}
Q_UNREACHABLE();
}
QThreadPipe::QThreadPipe()
{
fds[0] = -1;
fds[1] = -1;
#if defined(Q_OS_VXWORKS)
name[0] = '\0';
#endif
}
QThreadPipe::~QThreadPipe()
{
if (fds[0] >= 0)
close(fds[0]);
if (fds[1] >= 0)
close(fds[1]);
#if defined(Q_OS_VXWORKS)
pipeDevDelete(name, true);
#endif
}
#if defined(Q_OS_VXWORKS)
static void initThreadPipeFD(int fd)
{
int ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
if (ret == -1)
perror("QEventDispatcherUNIXPrivate: Unable to init thread pipe");
int flags = fcntl(fd, F_GETFL);
if (flags == -1)
perror("QEventDispatcherUNIXPrivate: Unable to get flags on thread pipe");
ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
if (ret == -1)
perror("QEventDispatcherUNIXPrivate: Unable to set flags on thread pipe");
}
#endif
bool QThreadPipe::init()
{
#if defined(Q_OS_NACL) || defined(Q_OS_WASM)
// do nothing.
#elif defined(Q_OS_VXWORKS)
qsnprintf(name, sizeof(name), "/pipe/qt_%08x", int(taskIdSelf()));
// make sure there is no pipe with this name
pipeDevDelete(name, true);
// create the pipe
if (pipeDevCreate(name, 128 /*maxMsg*/, 1 /*maxLength*/) != OK) {
perror("QThreadPipe: Unable to create thread pipe device %s", name);
return false;
}
if ((fds[0] = open(name, O_RDWR, 0)) < 0) {
perror("QThreadPipe: Unable to open pipe device %s", name);
return false;
}
initThreadPipeFD(fds[0]);
fds[1] = fds[0];
#else
# ifndef QT_NO_EVENTFD
if ((fds[0] = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)) >= 0)
return true;
# endif
if (qt_safe_pipe(fds, O_NONBLOCK) == -1) {
perror("QThreadPipe: Unable to create pipe");
return false;
}
#endif
return true;
}
pollfd QThreadPipe::prepare() const
{
return qt_make_pollfd(fds[0], POLLIN);
}
void QThreadPipe::wakeUp()
{
if (wakeUps.testAndSetAcquire(0, 1)) {
#ifndef QT_NO_EVENTFD
if (fds[1] == -1) {
// eventfd
eventfd_t value = 1;
int ret;
EINTR_LOOP(ret, eventfd_write(fds[0], value));
return;
}
#endif
char c = 0;
qt_safe_write(fds[1], &c, 1);
}
}
int QThreadPipe::check(const pollfd &pfd)
{
Q_ASSERT(pfd.fd == fds[0]);
char c[16];
const int readyread = pfd.revents & POLLIN;
if (readyread) {
// consume the data on the thread pipe so that
// poll doesn't immediately return next time
#if defined(Q_OS_VXWORKS)
::read(fds[0], c, sizeof(c));
::ioctl(fds[0], FIOFLUSH, 0);
#else
# ifndef QT_NO_EVENTFD
if (fds[1] == -1) {
// eventfd
eventfd_t value;
eventfd_read(fds[0], &value);
} else
# endif
{
while (::read(fds[0], c, sizeof(c)) > 0) {}
}
#endif
if (!wakeUps.testAndSetRelease(1, 0)) {
// hopefully, this is dead code
qWarning("QThreadPipe: internal error, wakeUps.testAndSetRelease(1, 0) failed!");
}
}
return readyread;
}
QEventDispatcherUNIXPrivate::QEventDispatcherUNIXPrivate()
{
if (Q_UNLIKELY(threadPipe.init() == false))
qFatal("QEventDispatcherUNIXPrivate(): Cannot continue without a thread pipe");
}
QEventDispatcherUNIXPrivate::~QEventDispatcherUNIXPrivate()
{
// cleanup timers
qDeleteAll(timerList);
}
void QEventDispatcherUNIXPrivate::setSocketNotifierPending(QSocketNotifier *notifier)
{
Q_ASSERT(notifier);
if (pendingNotifiers.contains(notifier))
return;
pendingNotifiers << notifier;
}
int QEventDispatcherUNIXPrivate::activateTimers()
{
return timerList.activateTimers();
}
void QEventDispatcherUNIXPrivate::markPendingSocketNotifiers()
{
for (const pollfd &pfd : qAsConst(pollfds)) {
if (pfd.fd < 0 || pfd.revents == 0)
continue;
auto it = socketNotifiers.find(pfd.fd);
Q_ASSERT(it != socketNotifiers.end());
const QSocketNotifierSetUNIX &sn_set = it.value();
static const struct {
QSocketNotifier::Type type;
short flags;
} notifiers[] = {
{ QSocketNotifier::Read, POLLIN | POLLHUP | POLLERR },
{ QSocketNotifier::Write, POLLOUT | POLLHUP | POLLERR },
{ QSocketNotifier::Exception, POLLPRI | POLLHUP | POLLERR }
};
for (const auto &n : notifiers) {
QSocketNotifier *notifier = sn_set.notifiers[n.type];
if (!notifier)
continue;
if (pfd.revents & POLLNVAL) {
qWarning("QSocketNotifier: Invalid socket %d with type %s, disabling...",
it.key(), socketType(n.type));
notifier->setEnabled(false);
}
if (pfd.revents & n.flags)
setSocketNotifierPending(notifier);
}
}
pollfds.clear();
}
int QEventDispatcherUNIXPrivate::activateSocketNotifiers()
{
markPendingSocketNotifiers();
if (pendingNotifiers.isEmpty())
return 0;
int n_activated = 0;
QEvent event(QEvent::SockAct);
while (!pendingNotifiers.isEmpty()) {
QSocketNotifier *notifier = pendingNotifiers.takeFirst();
QCoreApplication::sendEvent(notifier, &event);
++n_activated;
}
return n_activated;
}
QEventDispatcherUNIX::QEventDispatcherUNIX(QObject *parent)
: QAbstractEventDispatcher(*new QEventDispatcherUNIXPrivate, parent)
{ }
QEventDispatcherUNIX::QEventDispatcherUNIX(QEventDispatcherUNIXPrivate &dd, QObject *parent)
: QAbstractEventDispatcher(dd, parent)
{ }
QEventDispatcherUNIX::~QEventDispatcherUNIX()
{ }
/*!
\internal
*/
void QEventDispatcherUNIX::registerTimer(int timerId, int interval, Qt::TimerType timerType, QObject *obj)
{
#ifndef QT_NO_DEBUG
if (timerId < 1 || interval < 0 || !obj) {
qWarning("QEventDispatcherUNIX::registerTimer: invalid arguments");
return;
} else if (obj->thread() != thread() || thread() != QThread::currentThread()) {
qWarning("QEventDispatcherUNIX::registerTimer: timers cannot be started from another thread");
return;
}
#endif
Q_D(QEventDispatcherUNIX);
d->timerList.registerTimer(timerId, interval, timerType, obj);
}
/*!
\internal
*/
bool QEventDispatcherUNIX::unregisterTimer(int timerId)
{
#ifndef QT_NO_DEBUG
if (timerId < 1) {
qWarning("QEventDispatcherUNIX::unregisterTimer: invalid argument");
return false;
} else if (thread() != QThread::currentThread()) {
qWarning("QEventDispatcherUNIX::unregisterTimer: timers cannot be stopped from another thread");
return false;
}
#endif
Q_D(QEventDispatcherUNIX);
return d->timerList.unregisterTimer(timerId);
}
/*!
\internal
*/
bool QEventDispatcherUNIX::unregisterTimers(QObject *object)
{
#ifndef QT_NO_DEBUG
if (!object) {
qWarning("QEventDispatcherUNIX::unregisterTimers: invalid argument");
return false;
} else if (object->thread() != thread() || thread() != QThread::currentThread()) {
qWarning("QEventDispatcherUNIX::unregisterTimers: timers cannot be stopped from another thread");
return false;
}
#endif
Q_D(QEventDispatcherUNIX);
return d->timerList.unregisterTimers(object);
}
QList<QEventDispatcherUNIX::TimerInfo>
QEventDispatcherUNIX::registeredTimers(QObject *object) const
{
if (!object) {
qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument");
return QList<TimerInfo>();
}
Q_D(const QEventDispatcherUNIX);
return d->timerList.registeredTimers(object);
}
/*****************************************************************************
QEventDispatcher implementations for UNIX
*****************************************************************************/
void QEventDispatcherUNIX::registerSocketNotifier(QSocketNotifier *notifier)
{
Q_ASSERT(notifier);
int sockfd = notifier->socket();
QSocketNotifier::Type type = notifier->type();
#ifndef QT_NO_DEBUG
if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
qWarning("QSocketNotifier: socket notifiers cannot be enabled from another thread");
return;
}
#endif
Q_D(QEventDispatcherUNIX);
QSocketNotifierSetUNIX &sn_set = d->socketNotifiers[sockfd];
if (sn_set.notifiers[type] && sn_set.notifiers[type] != notifier)
qWarning("%s: Multiple socket notifiers for same socket %d and type %s",
Q_FUNC_INFO, sockfd, socketType(type));
sn_set.notifiers[type] = notifier;
}
void QEventDispatcherUNIX::unregisterSocketNotifier(QSocketNotifier *notifier)
{
Q_ASSERT(notifier);
int sockfd = notifier->socket();
QSocketNotifier::Type type = notifier->type();
#ifndef QT_NO_DEBUG
if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
qWarning("QSocketNotifier: socket notifier (fd %d) cannot be disabled from another thread.\n"
"(Notifier's thread is %s(%p), event dispatcher's thread is %s(%p), current thread is %s(%p))",
sockfd,
notifier->thread() ? notifier->thread()->metaObject()->className() : "QThread", notifier->thread(),
thread() ? thread()->metaObject()->className() : "QThread", thread(),
QThread::currentThread() ? QThread::currentThread()->metaObject()->className() : "QThread", QThread::currentThread());
return;
}
#endif
Q_D(QEventDispatcherUNIX);
d->pendingNotifiers.removeOne(notifier);
auto i = d->socketNotifiers.find(sockfd);
if (i == d->socketNotifiers.end())
return;
QSocketNotifierSetUNIX &sn_set = i.value();
if (sn_set.notifiers[type] == nullptr)
return;
if (sn_set.notifiers[type] != notifier) {
qWarning("%s: Multiple socket notifiers for same socket %d and type %s",
Q_FUNC_INFO, sockfd, socketType(type));
return;
}
sn_set.notifiers[type] = nullptr;
if (sn_set.isEmpty())
d->socketNotifiers.erase(i);
}
bool QEventDispatcherUNIX::processEvents(QEventLoop::ProcessEventsFlags flags)
{
Q_D(QEventDispatcherUNIX);
d->interrupt.storeRelaxed(0);
// we are awake, broadcast it
emit awake();
auto threadData = d->threadData.loadRelaxed();
QCoreApplicationPrivate::sendPostedEvents(nullptr, 0, threadData);
const bool include_timers = (flags & QEventLoop::X11ExcludeTimers) == 0;
const bool include_notifiers = (flags & QEventLoop::ExcludeSocketNotifiers) == 0;
const bool wait_for_events = flags & QEventLoop::WaitForMoreEvents;
const bool canWait = (threadData->canWaitLocked()
&& !d->interrupt.loadRelaxed()
&& wait_for_events);
if (canWait)
emit aboutToBlock();
if (d->interrupt.loadRelaxed())
return false;
timespec *tm = nullptr;
timespec wait_tm = { 0, 0 };
if (!canWait || (include_timers && d->timerList.timerWait(wait_tm)))
tm = &wait_tm;
d->pollfds.clear();
d->pollfds.reserve(1 + (include_notifiers ? d->socketNotifiers.size() : 0));
if (include_notifiers)
for (auto it = d->socketNotifiers.cbegin(); it != d->socketNotifiers.cend(); ++it)
d->pollfds.append(qt_make_pollfd(it.key(), it.value().events()));
// This must be last, as it's popped off the end below
d->pollfds.append(d->threadPipe.prepare());
int nevents = 0;
switch (qt_safe_poll(d->pollfds.data(), d->pollfds.size(), tm)) {
case -1:
perror("qt_safe_poll");
break;
case 0:
break;
default:
nevents += d->threadPipe.check(d->pollfds.takeLast());
if (include_notifiers)
nevents += d->activateSocketNotifiers();
break;
}
if (include_timers)
nevents += d->activateTimers();
// return true if we handled events, false otherwise
return (nevents > 0);
}
bool QEventDispatcherUNIX::hasPendingEvents()
{
extern uint qGlobalPostedEventsCount(); // from qapplication.cpp
return qGlobalPostedEventsCount();
}
int QEventDispatcherUNIX::remainingTime(int timerId)
{
#ifndef QT_NO_DEBUG
if (timerId < 1) {
qWarning("QEventDispatcherUNIX::remainingTime: invalid argument");
return -1;
}
#endif
Q_D(QEventDispatcherUNIX);
return d->timerList.timerRemainingTime(timerId);
}
void QEventDispatcherUNIX::wakeUp()
{
Q_D(QEventDispatcherUNIX);
d->threadPipe.wakeUp();
}
void QEventDispatcherUNIX::interrupt()
{
Q_D(QEventDispatcherUNIX);
d->interrupt.storeRelaxed(1);
wakeUp();
}
void QEventDispatcherUNIX::flush()
{ }
QT_END_NAMESPACE
#include "moc_qeventdispatcher_unix_p.cpp"
|