-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #403 from dic-iit/system/barrier
Add a synchronization mechanism for the AdvanceableRunner
- Loading branch information
Showing
6 changed files
with
175 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @file Barrier.h | ||
* @authors Giulio Romualdi | ||
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. | ||
*/ | ||
|
||
#ifndef BIPEDAL_LOCOMOTION_SYSTEM_BARRIER_H | ||
#define BIPEDAL_LOCOMOTION_SYSTEM_BARRIER_H | ||
|
||
#include <condition_variable> | ||
#include <cstddef> | ||
#include <mutex> | ||
|
||
namespace BipedalLocomotion | ||
{ | ||
namespace System | ||
{ | ||
/** | ||
* Barrier provides a thread-coordination mechanism that allows an expected number of threads to | ||
* block until the expected number of threads arrive at the barrier. | ||
*/ | ||
class Barrier | ||
{ | ||
public: | ||
/** | ||
* Constructor. | ||
* @param counter initial value of the expected counter | ||
*/ | ||
explicit Barrier(const std::size_t counter); | ||
|
||
/** | ||
* Blocks this thread at the phase synchronization point until its phase completion step is run | ||
*/ | ||
void wait(); | ||
|
||
private: | ||
std::mutex m_mutex; | ||
std::condition_variable m_cond; | ||
std::size_t m_initialCount; | ||
std::size_t m_count; | ||
std::size_t m_generation; | ||
}; | ||
} // namespace System | ||
} // namespace BipedalLocomotion | ||
|
||
#endif // BIPEDAL_LOCOMOTION_SYSTEM_BARRIER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @file Barrier.cpp | ||
* @authors Giulio Romualdi | ||
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. | ||
*/ | ||
|
||
#include <mutex> | ||
|
||
#include <BipedalLocomotion/System/Barrier.h> | ||
|
||
using namespace BipedalLocomotion::System; | ||
|
||
Barrier::Barrier(const std::size_t counter) | ||
: m_initialCount(counter) | ||
, m_count(counter) | ||
, m_generation(0) | ||
{ | ||
} | ||
|
||
void Barrier::wait() | ||
{ | ||
std::unique_lock lock{m_mutex}; | ||
const auto tempGeneration = m_generation; | ||
if ((--m_count) == 1) | ||
{ | ||
// all threads reached the barrier, so we can consider them synchronized | ||
m_generation++; | ||
|
||
// reset the counter | ||
m_count = m_initialCount; | ||
|
||
// notify the other threads | ||
m_cond.notify_all(); | ||
} else | ||
{ | ||
// waiting for the other threads | ||
m_cond.wait(lock, [this, tempGeneration] { return tempGeneration != m_generation; }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters