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 956dd8a commit aff63d0
Show file tree
Hide file tree
Showing 44 changed files with 508 additions and 355 deletions.
1 change: 0 additions & 1 deletion Tests/.clang-format → Examples/Elevator/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ BreakBeforeBinaryOperators: false
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: true
#BreakInheritanceList: true
ColumnLimit: 115
CommentPragmas: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
Expand Down
44 changes: 22 additions & 22 deletions Examples/Elevator/Controller/ElevatorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,28 @@
#include "Controller/ElevatorController.h"
#include "Controller/ElevatorPetriNet.h"

#include <iostream>
#include <algorithm>
#include <iostream>
#include <mutex>

using namespace std;

ElevatorController::ElevatorController()
: m_pPetriNet(nullptr)
, m_currentFloor(s_bottomFloor)
, m_toAddToDestination(s_bottomFloor)
, m_destinations()
, m_nextTravelDestinations()
, m_floorsWaitingToGoUp()
, m_floorsWaitingToGoDown()
: m_pPetriNet(nullptr)
, m_currentFloor(s_bottomFloor)
, m_toAddToDestination(s_bottomFloor)
, m_destinations()
, m_nextTravelDestinations()
, m_floorsWaitingToGoUp()
, m_floorsWaitingToGoDown()
{
m_pPetriNet = make_unique<ElevatorPetriNet>(*this);
m_pPetriNet->execute(false);
}

ElevatorController::~ElevatorController()
{}
{
}

void ElevatorController::checkPetriNetPointer() const
{
Expand Down Expand Up @@ -106,7 +107,7 @@ void ElevatorController::stop()
m_pPetriNet->stop();
}

//Actions
// Actions
void ElevatorController::addDestination1()
{
unique_lock<shared_mutex> l(m_mutex);
Expand All @@ -126,7 +127,7 @@ void ElevatorController::addDestination2()
m_floorsWaitingToGoUp.erase(m_toAddToDestination);

m_nextTravelDestinations.insert(m_toAddToDestination);

printNextDestinations();
}

Expand Down Expand Up @@ -190,7 +191,7 @@ void ElevatorController::removeCurrentFromWaitingToGoUp()
void ElevatorController::rotateLists()
{
unique_lock<shared_mutex> l(m_mutex);
swap(m_destinations, m_nextTravelDestinations);
swap(m_destinations, m_nextTravelDestinations);
m_destinations.erase(m_currentFloor);
}

Expand Down Expand Up @@ -229,7 +230,6 @@ void ElevatorController::mergeMinGoingUp()

m_nextTravelDestinations.insert(m_floorsWaitingToGoUp.begin(), m_floorsWaitingToGoUp.end());
m_floorsWaitingToGoUp.clear();

}

void ElevatorController::mergeMaxGoingDown()
Expand All @@ -245,7 +245,6 @@ void ElevatorController::mergeMaxGoingDown()

m_nextTravelDestinations.insert(m_floorsWaitingToGoDown.begin(), m_floorsWaitingToGoDown.end());
m_floorsWaitingToGoDown.clear();

}

void ElevatorController::mergePostponedToCurrent()
Expand All @@ -263,18 +262,19 @@ void ElevatorController::mergeGoingDownSTCurrent()
mergeToDestinations(m_floorsWaitingToGoDown, true);
}

