rewrite IoContext.

This commit is contained in:
amass 2024-11-10 18:02:28 +08:00
parent ddffa49a42
commit 090ea0803e
5 changed files with 50 additions and 25 deletions

View File

@ -12,6 +12,13 @@
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
},
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"E:/Projects/Libraries/boost_1_86_0_msvc2022_64bit/include/boost-1_86"
]
}
],
"version": 4

View File

@ -47,4 +47,9 @@ void *Buffer::release() {
m_size = 0;
return m_buffer;
}
std::string_view Buffer::data() const noexcept {
return std::string_view{reinterpret_cast<const char *>(m_buffer), m_size};
}
} // namespace Nng

View File

@ -2,6 +2,7 @@
#define __BUFFER_H__
#include <cstdint>
#include <string_view>
namespace Nng {
@ -37,6 +38,8 @@ public:
return reinterpret_cast<T *>(m_buffer);
}
std::string_view data() const noexcept;
std::size_t size() const noexcept {
return m_size;
}

View File

@ -3,23 +3,43 @@
#include <boost/scope_exit.hpp>
#include <boost/stacktrace.hpp>
IoContext::IoContext(int concurrency_hint)
: m_concurrency(concurrency_hint), m_ioContext(std::make_shared<boost::asio::io_context>(concurrency_hint)) {
}
IoContext::~IoContext() {
stop();
}
void IoContext::stop() {
m_guarder.reset();
m_ioContext->stop();
if (m_thread.joinable()) m_thread.join();
if (!m_asynchronous) {
for (auto &thread : m_threads) {
if (thread.joinable()) thread.join();
}
m_threads.clear();
}
}
void IoContext::run(bool asynchronous) {
using namespace boost::asio;
if (m_concurrency < 1) m_concurrency = 1;
m_asynchronous = asynchronous;
for (int i = 0; i < m_concurrency; i++) {
m_threads.push_back(std::thread(&IoContext::runIoContext, this));
}
m_guarder = std::make_unique<executor_work_guard<io_context::executor_type>>(m_ioContext->get_executor());
if (asynchronous) {
for (auto &thread : m_threads) {
if (thread.joinable()) thread.join();
}
m_threads.clear();
}
}
void IoContext::runIoContext() {
LOG(info) << "asio context started ...";
BOOST_SCOPE_EXIT(void) {
LOG(info) << "asio context exited ...";
}
BOOST_SCOPE_EXIT_END
try {
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work(m_ioContext->get_executor());
m_ioContext->run();
} catch (const boost::log::system_error &error) {
LOG(error) << error.what();

View File

@ -9,28 +9,17 @@
class IoContext {
public:
enum class Mode {
Synchronous,
Asynchronous,
};
friend class Amass::Singleton<IoContext, Amass::LocalInstance>;
std::shared_ptr<boost::asio::io_context> ioContext() const {
return m_ioContext;
}
template <Mode mode = Mode::Asynchronous>
void run() {
if constexpr (mode == Mode::Asynchronous) {
m_thread = std::thread(&IoContext::runIoContext, this);
} else {
runIoContext();
}
}
void run(bool asynchronous = true) ;
void stop();
~IoContext();
/**
* @brief
*
* @brief
*
* @tparam Executor boost::asio::io_contextboost::asio::strand( boost::asio::make_strand(io_context); )
*/
template <class Executor, class Func, class Rep, class Period>
@ -47,14 +36,15 @@ public:
}
protected:
template <typename... Args>
IoContext(Args &&...args) : m_ioContext(std::make_shared<boost::asio::io_context>(std::forward<Args>(args)...)) {
}
IoContext(int concurrency);
void runIoContext();
private:
std::thread m_thread;
int m_concurrency = 1;
std::shared_ptr<boost::asio::io_context> m_ioContext;
std::unique_ptr<boost::asio::executor_work_guard<boost::asio::io_context::executor_type>> m_guarder;
std::vector<std::thread> m_threads;
bool m_asynchronous=false;
};
#endif // IOCONTEXT_H