Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hal] Fix potential race in CANAPI #6819

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 11 additions & 25 deletions hal/src/main/native/athena/CANAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ctime>

#include <wpi/DenseMap.h>
#include <wpi/mutex.h>

#include "HALInitializer.h"
#include "hal/CAN.h"
Expand All @@ -26,8 +27,9 @@ struct CANStorage {
HAL_CANManufacturer manufacturer;
HAL_CANDeviceType deviceType;
uint8_t deviceId;
wpi::mutex mapMutex;
wpi::mutex periodicSendsMutex;
wpi::SmallDenseMap<int32_t, int32_t> periodicSends;
wpi::mutex receivesMutex;
wpi::SmallDenseMap<int32_t, Receives> receives;
};
} // namespace
Expand Down Expand Up @@ -89,7 +91,7 @@ void HAL_CleanCAN(HAL_CANHandle handle) {
return;
}

std::scoped_lock lock(data->mapMutex);
std::scoped_lock lock(data->periodicSendsMutex);

for (auto&& i : data->periodicSends) {
int32_t s = 0;
Expand All @@ -108,12 +110,8 @@ void HAL_WriteCANPacket(HAL_CANHandle handle, const uint8_t* data,
}
auto id = CreateCANId(can.get(), apiId);

std::scoped_lock lock(can->periodicSendsMutex);
HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status);

if (*status != 0) {
return;
}
std::scoped_lock lock(can->mapMutex);
can->periodicSends[apiId] = -1;
}

Expand All @@ -127,12 +125,8 @@ void HAL_WriteCANPacketRepeating(HAL_CANHandle handle, const uint8_t* data,
}
auto id = CreateCANId(can.get(), apiId);

std::scoped_lock lock(can->periodicSendsMutex);
HAL_CAN_SendMessage(id, data, length, repeatMs, status);

if (*status != 0) {
return;
}
std::scoped_lock lock(can->mapMutex);
can->periodicSends[apiId] = repeatMs;
}

Expand All @@ -148,12 +142,8 @@ void HAL_WriteCANRTRFrame(HAL_CANHandle handle, int32_t length, int32_t apiId,
uint8_t data[8];
std::memset(data, 0, sizeof(data));

std::scoped_lock lock(can->periodicSendsMutex);
HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status);

if (*status != 0) {
return;
}
std::scoped_lock lock(can->mapMutex);
can->periodicSends[apiId] = -1;
}

Expand All @@ -166,13 +156,9 @@ void HAL_StopCANPacketRepeating(HAL_CANHandle handle, int32_t apiId,
}
auto id = CreateCANId(can.get(), apiId);

std::scoped_lock lock(can->periodicSendsMutex);
HAL_CAN_SendMessage(id, nullptr, 0, HAL_CAN_SEND_PERIOD_STOP_REPEATING,
status);

if (*status != 0) {
return;
}
std::scoped_lock lock(can->mapMutex);
can->periodicSends[apiId] = -1;
}

Expand All @@ -191,7 +177,7 @@ void HAL_ReadCANPacketNew(HAL_CANHandle handle, int32_t apiId, uint8_t* data,
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);