void ElevatorController::mergeToDestinations(unordered_set<int>& toAdd, const bool lessThan)
void ElevatorController::mergeToDestinations(unordered_set<int> &toAdd, const bool lessThan)
{
bool deleted = false;
do
{
deleted = false;
auto floorIt = std::find_if(toAdd.cbegin(), toAdd.cend(), [this, &lessThan](const int floor) {
return (lessThan && floor < m_currentFloor) || (!lessThan && floor > m_currentFloor);
});
auto floorIt =
std::find_if(toAdd.cbegin(), toAdd.cend(),
[this, &lessThan](const int floor)
{ return (lessThan && floor < m_currentFloor) || (!lessThan && floor > m_currentFloor); });
if (floorIt != toAdd.cend())
{
auto& floor = *floorIt;
auto &floor = *floorIt;
m_destinations.insert(floor);
toAdd.erase(floor);
deleted = true;
Expand Down Expand Up @@ -374,7 +374,7 @@ bool ElevatorController::isMaxGreaterThanCurrent() const
}


//Info
// Info

void ElevatorController::elevatorStopped()
{
Expand Down Expand Up @@ -440,14 +440,14 @@ void ElevatorController::printWaitingGoUp() const
printFloorList(m_floorsWaitingToGoUp);
}

void ElevatorController::printFloorList(const unordered_set<int>& floors) const
void ElevatorController::printFloorList(const unordered_set<int> &floors) const
{
if (floors.empty())
{
cout << "Empty" << endl;
return;
}
for (const auto& d : floors)
for (const auto &d : floors)
{
cout << d << " ";
}
Expand Down
26 changes: 11 additions & 15 deletions Examples/Elevator/Controller/ElevatorController.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#pragma once

#include <memory>
#include <unordered_set>
#include <shared_mutex>
#include <unordered_set>

class IElevatorPetriNet;

Expand All @@ -30,7 +30,6 @@ class ElevatorController
friend class ElevatorPetriNet;

public:

//! Constructor.
ElevatorController();

Expand Down Expand Up @@ -58,7 +57,6 @@ class ElevatorController
static const int s_topFloor = 10;

private:

mutable std::shared_mutex m_mutex;

//! Base (nested) class for a Petri net based state machine.
Expand All @@ -80,7 +78,7 @@ class ElevatorController

//! List of destinations in the current travel.
std::unordered_set<int> m_destinations;

//! List of destinations for the next travel.
std::unordered_set<int> m_nextTravelDestinations;

Expand All @@ -91,7 +89,7 @@ class ElevatorController
std::unordered_set<int> m_floorsWaitingToGoDown;

//! Merge set to m_destinations and delete the merged items.
void mergeToDestinations(std::unordered_set<int>& toAdd, const bool lessThan);
void mergeToDestinations(std::unordered_set<int> &toAdd, const bool lessThan);

//! Remove current floor from m_floorsWaitingToGoDown
void removeCurrentFromWaitingToGoDown();
Expand All @@ -102,19 +100,19 @@ class ElevatorController
//! Check if pointer to PTN is valid.
void checkPetriNetPointer() const;

//Information
// Information
void printCurrentFloor() const;
void printDestinations() const;
void printNextDestinations() const;
void printWaitingGoDown() const;
void printWaitingGoUp() const;
void printFloorList(const std::unordered_set<int>& floors) const;
void printFloorList(const std::unordered_set<int> &floors) const;
void printSchedule() const;

////////////////////
//Methods used by the petri net
//Actions
// Methods used by the petri net

// Actions
void removeDestinationGU();
void removeDestinationGD();
void removeDestination();
Expand All @@ -129,7 +127,7 @@ class ElevatorController
void mergeMinGoingUp();
void mergeMaxGoingDown();
void mergePostponedToCurrent();
void mergeGoingDownSTCurrent();
void mergeGoingDownSTCurrent();
void processedLists();
void elevatorStopped();
void elevatorMoving();
Expand All @@ -138,7 +136,7 @@ class ElevatorController
void goingUp();
void goingDown();

//Conditions
// Conditions
bool isFloorNotInList() const;
bool isFloorInList() const;
bool isDestinationListNotEmpty() const;
Expand All @@ -150,6 +148,4 @@ class ElevatorController
bool isMinGreaterThanCurrent() const;
bool isMaxSmallerThanCurrent() const;
bool isMaxGreaterThanCurrent() const;

};

Loading

0 comments on commit aff63d0

Please sign in to comment.