mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-14 04:57:21 +01:00
57 lines
1.2 KiB
C++
57 lines
1.2 KiB
C++
|
/**
|
||
|
*
|
||
|
* 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/SerialTaskQueue.h>
|
||
|
#include <trantor/utils/Logger.h>
|
||
|
#ifdef __linux__
|
||
|
#include <sys/prctl.h>
|
||
|
#endif
|
||
|
namespace trantor
|
||
|
{
|
||
|
SerialTaskQueue::SerialTaskQueue(const std::string &name)
|
||
|
: queueName_(name.empty() ? "SerailTaskQueue" : name),
|
||
|
loopThread_(queueName_)
|
||
|
{
|
||
|
loopThread_.run();
|
||
|
}
|
||
|
void SerialTaskQueue::stop()
|
||
|
{
|
||
|
stop_ = true;
|
||
|
loopThread_.getLoop()->quit();
|
||
|
loopThread_.wait();
|
||
|
}
|
||
|
SerialTaskQueue::~SerialTaskQueue()
|
||
|
{
|
||
|
if (!stop_)
|
||
|
stop();
|
||
|
LOG_TRACE << "destruct SerialTaskQueue('" << queueName_ << "')";
|
||
|
}
|
||
|
void SerialTaskQueue::runTaskInQueue(const std::function<void()> &task)
|
||
|
{
|
||
|
loopThread_.getLoop()->runInLoop(task);
|
||
|
}
|
||
|
void SerialTaskQueue::runTaskInQueue(std::function<void()> &&task)
|
||
|
{
|
||
|
loopThread_.getLoop()->runInLoop(std::move(task));
|
||
|
}
|
||
|
|
||
|
void SerialTaskQueue::waitAllTasksFinished()
|
||
|
{
|
||
|
syncTaskInQueue([]() {
|
||
|
|
||
|
});
|
||
|
}
|
||
|
|
||
|
} // namespace trantor
|