Skip to content

Commit

Permalink
Use std::optional. Replace the custom binary semaphore by a POSIX sem… (
Browse files Browse the repository at this point in the history
#20)

* Use std::optional. Replace the custom binary semaphore by a POSIX semaphore.

* Remove useless include.
  • Loading branch information
mamaheux authored Apr 29, 2024
1 parent d33f82b commit f3e17af
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 146 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ include_directories(
## Declare a C++ library
add_library(${PROJECT_NAME}
src/hbba_lite_cpp/filters/FilterState.cpp
src/hbba_lite_cpp/utils/BinarySemaphore.cpp
src/hbba_lite_cpp/utils/HbbaLiteException.cpp
src/hbba_lite_cpp/core/Desire.cpp
src/hbba_lite_cpp/core/DesireSet.cpp
Expand Down Expand Up @@ -261,7 +260,6 @@ install(DIRECTORY include/${PROJECT_NAME}/

## Add gtest based cpp test target and link libraries
catkin_add_gtest(${PROJECT_NAME}-test
test/hbba_lite_cpp/utils/BinarySemaphoreTests.cpp
test/hbba_lite_cpp/core/DesireTests.cpp
test/hbba_lite_cpp/core/DesireSetTests.cpp
test/hbba_lite_cpp/core/StrategyTests.cpp
Expand Down
8 changes: 5 additions & 3 deletions include/hbba_lite/core/HbbaLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#define HBBA_LITE_CORE_HBBA_LITE_H

#include <hbba_lite/utils/ClassMacros.h>
#include <hbba_lite/utils/BinarySemaphore.h>

#include <hbba_lite/core/DesireSet.h>
#include <hbba_lite/core/Strategy.h>
#include <hbba_lite/core/Solver.h>
#include <hbba_lite/core/StrategyStateLogger.h>

#include <semaphore.h>

#include <atomic>
#include <unordered_map>
#include <memory>
Expand All @@ -17,6 +18,7 @@
#include <thread>
#include <mutex>
#include <set>
#include <optional>

template<>
struct std::hash<std::pair<DesireType, size_t>>
Expand All @@ -38,8 +40,8 @@ class HbbaLite : public DesireSetObserver
std::unique_ptr<StrategyStateLogger> m_strategyStateLogger;

std::mutex m_pendingDesiresMutex;
BinarySemaphore m_pendingDesiresSemaphore;
std::vector<std::unique_ptr<Desire>> m_pendingDesires;
sem_t m_pendingDesiresSemaphore; // TODO remplace with C++20 semaphore
std::optional<std::vector<std::unique_ptr<Desire>>> m_pendingDesires;

std::atomic_bool m_stopped;
std::unique_ptr<std::thread> m_thread;
Expand Down
9 changes: 5 additions & 4 deletions include/hbba_lite/core/Strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>
#include <mutex>
#include <memory>
#include <optional>

class BaseStrategy;

Expand Down Expand Up @@ -147,7 +148,7 @@ class FilterPool
class BaseStrategy
{
bool m_enabled;
uint64_t m_desireId;
std::optional<uint64_t> m_desireId;

uint16_t m_utility;
std::unordered_map<std::string, uint16_t> m_resourcesByName;
Expand All @@ -168,7 +169,7 @@ class BaseStrategy
DECLARE_NOT_MOVABLE(BaseStrategy);

uint16_t utility() const;
uint64_t desireId() const;
std::optional<uint64_t> desireId() const;

void enable(const Desire& desire);
void disable();
Expand All @@ -190,7 +191,7 @@ inline uint16_t BaseStrategy::utility() const
return m_utility;
}

inline uint64_t BaseStrategy::desireId() const
inline std::optional<uint64_t> BaseStrategy::desireId() const
{
return m_desireId;
}
Expand All @@ -210,7 +211,7 @@ inline void BaseStrategy::disable()
if (m_enabled)
{
m_enabled = false;
m_desireId = std::numeric_limits<uint64_t>::max();
m_desireId = std::nullopt;
onDisabling();
}
}
Expand Down
67 changes: 0 additions & 67 deletions include/hbba_lite/utils/BinarySemaphore.h

This file was deleted.

29 changes: 21 additions & 8 deletions src/hbba_lite_cpp/core/HbbaLite.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <hbba_lite/core/HbbaLite.h>
#include <hbba_lite/utils/HbbaLiteException.h>

#include <ctime>

using namespace std;

HbbaLite::HbbaLite(
Expand All @@ -13,9 +15,13 @@ HbbaLite::HbbaLite(
m_resourcesByNames(move(resourcesByNames)),
m_solver(move(solver)),
m_strategyStateLogger(move(strategyStateLogger)),
m_pendingDesiresSemaphore(false),
m_stopped(false)
{
if (sem_init(&m_pendingDesiresSemaphore, 0, 0) == -1)
{
throw HbbaLiteException("Semaphore init failed");
}

for (auto& strategy : strategies)
{
checkStrategyResources(strategy->desireType(), strategy->resourcesByName());
Expand All @@ -32,19 +38,21 @@ HbbaLite::~HbbaLite()

m_stopped.store(true);
m_thread->join();

sem_destroy(&m_pendingDesiresSemaphore);
}

void HbbaLite::onDesireSetChanged(const vector<unique_ptr<Desire>>& enabledDesires)
{
lock_guard<mutex> lock(m_pendingDesiresMutex);
m_pendingDesires.clear();
m_pendingDesires = vector<unique_ptr<Desire>>();

for (auto& enabledDesire : enabledDesires)
{
m_pendingDesires.emplace_back(enabledDesire->clone());
m_pendingDesires->emplace_back(enabledDesire->clone());
}

m_pendingDesiresSemaphore.release();
sem_post(&m_pendingDesiresSemaphore);
}

void HbbaLite::checkStrategyResources(DesireType desireType, const unordered_map<string, uint16_t>& resourcesByNames)
Expand All @@ -69,18 +77,23 @@ void HbbaLite::checkStrategyResources(DesireType desireType, const unordered_map

void HbbaLite::run()
{
constexpr chrono::milliseconds SEMAPHORE_WAIT_DURATION(10);
struct timespec waitDuration;
waitDuration.tv_sec = 0;
waitDuration.tv_nsec = 10'000'000;

while (!m_stopped.load())
{
if (m_pendingDesiresSemaphore.tryAcquireFor(SEMAPHORE_WAIT_DURATION))
if (sem_timedwait(&m_pendingDesiresSemaphore, &waitDuration) == 0)
{
vector<unique_ptr<Desire>> desires;
optional<vector<unique_ptr<Desire>>> desires;
{
lock_guard<mutex> lock(m_pendingDesiresMutex);
swap(m_pendingDesires, desires);
}
updateStrategies(move(desires));
if (desires.has_value())
{
updateStrategies(move(*desires));
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/hbba_lite_cpp/core/Strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ BaseStrategy::BaseStrategy(
unordered_map<string, FilterConfiguration> filterConfigurationsByName,
shared_ptr<FilterPool> filterPool)
: m_enabled(false),
m_desireId(std::numeric_limits<uint64_t>::max()),
m_desireId(nullopt),
m_utility(utility),
m_resourcesByName(move(resourcesByName)),
m_filterConfigurationsByName(move(filterConfigurationsByName)),
Expand Down
3 changes: 0 additions & 3 deletions src/hbba_lite_cpp/utils/BinarySemaphore.cpp

This file was deleted.

58 changes: 0 additions & 58 deletions test/hbba_lite_cpp/utils/BinarySemaphoreTests.cpp

This file was deleted.

0 comments on commit f3e17af

Please sign in to comment.