[wpilib] Fix deadlocks in Mechanism2d et al. (#3770)

UpdateEntries() and Flush() are called from methods that lock the mutex,
so locking it again will cause deadlocks. This also updates the Java
code to make MechanismObject2d::update synchronized like in the C++
version.
This commit is contained in:
Prateek Machiraju
2021-12-06 17:42:02 -05:00
committed by GitHub
parent acb64dff97
commit d81ef2bc5c
6 changed files with 8 additions and 7 deletions

View File

@@ -15,6 +15,10 @@
<Match>
<Bug pattern="EI_EXPOSE_REP2" />
</Match>
<Match>
<Bug pattern="IS2_INCONSISTENT_SYNC" />
<Source name="MechanismLigament2d.java" />
</Match>
<Match>
<Bug pattern="MS_CANNOT_BE_FINAL" />
<Class name="edu.wpi.first.wpilibj.examples.pacgoat.Robot" />

View File

@@ -21,7 +21,6 @@ MechanismLigament2d::MechanismLigament2d(std::string_view name, double length,
void MechanismLigament2d::UpdateEntries(
std::shared_ptr<nt::NetworkTable> table) {
std::scoped_lock lock(m_mutex);
table->GetEntry(".type").SetString("line");
m_colorEntry = table->GetEntry("color");

View File

@@ -20,14 +20,12 @@ void MechanismRoot2d::SetPosition(double x, double y) {
}
void MechanismRoot2d::UpdateEntries(std::shared_ptr<nt::NetworkTable> table) {
std::scoped_lock lock(m_mutex);
m_xEntry = table->GetEntry("x");
m_yEntry = table->GetEntry("y");
Flush();
}
inline void MechanismRoot2d::Flush() {
std::scoped_lock lock(m_mutex);
if (m_xEntry) {
m_xEntry.SetDouble(m_x);
}

View File

@@ -128,7 +128,7 @@ public class MechanismLigament2d extends MechanismObject2d {
}
@Override
protected synchronized void updateEntries(NetworkTable table) {
protected void updateEntries(NetworkTable table) {
table.getEntry(".type").setString("line");
m_angleEntry = table.getEntry("angle");
m_lengthEntry = table.getEntry("length");
@@ -138,7 +138,7 @@ public class MechanismLigament2d extends MechanismObject2d {
}
/** Flush latest data to NT. */
private synchronized void flush() {
private void flush() {
if (m_angleEntry != null) {
m_angleEntry.setDouble(m_angle);
}

View File

@@ -52,7 +52,7 @@ public abstract class MechanismObject2d {
return object;
}
final void update(NetworkTable table) {
final synchronized void update(NetworkTable table) {
m_table = table;
updateEntries(m_table);
for (MechanismObject2d obj : m_objects.values()) {

View File

@@ -86,7 +86,7 @@ public final class MechanismRoot2d {
return m_name;
}
private synchronized void flush() {
private void flush() {
if (m_xEntry != null) {
m_xEntry.setDouble(m_x);
}