Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scheduleWithContext to event dispatcher #1014

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/lib/fcitx-utils/eventdispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <functional>
#include <memory>
#include <fcitx-utils/macros.h>
#include <fcitx-utils/trackableobject.h>
#include "fcitxutils_export.h"

namespace fcitx {
Expand Down Expand Up @@ -43,6 +44,7 @@ class FCITXUTILS_EXPORT EventDispatcher {
* thread from event loop.
*/
void detach();

/**
* A thread-safe function to schedule a functor to be call from event loop.
*
Expand All @@ -54,6 +56,33 @@ class FCITXUTILS_EXPORT EventDispatcher {
*/
void schedule(std::function<void()> functor);

/**
* A helper function that allows to only invoke certain function if the
* reference is still valid.
*
* If context object is not valid when calling scheduleWithContext, it won't
* be scheduled at all.
*
* @param context the context object.
* @param functor function to be scheduled
*
* @since 5.1.8
*/
template <typename T>
void scheduleWithContext(TrackableObjectReference<T> context,
std::function<void()> functor) {
if (!context.isValid()) {
return;
}

schedule(
[context = std::move(context), functor = std::move(functor)]() {
if (context.isValid()) {
functor();
}
});
}

/**
* Return the currently attached event loop
*
Expand Down
45 changes: 45 additions & 0 deletions test/testeventdispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <unistd.h>
#include <atomic>
#include <thread>
#include <vector>
#include "fcitx-utils/event.h"
#include "fcitx-utils/eventdispatcher.h"
#include "fcitx-utils/log.h"
#include "fcitx-utils/trackableobject.h"

using namespace fcitx;

Expand Down Expand Up @@ -38,6 +40,22 @@ void basicTest() {
thread.join();
}

void testOrder() {
EventLoop loop;
EventDispatcher dispatcher;
dispatcher.attach(&loop);
std::vector<int> value;
for (int i = 0; i < 100; i++) {
dispatcher.schedule([i, &value]() { value.push_back(i); });
}
dispatcher.schedule([&loop]() { loop.exit(); });
loop.exec();
FCITX_ASSERT(value.size() == 100);
for (int i = 0; i < 100; i++) {
FCITX_ASSERT(i == value[i]) << i << " " << value[i];
}
}

void recursiveSchedule() {
EventDispatcher dispatcher;
EventLoop loop;
Expand All @@ -59,8 +77,35 @@ void recursiveSchedule() {
FCITX_ASSERT(counter == 100);
}

class TestObject : public TrackableObject<TestObject> {};

void withContext() {
EventDispatcher dispatcher;
EventLoop loop;
dispatcher.attach(&loop);
bool called = false;
bool invalidCalled = false;
TestObject validObject;
{
TestObject invalidObject;
dispatcher.scheduleWithContext(validObject.watch(),
[&called]() { called = true; });
dispatcher.scheduleWithContext(
invalidObject.watch(),
[&invalidCalled]() { invalidCalled = true; });
}

dispatcher.schedule([&loop]() { loop.exit(); });
loop.exec();

FCITX_ASSERT(called);
FCITX_ASSERT(!invalidCalled);
}

int main() {
basicTest();
testOrder();
recursiveSchedule();
withContext();
return 0;
}
Loading