Skip to content

Commit 6c13b48

Browse files
committed
Added comments.
1 parent 11fb9d5 commit 6c13b48

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

source/base/threading/asio_event_dispatcher.cc

+7-9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ bool AsioEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags)
5454

5555
QCoreApplication::sendPostedEvents();
5656

57+
// When calling method sendPostedEvents, the state of variable may change, so we check it.
5758
if (interrupted_.load(std::memory_order_relaxed))
5859
return false;
5960

@@ -106,8 +107,6 @@ void AsioEventDispatcher::unregisterSocketNotifier(QSocketNotifier* /* notifier
106107
void AsioEventDispatcher::registerTimer(
107108
int id, int interval, Qt::TimerType type, QObject* object)
108109
{
109-
DCHECK(object);
110-
111110
TimePoint start_time = Clock::now();
112111
TimePoint expire_time = start_time + Milliseconds(interval);
113112

@@ -132,8 +131,6 @@ bool AsioEventDispatcher::unregisterTimer(int id)
132131
//--------------------------------------------------------------------------------------------------
133132
bool AsioEventDispatcher::unregisterTimers(QObject* object)
134133
{
135-
DCHECK(object);
136-
137134
auto it = timers_.begin();
138135
bool removed = false;
139136

@@ -157,14 +154,11 @@ bool AsioEventDispatcher::unregisterTimers(QObject* object)
157154
}
158155

159156
//--------------------------------------------------------------------------------------------------
160-
QList<QAbstractEventDispatcher::TimerInfo> AsioEventDispatcher::registeredTimers(
161-
QObject* object) const
157+
QList<QAbstractEventDispatcher::TimerInfo> AsioEventDispatcher::registeredTimers(QObject* object) const
162158
{
163-
DCHECK(object);
164-
165159
QList<TimerInfo> list;
166160

167-
for (auto it = timers_.cbegin(); it != timers_.cend(); ++it)
161+
for (auto it = timers_.cbegin(), it_end = timers_.cend(); it != it_end; ++it)
168162
{
169163
if (it->second.object == object)
170164
list.append({ it->second.id, it->second.interval, it->second.type });
@@ -239,6 +233,7 @@ void AsioEventDispatcher::unregisterEventNotifier(QWinEventNotifier* notifier)
239233
//--------------------------------------------------------------------------------------------------
240234
void AsioEventDispatcher::wakeUp()
241235
{
236+
// Send an empty lambda so that call run_one inside method processEvents completes.
242237
asio::post(io_context_, []{});
243238
}
244239

@@ -294,12 +289,14 @@ void AsioEventDispatcher::asyncWaitForNextTimer()
294289
if (timers_.empty())
295290
return;
296291

292+
// Find the timer that should be completed before all others.
297293
auto next_expire_timer = std::min_element(timers_.begin(), timers_.end(),
298294
[](const auto& lhs, const auto& rhs)
299295
{
300296
return lhs.second.expire_time < rhs.second.expire_time;
301297
});
302298

299+
// Start waiting for the timer.
303300
timer_.expires_at(next_expire_timer->second.expire_time);
304301
timer_.async_wait(std::bind(
305302
&AsioEventDispatcher::onTimerEvent, this, std::placeholders::_1, next_expire_timer->second.id));
@@ -317,6 +314,7 @@ void AsioEventDispatcher::onTimerEvent(const std::error_code& error_code, int id
317314

318315
QCoreApplication::sendEvent(it->second.object, new QTimerEvent(id));
319316

317+
// When calling method sendEvent the timer may have been deleted, so we look for it again.
320318
it = timers_.find(id);
321319
if (it == timers_.end())
322320
return;

0 commit comments

Comments
 (0)