Skip to content
Snippets Groups Projects
Commit 36dfd11f authored by Adrien Béraud's avatar Adrien Béraud
Browse files

manager: add shared asio::io_context

Change-Id: Iebd54e75d9f7baaa61547dd1e18100a7d88cc807
parent 331a4105
No related branches found
Tags
No related merge requests found
...@@ -209,6 +209,8 @@ AS_IF([test "x$enable_shared" == "xyes"], [ ...@@ -209,6 +209,8 @@ AS_IF([test "x$enable_shared" == "xyes"], [
]) ])
AC_MSG_RESULT([$RING_SHARED]) AC_MSG_RESULT([$RING_SHARED])
CPPFLAGS="${CPPFLAGS} -DASIO_STANDALONE"
dnl dnl
dnl Check for the contrib directory dnl Check for the contrib directory
dnl dnl
......
...@@ -83,6 +83,9 @@ using random_device = dht::crypto::random_device; ...@@ -83,6 +83,9 @@ using random_device = dht::crypto::random_device;
#include <opendht/thread_pool.h> #include <opendht/thread_pool.h>
#include <asio/io_context.hpp>
#include <asio/executor_work_guard.hpp>
#ifndef WIN32 #ifndef WIN32
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
...@@ -331,6 +334,9 @@ struct Manager::ManagerPimpl ...@@ -331,6 +334,9 @@ struct Manager::ManagerPimpl
Manager& base_; // pimpl back-pointer Manager& base_; // pimpl back-pointer
std::shared_ptr<asio::io_context> ioContext_;
std::thread ioContextRunner_;
/** Main scheduler */ /** Main scheduler */
ScheduledExecutor scheduler_; ScheduledExecutor scheduler_;
...@@ -408,6 +414,7 @@ struct Manager::ManagerPimpl ...@@ -408,6 +414,7 @@ struct Manager::ManagerPimpl
Manager::ManagerPimpl::ManagerPimpl(Manager& base) Manager::ManagerPimpl::ManagerPimpl(Manager& base)
: base_(base) : base_(base)
, ioContext_(std::make_shared<asio::io_context>())
, toneCtrl_(base.preferences) , toneCtrl_(base.preferences)
, currentCallMutex_() , currentCallMutex_()
, dtmfKey_() , dtmfKey_()
...@@ -431,6 +438,15 @@ Manager::ManagerPimpl::ManagerPimpl(Manager& base) ...@@ -431,6 +438,15 @@ Manager::ManagerPimpl::ManagerPimpl(Manager& base)
rand_.seed(seed); rand_.seed(seed);
jami::libav_utils::ring_avcodec_init(); jami::libav_utils::ring_avcodec_init();
ioContextRunner_ = std::thread([context = ioContext_](){
try {
auto work = asio::make_work_guard(*context);
context->run();
} catch (const std::exception& ex) {
JAMI_ERR("Unexpected io_context thread exception: %s", ex.what());
}
});
} }
bool bool
...@@ -836,6 +852,13 @@ Manager::finish() noexcept ...@@ -836,6 +852,13 @@ Manager::finish() noexcept
dht::ThreadPool::computation().join(); dht::ThreadPool::computation().join();
pj_shutdown(); pj_shutdown();
if (!pimpl_->ioContext_->stopped()){
pimpl_->ioContext_->reset(); // allow to finish
pimpl_->ioContext_->stop(); // make thread stop
}
if (pimpl_->ioContextRunner_.joinable())
pimpl_->ioContextRunner_.join();
} catch (const VoipLinkException &err) { } catch (const VoipLinkException &err) {
JAMI_ERR("%s", err.what()); JAMI_ERR("%s", err.what());
} }
...@@ -1704,6 +1727,12 @@ Manager::scheduler() ...@@ -1704,6 +1727,12 @@ Manager::scheduler()
return pimpl_->scheduler_; return pimpl_->scheduler_;
} }
std::shared_ptr<asio::io_context>
Manager::ioContext() const
{
return pimpl_->ioContext_;
}
void void
Manager::addTask(std::function<bool()>&& task) Manager::addTask(std::function<bool()>&& task)
{ {
......
...@@ -44,6 +44,10 @@ ...@@ -44,6 +44,10 @@
#include <functional> #include <functional>
#include <algorithm> #include <algorithm>
namespace asio {
class io_context;
}
namespace jami { namespace jami {
namespace video { namespace video {
...@@ -858,6 +862,8 @@ class Manager { ...@@ -858,6 +862,8 @@ class Manager {
ScheduledExecutor& scheduler(); ScheduledExecutor& scheduler();
std::shared_ptr<asio::io_context> ioContext() const;
void addTask(std::function<bool()>&& task); void addTask(std::function<bool()>&& task);
std::shared_ptr<Task> scheduleTask(std::function<void()>&& task, std::chrono::steady_clock::time_point when); std::shared_ptr<Task> scheduleTask(std::function<void()>&& task, std::chrono::steady_clock::time_point when);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment