summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/EventLoop/EventLoop.cpp')
-rw-r--r--Swiften/EventLoop/EventLoop.cpp21
1 files changed, 9 insertions, 12 deletions
diff --git a/Swiften/EventLoop/EventLoop.cpp b/Swiften/EventLoop/EventLoop.cpp
index f6af699..31c93e9 100644
--- a/Swiften/EventLoop/EventLoop.cpp
+++ b/Swiften/EventLoop/EventLoop.cpp
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2010-2018 Isode Limited. 2 * Copyright (c) 2010-2019 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
@@ -35,9 +35,8 @@ EventLoop::EventLoop() : nextEventID_(0), handlingEvents_(false) {
35EventLoop::~EventLoop() { 35EventLoop::~EventLoop() {
36} 36}
37 37
38void EventLoop::handleNextEvents() { 38void EventLoop::handleNextEvent() {
39 const int eventsBatched = 100; 39 // If handleNextEvent is already in progress, e.g. in case of a recursive call due to
40 // If handleNextEvents is already in progress, e.g. in case of a recursive call due to
41 // the event loop implementation, then do no handle further events. Instead call 40 // the event loop implementation, then do no handle further events. Instead call
42 // eventPosted() to continue event handling later. 41 // eventPosted() to continue event handling later.
43 bool callEventPosted = handlingEvents_; 42 bool callEventPosted = handlingEvents_;
@@ -45,19 +44,17 @@ void EventLoop::handleNextEvents() {
45 handlingEvents_ = true; 44 handlingEvents_ = true;
46 std::unique_lock<std::recursive_mutex> lock(removeEventsMutex_); 45 std::unique_lock<std::recursive_mutex> lock(removeEventsMutex_);
47 { 46 {
48 std::vector<Event> nextEvents; 47 boost::optional<Event> nextEvent;
49 { 48 {
50 std::unique_lock<std::recursive_mutex> lock(eventsMutex_); 49 std::unique_lock<std::recursive_mutex> eventsLock(eventsMutex_);
51 for (int n = 0; ((n < eventsBatched) && !events_.empty()); n++) { 50 if (!events_.empty()) {
52 nextEvents.push_back(events_.front()); 51 nextEvent = events_.front();
53 events_.pop_front(); 52 events_.pop_front();
54 } 53 }
55 callEventPosted = !events_.empty(); 54 callEventPosted = !events_.empty();
56 } 55 }
57 if (!nextEvents.empty()) { 56 if (nextEvent) {
58 for (const auto& event : nextEvents) { 57 invokeCallback(*nextEvent);
59 invokeCallback(event);
60 }
61 } 58 }
62 } 59 }
63 handlingEvents_ = false; 60 handlingEvents_ = false;