Skip to content

Commit

Permalink
Issue #31 - Fix thread safety.
Browse files Browse the repository at this point in the history
  • Loading branch information
vldtecno authored and Eduardo Valgôde committed May 14, 2023
1 parent 28a6acc commit 956dd8a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
26 changes: 13 additions & 13 deletions PTN_Engine/Place.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void Place::enterPlace(const size_t tokens)
// is thrown.
std::this_thread::sleep_for(100ms);
}
executeAction(m_onEnterAction, m_onEnterActionInExecution);
executeAction(m_onEnterAction, m_onEnterActionsInExecution);
}

void Place::exitPlace(const size_t tokens)
Expand All @@ -93,7 +93,7 @@ void Place::exitPlace(const size_t tokens)
{
return;
}
executeAction(m_onExitAction, m_onExitActionInExecution);
executeAction(m_onExitAction, m_onExitActionsInExecution);
}

void Place::increaseNumberOfTokens(const size_t tokens)
Expand Down Expand Up @@ -157,7 +157,7 @@ const string Place::getOnExitActionName() const
return m_onExitActionName;
}

void Place::executeAction(const ActionFunction &action, std::atomic<size_t> &actionInExecution)
void Place::executeAction(const ActionFunction &action, std::atomic<size_t> &actionsInExecution)
{
switch (m_ptnEngine.getActionsThreadOption())
{
Expand All @@ -168,31 +168,31 @@ void Place::executeAction(const ActionFunction &action, std::atomic<size_t> &act
case PTN_Engine::ACTIONS_THREAD_OPTION::SINGLE_THREAD:
case PTN_Engine::ACTIONS_THREAD_OPTION::EVENT_LOOP:
{
actionInExecution = true;
++actionsInExecution;
action();
actionInExecution = false;
--actionsInExecution;
break;
}
case PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE:
{
++actionInExecution;
auto f = [&actionInExecution, &action]()
++actionsInExecution;
auto f = [&actionsInExecution, &action]()
{
action();
--actionInExecution;
--actionsInExecution;
};
m_ptnEngine.addJob(f);
break;
}
case PTN_Engine::ACTIONS_THREAD_OPTION::DETACHED:
{
++actionInExecution;
auto f = [&actionInExecution, &action]()
++actionsInExecution;
auto job = [&actionsInExecution, &action]()
{
action();
--actionInExecution;
--actionsInExecution;
};
auto t = std::thread(f);
auto t = std::thread(job);
t.detach();
break;
}
Expand All @@ -201,7 +201,7 @@ void Place::executeAction(const ActionFunction &action, std::atomic<size_t> &act

bool Place::isOnEnterActionInExecution() const
{
return m_onEnterActionInExecution > 0;
return m_onEnterActionsInExecution > 0;
}

void Place::blockStartingOnEnterActions(const bool value)
Expand Down
4 changes: 2 additions & 2 deletions PTN_Engine/Place.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ class Place final
mutable std::shared_mutex m_mutex;

//! Counter of on enter functions being executed.
std::atomic<size_t> m_onEnterActionInExecution = 0;
std::atomic<size_t> m_onEnterActionsInExecution = 0;

//! Counter of on exit functions being executed.
std::atomic<size_t> m_onExitActionInExecution = 0;
std::atomic<size_t> m_onExitActionsInExecution = 0;

//! Flag to block triggering on enter actions.
std::atomic<bool> m_blockStartingOnEnterActions = false;
Expand Down
3 changes: 1 addition & 2 deletions PTN_Engine/Transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ bool Transition::isEnabledInternal() const

bool Transition::isActive() const
{
return isEnabledInternal() &&
(!m_requireNoActionsInExecution || (m_requireNoActionsInExecution && noActionsInExecution())) &&
return isEnabledInternal() && (!m_requireNoActionsInExecution || noActionsInExecution()) &&
checkAdditionalConditions();
}

Expand Down

0 comments on commit 956dd8a

Please sign in to comment.