Skip to content

Commit

Permalink
[memory][kpm] Fix waitEvent and extend applyAction
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Sep 29, 2023
1 parent c3c7545 commit 8770d48
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
14 changes: 10 additions & 4 deletions sc-kpm/sc-agents-common/utils/AgentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,21 @@ bool AgentUtils::applyAction(
return applyAction(ms_context, actionNode, waitTime);
}

bool AgentUtils::applyAction(ScMemoryContext * ms_context, const ScAddr & actionNode, const sc_uint32 & waitTime)
bool AgentUtils::applyAction(
ScMemoryContext * ms_context,
const ScAddr & actionNode,
const sc_uint32 & waitTime,
ScAddr onEventClassAddr)
{
if (!onEventClassAddr.IsValid())
onEventClassAddr = scAgentsCommon::CoreKeynodes::question_initiated;

auto check = [](ScAddr const & listenAddr, ScAddr const & edgeAddr, ScAddr const & otherAddr) {
return otherAddr == scAgentsCommon::CoreKeynodes::question_finished;
};

auto initialize = [ms_context, actionNode]() {
ms_context->CreateEdge(
ScType::EdgeAccessConstPosPerm, scAgentsCommon::CoreKeynodes::question_initiated, actionNode);
auto initialize = [ms_context, onEventClassAddr, actionNode]() {
ms_context->CreateEdge(ScType::EdgeAccessConstPosPerm, onEventClassAddr, actionNode);
};

ScWaitCondition<ScEventAddInputEdge> waiter(*ms_context, actionNode, SC_WAIT_CHECK(check));
Expand Down
3 changes: 2 additions & 1 deletion sc-kpm/sc-agents-common/utils/AgentUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class AgentUtils
static bool applyAction(
ScMemoryContext * ms_context,
const ScAddr & actionNode,
const sc_uint32 & waitTime = DEFAULT_WAIT_TIME);
const sc_uint32 & waitTime = DEFAULT_WAIT_TIME,
ScAddr onEventClassAddr = ScAddr::Empty);

SC_DEPRECATED(
0.7.0,
Expand Down
18 changes: 2 additions & 16 deletions sc-memory/sc-memory/sc_wait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,20 @@ class ScWait
void Resolve()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_isResolved = true;
m_cond.notify_one();
}

bool Wait(uint32_t timeout_ms, std::function<void(void)> const & initializationFunction)
{
std::unique_lock<std::mutex> lock(m_mutex);

sc_bool actionPerformed = SC_FALSE;
while (!m_isResolved)
{
if (actionPerformed == SC_FALSE)
{
initializationFunction();
actionPerformed = SC_TRUE;
}

if (m_cond.wait_for(lock, std::chrono::milliseconds(timeout_ms)) == std::cv_status::timeout)
return false;
}

return true;
initializationFunction();
return m_cond.wait_for(lock, std::chrono::milliseconds(timeout_ms)) == std::cv_status::timeout;
}

private:
std::mutex m_mutex;
std::condition_variable m_cond;
bool m_isResolved = false;
};

public:
Expand Down

0 comments on commit 8770d48

Please sign in to comment.