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", "cStandard": "c17",
"cppStandard": "gnu++17", "cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64" "intelliSenseMode": "linux-gcc-x64"
},
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"E:/Projects/Libraries/boost_1_86_0_msvc2022_64bit/include/boost-1_86"
]
} }
], ],
"version": 4 "version": 4

View File

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

View File

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

View File

@ -3,23 +3,43 @@
#include <boost/scope_exit.hpp> #include <boost/scope_exit.hpp>
#include <boost/stacktrace.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() { IoContext::~IoContext() {
stop(); stop();
} }
void IoContext::stop() { void IoContext::stop() {
m_guarder.reset();
m_ioContext->stop(); 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() { void IoContext::runIoContext() {
LOG(info) << "asio context started ...";
BOOST_SCOPE_EXIT(void) {
LOG(info) << "asio context exited ...";
}
BOOST_SCOPE_EXIT_END
try { try {
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work(m_ioContext->get_executor());
m_ioContext->run(); m_ioContext->run();
} catch (const boost::log::system_error &error) { } catch (const boost::log::system_error &error) {
LOG(error) << error.what(); LOG(error) << error.what();

View File

@ -9,22 +9,11 @@
class IoContext { class IoContext {
public: public:
enum class Mode {
Synchronous,
Asynchronous,
};
friend class Amass::Singleton<IoContext, Amass::LocalInstance>; friend class Amass::Singleton<IoContext, Amass::LocalInstance>;
std::shared_ptr<boost::asio::io_context> ioContext() const { std::shared_ptr<boost::asio::io_context> ioContext() const {
return m_ioContext; return m_ioContext;
} }
template <Mode mode = Mode::Asynchronous> void run(bool asynchronous = true) ;
void run() {
if constexpr (mode == Mode::Asynchronous) {
m_thread = std::thread(&IoContext::runIoContext, this);
} else {
runIoContext();
}
}
void stop(); void stop();
~IoContext(); ~IoContext();
@ -47,14 +36,15 @@ public:
} }
protected: protected:
template <typename... Args> IoContext(int concurrency);
IoContext(Args &&...args) : m_ioContext(std::make_shared<boost::asio::io_context>(std::forward<Args>(args)...)) {
}
void runIoContext(); void runIoContext();
private: private:
std::thread m_thread; int m_concurrency = 1;
std::shared_ptr<boost::asio::io_context> m_ioContext; 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 #endif // IOCONTEXT_H