SendableBase: remove unnecessary synchronization (#1797)

Also fixes the move constructor to update LiveWindow to follow the move.
This commit is contained in:
Oblarg
2019-08-03 02:47:17 -04:00
committed by Peter Johnson
parent e6d348f382
commit 3b12276bc3
5 changed files with 33 additions and 37 deletions

View File

@@ -81,9 +81,9 @@ void LiveWindow::AddChild(Sendable* parent, void* child) {
comp.telemetryEnabled = false;
}
void LiveWindow::Remove(Sendable* sendable) {
bool LiveWindow::Remove(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
m_impl->components.erase(sendable);
return m_impl->components.erase(sendable);
}
void LiveWindow::EnableTelemetry(Sendable* sendable) {

View File

@@ -17,39 +17,35 @@ SendableBase::SendableBase(bool addLiveWindow) {
if (addLiveWindow) LiveWindow::GetInstance()->Add(this);
}
SendableBase::~SendableBase() { LiveWindow::GetInstance()->Remove(this); }
SendableBase::SendableBase(SendableBase&& rhs) {
m_name = std::move(rhs.m_name);
m_subsystem = std::move(rhs.m_subsystem);
SendableBase::SendableBase(SendableBase&& other)
: m_name(std::move(other.m_name)),
m_subsystem(std::move(other.m_subsystem)) {
auto&& lw = LiveWindow::GetInstance();
if (lw->Remove(&other)) {
lw->Add(this);
}
}
SendableBase& SendableBase::operator=(SendableBase&& rhs) {
Sendable::operator=(std::move(rhs));
m_name = std::move(rhs.m_name);
m_subsystem = std::move(rhs.m_subsystem);
SendableBase& SendableBase::operator=(SendableBase&& other) {
m_name = std::move(other.m_name);
m_subsystem = std::move(other.m_subsystem);
auto&& lw = LiveWindow::GetInstance();
if (lw->Remove(&other)) {
lw->Add(this);
}
return *this;
}
std::string SendableBase::GetName() const {
std::scoped_lock lock(m_mutex);
return m_name;
}
SendableBase::~SendableBase() { LiveWindow::GetInstance()->Remove(this); }
void SendableBase::SetName(const wpi::Twine& name) {
std::scoped_lock lock(m_mutex);
m_name = name.str();
}
std::string SendableBase::GetName() const { return m_name; }
std::string SendableBase::GetSubsystem() const {
std::scoped_lock lock(m_mutex);
return m_subsystem;
}
void SendableBase::SetName(const wpi::Twine& name) { m_name = name.str(); }
std::string SendableBase::GetSubsystem() const { return m_subsystem; }
void SendableBase::SetSubsystem(const wpi::Twine& subsystem) {
std::scoped_lock lock(m_mutex);
m_subsystem = subsystem.str();
}

View File

@@ -67,8 +67,9 @@ class LiveWindow {
* Remove the component from the LiveWindow.
*
* @param sendable component to remove
* @return true if the component was removed; false if it was not present
*/
void Remove(Sendable* component);
bool Remove(Sendable* component);
/**
* Enable telemetry for a single component.

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -10,8 +10,6 @@
#include <memory>
#include <string>
#include <wpi/mutex.h>
#include "frc/smartdashboard/Sendable.h"
namespace frc {
@@ -27,8 +25,10 @@ class SendableBase : public Sendable {
~SendableBase() override;
SendableBase(SendableBase&& rhs);
SendableBase& operator=(SendableBase&& rhs);
SendableBase(const SendableBase&) = default;
SendableBase& operator=(const SendableBase&) = default;
SendableBase(SendableBase&&);
SendableBase& operator=(SendableBase&&);
using Sendable::SetName;
@@ -73,7 +73,6 @@ class SendableBase : public Sendable {
void SetName(const wpi::Twine& moduleType, int moduleNumber, int channel);
private:
mutable wpi::mutex m_mutex;
std::string m_name;
std::string m_subsystem = "Ungrouped";
};

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -46,12 +46,12 @@ public abstract class SendableBase implements Sendable, AutoCloseable {
}
@Override
public final synchronized String getName() {
public final String getName() {
return m_name;
}
@Override
public final synchronized void setName(String name) {
public final void setName(String name) {
m_name = name;
}
@@ -77,12 +77,12 @@ public abstract class SendableBase implements Sendable, AutoCloseable {
}
@Override
public final synchronized String getSubsystem() {
public final String getSubsystem() {
return m_subsystem;
}
@Override
public final synchronized void setSubsystem(String subsystem) {
public final void setSubsystem(String subsystem) {
m_subsystem = subsystem;
}