2021-06-17 14:43:29 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* SerialTaskQueue.cc
|
|
|
|
* An Tao
|
|
|
|
*
|
|
|
|
* Public header file in trantor lib.
|
|
|
|
*
|
|
|
|
* Copyright 2018, An Tao. All rights reserved.
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the License file.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <trantor/utils/Logger.h>
|
2021-06-17 14:53:13 +02:00
|
|
|
#include <trantor/utils/SerialTaskQueue.h>
|
2021-06-17 14:43:29 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
2021-06-17 14:53:13 +02:00
|
|
|
namespace trantor {
|
|
|
|
SerialTaskQueue::SerialTaskQueue(const std::string &name) :
|
|
|
|
queueName_(name.empty() ? "SerailTaskQueue" : name),
|
|
|
|
loopThread_(queueName_) {
|
|
|
|
loopThread_.run();
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
2021-06-17 14:53:13 +02:00
|
|
|
void SerialTaskQueue::stop() {
|
|
|
|
stop_ = true;
|
|
|
|
loopThread_.getLoop()->quit();
|
|
|
|
loopThread_.wait();
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
2021-06-17 14:53:13 +02:00
|
|
|
SerialTaskQueue::~SerialTaskQueue() {
|
|
|
|
if (!stop_)
|
|
|
|
stop();
|
|
|
|
LOG_TRACE << "destruct SerialTaskQueue('" << queueName_ << "')";
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
2021-06-17 14:53:13 +02:00
|
|
|
void SerialTaskQueue::runTaskInQueue(const std::function<void()> &task) {
|
|
|
|
loopThread_.getLoop()->runInLoop(task);
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
2021-06-17 14:53:13 +02:00
|
|
|
void SerialTaskQueue::runTaskInQueue(std::function<void()> &&task) {
|
|
|
|
loopThread_.getLoop()->runInLoop(std::move(task));
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 14:53:13 +02:00
|
|
|
void SerialTaskQueue::waitAllTasksFinished() {
|
|
|
|
syncTaskInQueue([]() {
|
2021-06-17 14:43:29 +02:00
|
|
|
|
2021-06-17 14:53:13 +02:00
|
|
|
});
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 14:53:13 +02:00
|
|
|
} // namespace trantor
|