|
| 1 | +#include <QtCore/QPair> |
| 2 | +#include <QtCore/QSocketNotifier> |
| 3 | +#include <QtCore/QThread> |
| 4 | +#include "eventdispatcher_libev.h" |
| 5 | +#include "eventdispatcher_libev_p.h" |
| 6 | + |
| 7 | + |
| 8 | +EventDispatcherLibEv::EventDispatcherLibEv(QObject* parent) |
| 9 | + : QAbstractEventDispatcher(parent), d_ptr(new EventDispatcherLibEvPrivate(this)) |
| 10 | +{ |
| 11 | +} |
| 12 | + |
| 13 | +EventDispatcherLibEv::~EventDispatcherLibEv(void) |
| 14 | +{ |
| 15 | +} |
| 16 | + |
| 17 | +bool EventDispatcherLibEv::processEvents(QEventLoop::ProcessEventsFlags flags) |
| 18 | +{ |
| 19 | + Q_D(EventDispatcherLibEv); |
| 20 | + return d->processEvents(flags); |
| 21 | +} |
| 22 | + |
| 23 | +bool EventDispatcherLibEv::hasPendingEvents(void) |
| 24 | +{ |
| 25 | + extern uint qGlobalPostedEventsCount(); |
| 26 | + return qGlobalPostedEventsCount() > 0; |
| 27 | +} |
| 28 | + |
| 29 | +void EventDispatcherLibEv::registerSocketNotifier(QSocketNotifier* notifier) |
| 30 | +{ |
| 31 | +#ifndef QT_NO_DEBUG |
| 32 | + if (notifier->socket() < 0) { |
| 33 | + qWarning("QSocketNotifier: Internal error: sockfd < 0"); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if (notifier->thread() != thread() || thread() != QThread::currentThread()) { |
| 38 | + qWarning("QSocketNotifier: socket notifiers cannot be enabled from another thread"); |
| 39 | + return; |
| 40 | + } |
| 41 | +#endif |
| 42 | + |
| 43 | + if (notifier->type() == QSocketNotifier::Exception) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + Q_D(EventDispatcherLibEv); |
| 48 | + d->registerSocketNotifier(notifier); |
| 49 | +} |
| 50 | + |
| 51 | +void EventDispatcherLibEv::unregisterSocketNotifier(QSocketNotifier* notifier) |
| 52 | +{ |
| 53 | +#ifndef QT_NO_DEBUG |
| 54 | + if (notifier->socket() < 0) { |
| 55 | + qWarning("QSocketNotifier: Internal error: sockfd < 0"); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (notifier->thread() != thread() || thread() != QThread::currentThread()) { |
| 60 | + qWarning("QSocketNotifier: socket notifiers cannot be disabled from another thread"); |
| 61 | + return; |
| 62 | + } |
| 63 | +#endif |
| 64 | + |
| 65 | + // Short circuit, we do not support QSocketNotifier::Exception |
| 66 | + if (notifier->type() == QSocketNotifier::Exception) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + Q_D(EventDispatcherLibEv); |
| 71 | + d->unregisterSocketNotifier(notifier); |
| 72 | +} |
| 73 | + |
| 74 | +void EventDispatcherLibEv::registerTimer(int timerId,int interval, |
| 75 | + Qt::TimerType timerType,QObject* object) |
| 76 | +{ |
| 77 | +#ifndef QT_NO_DEBUG |
| 78 | + if (timerId < 1 || interval < 0 || !object) { |
| 79 | + qWarning("%s: invalid arguments", Q_FUNC_INFO); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (object->thread() != this->thread() && this->thread() != QThread::currentThread()) { |
| 84 | + qWarning("%s: timers cannot be started from another thread", Q_FUNC_INFO); |
| 85 | + return; |
| 86 | + } |
| 87 | +#endif |
| 88 | + |
| 89 | + Qt::TimerType type; |
| 90 | + type = timerType; |
| 91 | + |
| 92 | + Q_D(EventDispatcherLibEv); |
| 93 | + if (interval) { |
| 94 | + d->registerTimer(timerId, interval, type, object); |
| 95 | + } |
| 96 | + else { |
| 97 | + d->registerZeroTimer(timerId, object); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +bool EventDispatcherLibEv::unregisterTimer(int timerId) |
| 102 | +{ |
| 103 | +#ifndef QT_NO_DEBUG |
| 104 | + if (timerId < 1) { |
| 105 | + qWarning("%s: invalid arguments", Q_FUNC_INFO); |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + if (this->thread() != QThread::currentThread()) { |
| 110 | + qWarning("%s: timers cannot be stopped from another thread", Q_FUNC_INFO); |
| 111 | + return false; |
| 112 | + } |
| 113 | +#endif |
| 114 | + |
| 115 | + Q_D(EventDispatcherLibEv); |
| 116 | + return d->unregisterTimer(timerId); |
| 117 | +} |
| 118 | + |
| 119 | +bool EventDispatcherLibEv::unregisterTimers(QObject* object) |
| 120 | +{ |
| 121 | +#ifndef QT_NO_DEBUG |
| 122 | + if (!object) { |
| 123 | + qWarning("%s: invalid arguments", Q_FUNC_INFO); |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + if (object->thread() != this->thread() && this->thread() != QThread::currentThread()) { |
| 128 | + qWarning("%s: timers cannot be stopped from another thread", Q_FUNC_INFO); |
| 129 | + return false; |
| 130 | + } |
| 131 | +#endif |
| 132 | + |
| 133 | + Q_D(EventDispatcherLibEv); |
| 134 | + return d->unregisterTimers(object); |
| 135 | +} |
| 136 | + |
| 137 | +QList<QAbstractEventDispatcher::TimerInfo> EventDispatcherLibEv::registeredTimers(QObject* object) const |
| 138 | +{ |
| 139 | + if (!object) { |
| 140 | + qWarning("%s: invalid argument", Q_FUNC_INFO); |
| 141 | + return QList<QAbstractEventDispatcher::TimerInfo>(); |
| 142 | + } |
| 143 | + |
| 144 | + Q_D(const EventDispatcherLibEv); |
| 145 | + return d->registeredTimers(object); |
| 146 | +} |
| 147 | + |
| 148 | +int EventDispatcherLibEv::remainingTime(int timerId) |
| 149 | +{ |
| 150 | + Q_D(const EventDispatcherLibEv); |
| 151 | + return d->remainingTime(timerId); |
| 152 | +} |
| 153 | + |
| 154 | +void EventDispatcherLibEv::wakeUp(void) |
| 155 | +{ |
| 156 | + Q_D(EventDispatcherLibEv); |
| 157 | + |
| 158 | + if (d->m_wakeups.testAndSetAcquire(0, 1)) |
| 159 | + { |
| 160 | + ev_async_send(d->m_base, &d->m_wakeup); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +void EventDispatcherLibEv::interrupt(void) |
| 165 | +{ |
| 166 | + Q_D(EventDispatcherLibEv); |
| 167 | + d->m_interrupt = true; |
| 168 | + this->wakeUp(); |
| 169 | +} |
| 170 | + |
| 171 | +void EventDispatcherLibEv::flush(void) |
| 172 | +{ |
| 173 | +} |
| 174 | + |
| 175 | +EventDispatcherLibEv::EventDispatcherLibEv(EventDispatcherLibEvPrivate& dd, QObject* parent) |
| 176 | + : QAbstractEventDispatcher(parent), d_ptr(&dd) |
| 177 | +{ |
| 178 | +} |
0 commit comments