File tree Expand file tree Collapse file tree 2 files changed +40
-6
lines changed Expand file tree Collapse file tree 2 files changed +40
-6
lines changed Original file line number Diff line number Diff line change @@ -65,14 +65,14 @@ void EventDispatcher::detach() {
65
65
66
66
void EventDispatcher::schedule (std::function<void ()> functor) {
67
67
FCITX_D ();
68
- if (!functor) {
69
- return ;
70
- }
71
68
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));
74
75
}
75
- d->eventList_ .push (std::move (functor));
76
76
d->asyncEvent_ ->send ();
77
77
}
78
78
Original file line number Diff line number Diff line change 6
6
*/
7
7
#include < unistd.h>
8
8
#include < atomic>
9
+ #include < mutex>
9
10
#include < thread>
10
11
#include < vector>
11
12
#include " fcitx-utils/event.h"
12
13
#include " fcitx-utils/eventdispatcher.h"
14
+ #include " fcitx-utils/eventloopinterface.h"
13
15
#include " fcitx-utils/log.h"
14
16
#include " fcitx-utils/trackableobject.h"
15
17
@@ -102,11 +104,43 @@ void withContext() {
102
104
FCITX_ASSERT (!invalidCalled);
103
105
}
104
106
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
+
105
138
int main () {
106
139
fcitx::Log::setLogRule (" *=5" );
107
140
basicTest ();
108
141
testOrder ();
109
142
recursiveSchedule ();
110
143
withContext ();
144
+ scheduleNull ();
111
145
return 0 ;
112
146
}
You can’t perform that action at this time.
0 commit comments