if (*status == 0) {
std::scoped_lock lock(can->mapMutex);
std::scoped_lock lock(can->receivesMutex);
auto& msg = can->receives[messageId];
msg.length = dataSize;
msg.lastTimeStamp = ts;
Expand All @@ -216,7 +202,7 @@ void HAL_ReadCANPacketLatest(HAL_CANHandle handle, int32_t apiId, uint8_t* data,
uint32_t ts = 0;
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);

std::scoped_lock lock(can->mapMutex);
std::scoped_lock lock(can->receivesMutex);
if (*status == 0) {
// fresh update
auto& msg = can->receives[messageId];
Expand Down Expand Up @@ -253,7 +239,7 @@ void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId,
uint32_t ts = 0;
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);

std::scoped_lock lock(can->mapMutex);
std::scoped_lock lock(can->receivesMutex);
if (*status == 0) {
// fresh update
auto& msg = can->receives[messageId];
Expand Down
35 changes: 10 additions & 25 deletions hal/src/main/native/sim/CANAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ struct CANStorage {
HAL_CANManufacturer manufacturer;
HAL_CANDeviceType deviceType;
uint8_t deviceId;
wpi::mutex mapMutex;
wpi::mutex periodicSendsMutex;
wpi::SmallDenseMap<int32_t, int32_t> periodicSends;
wpi::mutex receivesMutex;
wpi::SmallDenseMap<int32_t, Receives> receives;
};
} // namespace
Expand Down Expand Up @@ -97,7 +98,7 @@ void HAL_CleanCAN(HAL_CANHandle handle) {
return;
}

std::scoped_lock lock(data->mapMutex);
std::scoped_lock lock(data->periodicSendsMutex);

for (auto&& i : data->periodicSends) {
int32_t s = 0;
Expand All @@ -116,12 +117,8 @@ void HAL_WriteCANPacket(HAL_CANHandle handle, const uint8_t* data,
}
auto id = CreateCANId(can.get(), apiId);

std::scoped_lock lock(can->periodicSendsMutex);
HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status);

if (*status != 0) {
return;
}
std::scoped_lock lock(can->mapMutex);
can->periodicSends[apiId] = -1;
}

Expand All @@ -135,12 +132,8 @@ void HAL_WriteCANPacketRepeating(HAL_CANHandle handle, const uint8_t* data,
}
auto id = CreateCANId(can.get(), apiId);

std::scoped_lock lock(can->periodicSendsMutex);
HAL_CAN_SendMessage(id, data, length, repeatMs, status);

if (*status != 0) {
return;
}
std::scoped_lock lock(can->mapMutex);
can->periodicSends[apiId] = repeatMs;
}

Expand All @@ -156,12 +149,8 @@ void HAL_WriteCANRTRFrame(HAL_CANHandle handle, int32_t length, int32_t apiId,
uint8_t data[8];
std::memset(data, 0, sizeof(data));

std::scoped_lock lock(can->periodicSendsMutex);
HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status);

if (*status != 0) {
return;
}
std::scoped_lock lock(can->mapMutex);
can->periodicSends[apiId] = -1;
}

Expand All @@ -174,13 +163,9 @@ void HAL_StopCANPacketRepeating(HAL_CANHandle handle, int32_t apiId,
}
auto id = CreateCANId(can.get(), apiId);

std::scoped_lock lock(can->periodicSendsMutex);
HAL_CAN_SendMessage(id, nullptr, 0, HAL_CAN_SEND_PERIOD_STOP_REPEATING,
status);

if (*status != 0) {
return;
}
std::scoped_lock lock(can->mapMutex);
can->periodicSends[apiId] = -1;
}

Expand All @@ -199,7 +184,7 @@ void HAL_ReadCANPacketNew(HAL_CANHandle handle, int32_t apiId, uint8_t* data,
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);

if (*status == 0) {
std::scoped_lock lock(can->mapMutex);
std::scoped_lock lock(can->receivesMutex);
auto& msg = can->receives[messageId];
msg.length = dataSize;
msg.lastTimeStamp = ts;
Expand All @@ -224,7 +209,7 @@ void HAL_ReadCANPacketLatest(HAL_CANHandle handle, int32_t apiId, uint8_t* data,
uint32_t ts = 0;
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);

std::scoped_lock lock(can->mapMutex);
std::scoped_lock lock(can->receivesMutex);
if (*status == 0) {
// fresh update
auto& msg = can->receives[messageId];
Expand Down Expand Up @@ -261,7 +246,7 @@ void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId,
uint32_t ts = 0;
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);

std::scoped_lock lock(can->mapMutex);
std::scoped_lock lock(can->receivesMutex);
if (*status == 0) {
// fresh update
auto& msg = can->receives[messageId];
Expand Down
Loading