Skip to content

Commit

Permalink
Use scheduleWithContext where appropriate.
Browse files Browse the repository at this point in the history
When using shared dispatcher, we can not detach upon destruct, which
means we will need to check if context is still valid.
  • Loading branch information
wengxt committed Apr 21, 2024
1 parent c069a4c commit 12418f5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/modules/clipboard/waylandclipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace fcitx {

uint64_t DataReaderThread::addTask(std::shared_ptr<UnixFD> fd,
uint64_t DataReaderThread::addTask(DataOffer* offer, std::shared_ptr<UnixFD> fd,
DataOfferDataCallback callback) {
auto id = nextId_++;
if (id == 0) {
Expand All @@ -22,14 +22,15 @@ uint64_t DataReaderThread::addTask(std::shared_ptr<UnixFD> fd,
FCITX_CLIPBOARD_DEBUG() << "Add task: " << id << " " << fd;
dispatcherToWorker_.schedule([this, id, fd = std::move(fd),
dispatcher = &dispatcherToWorker_,
offerRef = offer->watch(),
callback = std::move(callback)]() mutable {
auto &task = ((*tasks_)[id] = std::make_unique<DataOfferTask>());
task->fd_ = fd;
task->callback_ = std::move(callback);
try {
task->ioEvent_ = dispatcher->eventLoop()->addIOEvent(
fd->fd(), {IOEventFlag::In, IOEventFlag::Err},
[this, id, task = task.get()](EventSource *, int fd,
[this, id, task = task.get(), offerRef](EventSource *, int fd,
IOEventFlags flags) {
if (flags.test(IOEventFlag::Err)) {
tasks_->erase(id);
Expand All @@ -38,7 +39,8 @@ uint64_t DataReaderThread::addTask(std::shared_ptr<UnixFD> fd,
char buf[4096];
auto n = fs::safeRead(fd, buf, sizeof(buf));
if (n == 0) {
dispatcherToMain_.schedule(
dispatcherToMain_.scheduleWithContext(
offerRef,
[data = std::move(task->data_),
callback = std::move(task->callback_)]() {
callback(data);
Expand Down Expand Up @@ -164,7 +166,7 @@ void DataOffer::receiveDataForMime(const std::string &mime,
offer_->receive(mime.data(), pipeFds[1]);
close(pipeFds[1]);

taskId_ = thread_->addTask(
taskId_ = thread_->addTask(this,
std::make_shared<UnixFD>(UnixFD::own(pipeFds[0])), std::move(callback));
}

Expand Down
6 changes: 4 additions & 2 deletions src/modules/clipboard/waylandclipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fcitx-utils/event.h>
#include <fcitx-utils/eventdispatcher.h>
#include <fcitx-utils/signals.h>
#include <fcitx-utils/trackableobject.h>
#include <fcitx-utils/unixfd.h>
#include "display.h"
#include "zwlr_data_control_device_v1.h"
Expand All @@ -37,6 +38,7 @@ struct DataOfferTask {
std::unique_ptr<EventSource> timeEvent_;
};

class DataOffer;
class DataReaderThread {
public:
DataReaderThread(EventDispatcher &dispatcherToMain)
Expand All @@ -59,7 +61,7 @@ class DataReaderThread {

static void run(DataReaderThread *self) { self->realRun(); }

uint64_t addTask(std::shared_ptr<UnixFD> fd,
uint64_t addTask(DataOffer *offer, std::shared_ptr<UnixFD> fd,
DataOfferDataCallback callback);
void removeTask(uint64_t token);

Expand All @@ -75,7 +77,7 @@ class DataReaderThread {
nullptr;
};

class DataOffer {
class DataOffer : public TrackableObject<DataOffer> {
public:
DataOffer(wayland::ZwlrDataControlOfferV1 *offer, bool ignorePassword);
~DataOffer();
Expand Down
6 changes: 3 additions & 3 deletions src/modules/wayland/waylandeventreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ WaylandEventReader::WaylandEventReader(WaylandConnection *conn)
// Actively trigger an initial dispatch so we can make sure:
// 1. depending even before this WaylandEventReader are handled.
// 2. prepare_read is called.
dispatcherToMain_.schedule([this]() { dispatch(); });
dispatcherToMain_.scheduleWithContext(watch(), [this]() { dispatch(); });
thread_ =
std::make_unique<std::thread>(&WaylandEventReader::runThread, this);
}
Expand Down Expand Up @@ -91,7 +91,7 @@ bool WaylandEventReader::onIOEvent(IOEventFlags flags) {
}

wl_display_read_events(display_);
dispatcherToMain_.schedule([this]() { dispatch(); });
dispatcherToMain_.scheduleWithContext(watch(), [this]() { dispatch(); });
return true;
}

Expand All @@ -109,7 +109,7 @@ void WaylandEventReader::quit() {
// Make sure the connection will be removed.
// The destructor will join the reader thread so it's ok.
dispatcherToMain_.scheduleWithContext(
this->watch(), [module = module_, name = conn_->name()]() {
watch(), [module = module_, name = conn_->name()]() {
module->removeConnection(name);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/xcb/xcbeventreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool XCBEventReader::onIOEvent(IOEventFlags flags) {
hadError_ = true;
FCITX_WARN() << "XCB connection \"" << conn_->name()
<< "\" got error: " << err;
dispatcherToMain_.schedule([this]() {
dispatcherToMain_.scheduleWithContext(watch(),[this]() {
deferEvent_ =
conn_->parent()->instance()->eventLoop().addDeferEvent(
[this](EventSource *) {
Expand All @@ -72,7 +72,7 @@ bool XCBEventReader::onIOEvent(IOEventFlags flags) {
hasEvent = !events_.empty();
}
if (hasEvent) {
dispatcherToMain_.schedule([this]() { conn_->processEvent(); });
dispatcherToMain_.scheduleWithContext(watch(), [this]() { conn_->processEvent(); });
}
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/xcb/xcbeventreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
#include <thread>
#include <fcitx-utils/event.h>
#include <fcitx-utils/eventdispatcher.h>
#include <fcitx-utils/trackableobject.h>
#include <xcb/xcb.h>
#include "xcb_public.h"

namespace fcitx {

class XCBConnection;

class XCBEventReader {
class XCBEventReader : public TrackableObject<XCBEventReader> {
public:
XCBEventReader(XCBConnection *conn);
~XCBEventReader();
Expand Down

0 comments on commit 12418f5

Please sign in to comment.