50 for (qsizetype i = 0; !pending && i < src->pollfds.size(); ++i) {
51 GPollFDWithQSocketNotifier *p = src->pollfds.at(i);
53 if (p->pollfd.revents & G_IO_NVAL) {
55 const char *
const t[] = {
"Read",
"Write",
"Exception" };
56 qWarning(
"QSocketNotifier: Invalid socket %d and type '%s', disabling...",
57 p->pollfd.fd, t[p->socketNotifier->type()]);
59 p->socketNotifier->setEnabled(
false);
62 pending = pending || ((p->pollfd.revents & p->pollfd.events) != 0);
71 QEvent event(QEvent::SockAct);
74 for (src->activeNotifierPos = 0; src->activeNotifierPos < src->pollfds.size();
75 ++src->activeNotifierPos) {
76 GPollFDWithQSocketNotifier *p = src->pollfds.at(src->activeNotifierPos);
78 if ((p->pollfd.revents & p->pollfd.events) != 0)
79 QCoreApplication::sendEvent(p->socketNotifier, &event);
85Q_CONSTINIT
static GSourceFuncs socketNotifierSourceFuncs = {
86 socketNotifierSourcePrepare,
87 socketNotifierSourceCheck,
88 socketNotifierSourceDispatch,
265QEventDispatcherGlibPrivate::QEventDispatcherGlibPrivate(GMainContext *context)
266 : mainContext(context)
268#if GLIB_MAJOR_VERSION == 2
&& GLIB_MINOR_VERSION < 32
269 if (qEnvironmentVariableIsEmpty(
"QT_NO_THREADED_GLIB")) {
270 Q_CONSTINIT
static QBasicMutex mutex;
271 QMutexLocker locker(&mutex);
272 if (!g_thread_supported())
278 g_main_context_ref(mainContext);
280 QCoreApplication *app = QCoreApplication::instance();
281 if (app && QThread::isMainThread()) {
282 mainContext = g_main_context_default();
283 g_main_context_ref(mainContext);
285 mainContext = g_main_context_new();
289#if GLIB_CHECK_VERSION (2
, 22
, 0
)
290 g_main_context_push_thread_default (mainContext);
294 GSource *source = g_source_new(&postEventSourceFuncs,
sizeof(GPostEventSource));
295 postEventSource =
reinterpret_cast<GPostEventSource *>(source);
298 q20::construct_at(&postEventSource->serialNumber, 1);
299 postEventSource->d =
this;
300 attachSource(source,
"[Qt] GPostEventSource");
303 source = g_source_new(&socketNotifierSourceFuncs,
sizeof(GSocketNotifierSource));
304 socketNotifierSource =
reinterpret_cast<GSocketNotifierSource *>(source);
305 (
void)
new (&socketNotifierSource->pollfds) QList<GPollFDWithQSocketNotifier *>();
306 attachSource(source,
"[Qt] GSocketNotifierSource");
309 source = g_source_new(&timerSourceFuncs,
sizeof(GTimerSource));
310 timerSource =
reinterpret_cast<GTimerSource *>(source);
311 (
void)
new (&timerSource->timerList) QTimerInfoList();
312 timerSource->processEventsFlags = QEventLoop::AllEvents;
313 timerSource->runWithIdlePriority =
false;
314 attachSource(source,
"[Qt] GTimerSource");
316 source = g_source_new(&idleTimerSourceFuncs,
sizeof(GIdleTimerSource));
317 idleTimerSource =
reinterpret_cast<GIdleTimerSource *>(source);
318 idleTimerSource->timerSource = timerSource;
319 attachSource(source,
"[Qt] GIdleTimerSource");
325void QEventDispatcherGlibPrivate::attachSource(GSource *source,
const char *name)
328 Q_ASSERT(g_source_get_priority(source) == G_PRIORITY_DEFAULT);
329 g_source_set_name(source, name);
330 g_source_set_can_recurse(source,
true);
331 g_source_attach(source, mainContext);
354QEventDispatcherGlib::~QEventDispatcherGlib()
356 Q_D(QEventDispatcherGlib);
359 d->timerSource->timerList.clearTimers();
360 d->timerSource->timerList.~QTimerInfoList();
361 d->destroySource(&d->timerSource->source);
362 d->timerSource =
nullptr;
363 d->destroySource(&d->idleTimerSource->source);
364 d->idleTimerSource =
nullptr;
367 for (qsizetype i = 0; i < d->socketNotifierSource->pollfds.size(); ++i) {
368 GPollFDWithQSocketNotifier *p = d->socketNotifierSource->pollfds.at(i);
369 g_source_remove_poll(&d->socketNotifierSource->source, &p->pollfd);
372 d->socketNotifierSource->pollfds.~QList<GPollFDWithQSocketNotifier *>();
373 d->destroySource(&d->socketNotifierSource->source);
374 d->socketNotifierSource =
nullptr;
377 d->destroySource(&d->postEventSource->source);
378 d->postEventSource =
nullptr;
380 Q_ASSERT(d->mainContext !=
nullptr);
381#if GLIB_CHECK_VERSION (2
, 22
, 0
)
382 g_main_context_pop_thread_default (d->mainContext);
384 g_main_context_unref(d->mainContext);
385 d->mainContext =
nullptr;
388bool QEventDispatcherGlib::processEvents(QEventLoop::ProcessEventsFlags flags)
390 Q_D(QEventDispatcherGlib);
392 const bool canWait = flags.testAnyFlag(QEventLoop::WaitForMoreEvents);
399 QEventLoop::ProcessEventsFlags savedFlags = d->timerSource->processEventsFlags;
400 d->timerSource->processEventsFlags = flags;
402 if (!(flags & QEventLoop::EventLoopExec)) {
404 d->timerSource->runWithIdlePriority =
false;
407 bool result = g_main_context_iteration(d->mainContext, canWait);
408 while (!result && canWait)
409 result = g_main_context_iteration(d->mainContext, canWait);
411 d->timerSource->processEventsFlags = savedFlags;
419void QEventDispatcherGlib::registerSocketNotifier(QSocketNotifier *notifier)
422 int sockfd =
int(notifier->socket());
423 const auto type = notifier->type();
426 qWarning(
"QSocketNotifier: Internal error");
428 }
else if (notifier->thread() != thread()
429 || thread() != QThread::currentThread()) {
430 qWarning(
"QSocketNotifier: socket notifiers cannot be enabled from another thread");
435 Q_D(QEventDispatcherGlib);
438 GPollFDWithQSocketNotifier *p =
new GPollFDWithQSocketNotifier;
439 p->pollfd.fd = sockfd;
441 case QSocketNotifier::Read:
442 p->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
444 case QSocketNotifier::Write:
445 p->pollfd.events = G_IO_OUT | G_IO_HUP | G_IO_ERR;
447 case QSocketNotifier::Exception:
448 p->pollfd.events = G_IO_PRI | G_IO_HUP | G_IO_ERR;
451 p->socketNotifier = notifier;
453 d->socketNotifierSource->pollfds.append(p);
455 g_source_add_poll(&d->socketNotifierSource->source, &p->pollfd);
458void QEventDispatcherGlib::unregisterSocketNotifier(QSocketNotifier *notifier)
462 if (notifier->socket() < 0) {
463 qWarning(
"QSocketNotifier: Internal error");
465 }
else if (notifier->thread() != thread()
466 || thread() != QThread::currentThread()) {
467 qWarning(
"QSocketNotifier: socket notifiers cannot be disabled from another thread");
472 Q_D(QEventDispatcherGlib);
474 for (qsizetype i = 0; i < d->socketNotifierSource->pollfds.size(); ++i) {
475 GPollFDWithQSocketNotifier *p = d->socketNotifierSource->pollfds.at(i);
476 if (p->socketNotifier == notifier) {
478 g_source_remove_poll(&d->socketNotifierSource->source, &p->pollfd);
480 d->socketNotifierSource->pollfds.removeAt(i);
484 if (i <= d->socketNotifierSource->activeNotifierPos)
485 --d->socketNotifierSource->activeNotifierPos;
492void QEventDispatcherGlib::registerTimer(Qt::TimerId timerId, Duration interval,
493 Qt::TimerType timerType, QObject *object)
496 if (qToUnderlying(timerId) < 1 || interval < 0ns || !object) {
497 qWarning(
"QEventDispatcherGlib::registerTimer: invalid arguments");
499 }
else if (object->thread() != thread() || thread() != QThread::currentThread()) {
500 qWarning(
"QEventDispatcherGlib::registerTimer: timers cannot be started from another thread");
505 Q_D(QEventDispatcherGlib);
506 d->timerSource->timerList.registerTimer(timerId, interval, timerType, object);
509bool QEventDispatcherGlib::unregisterTimer(Qt::TimerId timerId)
512 if (qToUnderlying(timerId) < 1) {
513 qWarning(
"QEventDispatcherGlib::unregisterTimer: invalid argument");
515 }
else if (thread() != QThread::currentThread()) {
516 qWarning(
"QEventDispatcherGlib::unregisterTimer: timers cannot be stopped from another thread");
521 Q_D(QEventDispatcherGlib);
522 return d->timerSource->timerList.unregisterTimer(timerId);
QEventDispatcherGlibPrivate * d