-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
165 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024-2024 CSSlayer <[email protected]> | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* | ||
*/ | ||
#include "workerthread.h" | ||
#include <condition_variable> | ||
#include <fcitx-utils/eventdispatcher.h> | ||
#include <memory> | ||
#include <mutex> | ||
#include <thread> | ||
|
||
WorkerThread::WorkerThread(fcitx::EventDispatcher &dispatcher) | ||
: dispatcher_(dispatcher), thread_(&WorkerThread::runThread, this) {} | ||
|
||
WorkerThread::~WorkerThread() { | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
exit_ = true; | ||
condition_.notify_one(); | ||
} | ||
if (thread_.joinable()) { | ||
thread_.join(); | ||
} | ||
} | ||
|
||
std::unique_ptr<TaskToken> | ||
WorkerThread::addTaskImpl(std::function<void()> task, | ||
std::function<void()> onDone) { | ||
auto token = std::make_unique<TaskToken>(); | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
queue_.push({.task = std::move(task), | ||
.callback = std::move(onDone), | ||
.context = token->watch()}); | ||
condition_.notify_one(); | ||
return token; | ||
} | ||
|
||
void WorkerThread::run() { | ||
while (true) { | ||
Task task; | ||
{ | ||
std::unique_lock lock(mutex_); | ||
condition_.wait(lock, [this] { return exit_ || !queue_.empty(); }); | ||
if (exit_) { | ||
break; | ||
} | ||
|
||
task = std::move(queue_.front()); | ||
queue_.pop(); | ||
} | ||
task.task(); | ||
dispatcher_.scheduleWithContext(task.context, std::move(task.callback)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024-2024 CSSlayer <[email protected]> | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* | ||
*/ | ||
#ifndef _PINYIN_WORKERTHREAD_H_ | ||
#define _PINYIN_WORKERTHREAD_H_ | ||
|
||
#include <condition_variable> | ||
#include <fcitx-utils/eventdispatcher.h> | ||
#include <fcitx-utils/macros.h> | ||
#include <fcitx-utils/trackableobject.h> | ||
#include <functional> | ||
#include <future> | ||
#include <list> | ||
#include <memory> | ||
#include <mutex> | ||
#include <queue> | ||
|
||
class TaskToken : public fcitx::TrackableObject<TaskToken> {}; | ||
|
||
class WorkerThread { | ||
public: | ||
WorkerThread(fcitx::EventDispatcher &dispatcher); | ||
~WorkerThread(); | ||
|
||
template <typename Ret, typename OnDone> | ||
FCITX_NODISCARD std::unique_ptr<TaskToken> | ||
addTask(std::packaged_task<Ret()> task, OnDone onDone) { | ||
std::future<Ret> future = task.get_future(); | ||
std::function<void()> taskFunction = | ||
[task = std::make_shared<decltype(task)>( | ||
std::move(task))]() mutable { (*task)(); }; | ||
std::function<void()> callback = [onDone = std::move(onDone), | ||
future = future.share()]() mutable { | ||
onDone(future); | ||
}; | ||
|
||
return addTaskImpl(std::move(taskFunction), std::move(callback)); | ||
} | ||
|
||
private: | ||
std::unique_ptr<TaskToken> addTaskImpl(std::function<void()> task, | ||
std::function<void()> onDone); | ||
static void runThread(WorkerThread *self) { self->run(); } | ||
void run(); | ||
|
||
struct Task { | ||
std::function<void()> task; | ||
std::function<void()> callback; | ||
fcitx::TrackableObjectReference<TaskToken> context; | ||
}; | ||
|
||
fcitx::EventDispatcher &dispatcher_; | ||
std::mutex mutex_; | ||
std::queue<Task, std::list<Task>> queue_; | ||
bool exit_ = false; | ||
std::condition_variable condition_; | ||
|
||
// Must be the last member | ||
std::thread thread_; | ||
}; | ||
|
||
#endif |