RPC: host adapter boundary for embedded and managed modes (S-RPC-05)
This commit is contained in:
1 parent
1f92dd054e
commit
c6c0303ef8
3 files changed
+201
No files matched your search
@@ -0,0 +1,67 @@
|
||||
#include <Void/RPC/HostAdapter.hpp>
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace Void::RPC
|
||||
{
|
||||
bool InlineHostAdapter::post(std::function<void()> task)
|
||||
{
|
||||
if (!task) {
|
||||
return false;
|
||||
}
|
||||
task();
|
||||
return true;
|
||||
}
|
||||
|
||||
DeferredHostAdapter::DeferredHostAdapter(HostLimits limits)
|
||||
: m_limits(limits)
|
||||
{
|
||||
}
|
||||
|
||||
bool DeferredHostAdapter::post(std::function<void()> task)
|
||||
{
|
||||
if (!task) {
|
||||
return false;
|
||||
}
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (m_tasks.size() >= m_limits.max_pending_tasks) {
|
||||
return false;
|
||||
}
|
||||
m_tasks.push_back(std::move(task));
|
||||
return true;
|
||||
}
|
||||
|
||||
std::size_t DeferredHostAdapter::pending() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_tasks.size();
|
||||
}
|
||||
|
||||
std::size_t DeferredHostAdapter::capacity() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_limits.max_pending_tasks;
|
||||
}
|
||||
|
||||
std::size_t DeferredHostAdapter::run_ready(std::size_t budget)
|
||||
{
|
||||
std::size_t run = 0;
|
||||
for (;;) {
|
||||
if (budget != 0 && run >= budget) {
|
||||
break;
|
||||
}
|
||||
std::function<void()> task;
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (m_tasks.empty()) {
|
||||
break;
|
||||
}
|
||||
task = std::move(m_tasks.front());
|
||||
m_tasks.pop_front();
|
||||
}
|
||||
++run;
|
||||
task();
|
||||
}
|
||||
return run;
|
||||
}
|
||||
} // namespace Void::RPC
|
||||
Reference in new issue
Block a user