Skip to content

Commit 1cd5f66

Browse files
committed
Add back support for EventDispatcher::schedule to accept a nullptr.
Also add a test to ensure we don't regress on this again.
1 parent 7921881 commit 1cd5f66

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/lib/fcitx-utils/eventdispatcher.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ void EventDispatcher::detach() {
6565

6666
void EventDispatcher::schedule(std::function<void()> functor) {
6767
FCITX_D();
68-
if (!functor) {
69-
return;
70-
}
7168
std::lock_guard<std::mutex> lock(d->mutex_);
72-
if (!d->asyncEvent_) {
73-
return;
69+
// functor can be null and we will still trigger async event.
70+
if (functor) {
71+
if (!d->asyncEvent_) {
72+
return;
73+
}
74+
d->eventList_.push(std::move(functor));
7475
}
75-
d->eventList_.push(std::move(functor));
7676
d->asyncEvent_->send();
7777
}
7878

test/testeventdispatcher.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
*/
77
#include <unistd.h>
88
#include <atomic>
9+
#include <mutex>
910
#include <thread>
1011
#include <vector>
1112
#include "fcitx-utils/event.h"
1213
#include "fcitx-utils/eventdispatcher.h"
14+
#include "fcitx-utils/eventloopinterface.h"
1315
#include "fcitx-utils/log.h"
1416
#include "fcitx-utils/trackableobject.h"
1517

@@ -102,11 +104,43 @@ void withContext() {
102104
FCITX_ASSERT(!invalidCalled);
103105
}
104106

107+
void scheduleNull() {
108+
EventLoop e;
109+
EventDispatcher dispatcher;
110+
std::mutex readyLock;
111+
bool ready = false;
112+
// Post event may run immediately after exec, so we need a "ready" to ensure
113+
// it is after schedule the event.
114+
auto post = e.addPostEvent([&ready, &readyLock, &e](EventSource *) {
115+
FCITX_INFO() << "POST IO";
116+
{
117+
std::lock_guard<std::mutex> lock(readyLock);
118+
if (ready) {
119+
e.exit();
120+
}
121+
}
122+
return true;
123+
});
124+
dispatcher.attach(&e);
125+
std::thread thread([&dispatcher, &ready, &readyLock]() {
126+
sleep(2);
127+
{
128+
std::lock_guard<std::mutex> lock(readyLock);
129+
ready = true;
130+
}
131+
// Test schedule nullptr is accepted.
132+
dispatcher.schedule(nullptr);
133+
});
134+
e.exec();
135+
thread.join();
136+
}
137+
105138
int main() {
106139
fcitx::Log::setLogRule("*=5");
107140
basicTest();
108141
testOrder();
109142
recursiveSchedule();
110143
withContext();
144+
scheduleNull();
111145
return 0;
112146
}

0 commit comments

Comments
 (0)