2021-06-17 14:43:29 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @file EventLoopThread.cc
|
|
|
|
* @author 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/net/EventLoopThread.h>
|
2022-02-10 00:50:19 +01:00
|
|
|
#include "core/log/logger.h"
|
2021-06-17 14:43:29 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace trantor;
|
2021-06-17 14:53:13 +02:00
|
|
|
EventLoopThread::EventLoopThread(const std::string &threadName) :
|
|
|
|
loop_(nullptr),
|
|
|
|
loopThreadName_(threadName),
|
|
|
|
thread_([this]() { loopFuncs(); }) {
|
|
|
|
auto f = promiseForLoopPointer_.get_future();
|
|
|
|
loop_ = f.get();
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
2021-06-17 14:53:13 +02:00
|
|
|
EventLoopThread::~EventLoopThread() {
|
|
|
|
run();
|
|
|
|
if (loop_) {
|
|
|
|
loop_->quit();
|
|
|
|
}
|
|
|
|
if (thread_.joinable()) {
|
|
|
|
thread_.join();
|
|
|
|
}
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
|
|
|
// void EventLoopThread::stop() {
|
|
|
|
// if(loop_)
|
|
|
|
// loop_->quit();
|
|
|
|
//}
|
2021-06-17 14:53:13 +02:00
|
|
|
void EventLoopThread::wait() {
|
|
|
|
thread_.join();
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
2021-06-17 14:53:13 +02:00
|
|
|
void EventLoopThread::loopFuncs() {
|
2021-06-17 14:43:29 +02:00
|
|
|
#ifdef __linux__
|
2021-06-17 14:53:13 +02:00
|
|
|
::prctl(PR_SET_NAME, loopThreadName_.c_str());
|
2021-06-17 14:43:29 +02:00
|
|
|
#endif
|
2021-06-17 14:53:13 +02:00
|
|
|
EventLoop loop;
|
|
|
|
loop.queueInLoop([this]() { promiseForLoop_.set_value(1); });
|
|
|
|
promiseForLoopPointer_.set_value(&loop);
|
|
|
|
auto f = promiseForRun_.get_future();
|
|
|
|
(void)f.get();
|
|
|
|
loop.loop();
|
|
|
|
// LOG_DEBUG << "loop out";
|
|
|
|
loop_ = NULL;
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|
2021-06-17 14:53:13 +02:00
|
|
|
void EventLoopThread::run() {
|
|
|
|
std::call_once(once_, [this]() {
|
|
|
|
auto f = promiseForLoop_.get_future();
|
|
|
|
promiseForRun_.set_value(1);
|
|
|
|
// Make sure the event loop loops before returning.
|
|
|
|
(void)f.get();
|
|
|
|
});
|
2021-06-17 14:43:29 +02:00
|
|
|
}
|