Skip to content

Commit

Permalink
fix: data races when running PGNRequestExampleTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
GwnDaan committed Feb 6, 2025
1 parent a71c186 commit 7ca7dfd
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace isobus
void receive_thread_function();

std::unique_ptr<std::thread> receiveMessageThread; ///< Thread to manage getting messages from a CAN channel
bool receiveThreadRunning = false; ///< Flag to indicate if the receive thread is running
std::atomic_bool receiveThreadRunning = { false }; ///< Flag to indicate if the receive thread is running
#endif

std::shared_ptr<CANHardwarePlugin> frameHandler; ///< The CAN driver to use for a CAN channel
Expand Down Expand Up @@ -190,7 +190,7 @@ namespace isobus
static std::vector<std::unique_ptr<CANHardware>> hardwareChannels; ///< A list of all CAN channel's metadata
static Mutex hardwareChannelsMutex; ///< Mutex to protect `hardwareChannels`
static Mutex updateMutex; ///< A mutex for the main thread
static bool started; ///< Stores if the threads have been started
static std::atomic_bool started; ///< Stores if the threads have been started
};
}
#endif // CAN_HARDWARE_INTERFACE_HPP
2 changes: 1 addition & 1 deletion hardware_integration/src/can_hardware_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace isobus
std::vector<std::unique_ptr<CANHardwareInterface::CANHardware>> CANHardwareInterface::hardwareChannels;
Mutex CANHardwareInterface::hardwareChannelsMutex;
Mutex CANHardwareInterface::updateMutex;
bool CANHardwareInterface::started = false;
std::atomic_bool CANHardwareInterface::started = { false };

CANHardwareInterface CANHardwareInterface::SINGLETON;

Expand Down
2 changes: 1 addition & 1 deletion isobus/include/isobus/isobus/can_control_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace isobus
const Type controlFunctionType; ///< The Type of the control function
NAME controlFunctionNAME; ///< The NAME of the control function
bool claimedAddressSinceLastAddressClaimRequest = false; ///< Used to mark CFs as stale if they don't claim within a certain time
std::uint8_t address; ///< The address of the control function
std::atomic_uint8_t address; ///< The address of the control function
const std::uint8_t canPortIndex; ///< The CAN channel index of the control function
};

Expand Down
1 change: 1 addition & 0 deletions isobus/include/isobus/isobus/can_network_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ namespace isobus
std::array<std::array<std::shared_ptr<ControlFunction>, NULL_CAN_ADDRESS>, CAN_PORT_MAXIMUM> controlFunctionTable; ///< Table to maintain address to NAME mappings
std::list<std::shared_ptr<ControlFunction>> inactiveControlFunctions; ///< A list of the control function that currently don't have a valid address
std::list<std::shared_ptr<InternalControlFunction>> internalControlFunctions; ///< A list of the internal control functions
Mutex internalControlFunctionsMutex; ///< A mutex for internal control functions thread safety
std::list<std::shared_ptr<PartneredControlFunction>> partneredControlFunctions; ///< A list of the partnered control functions

std::list<ParameterGroupNumberCallbackData> protocolPGNCallbacks; ///< A list of PGN callback registered by CAN protocols
Expand Down
2 changes: 2 additions & 0 deletions isobus/src/can_network_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace isobus

std::shared_ptr<InternalControlFunction> CANNetworkManager::create_internal_control_function(NAME desiredName, std::uint8_t CANPort, std::uint8_t preferredAddress)
{
LOCK_GUARD(Mutex, internalControlFunctionsMutex);
auto controlFunction = std::make_shared<InternalControlFunction>(desiredName, preferredAddress, CANPort);
controlFunction->pgnRequestProtocol.reset(new ParameterGroupNumberRequestProtocol(controlFunction));
internalControlFunctions.push_back(controlFunction);
Expand Down Expand Up @@ -606,6 +607,7 @@ namespace isobus

void CANNetworkManager::update_internal_cfs()
{
LOCK_GUARD(Mutex, internalControlFunctionsMutex);
for (const auto &currentInternalControlFunction : internalControlFunctions)
{
if (currentInternalControlFunction->update_address_claiming())
Expand Down

0 comments on commit 7ca7dfd

Please sign in to comment.