Make Sendable setters synchronous (#1799)

Instead of being called asynchronously by NetworkTables, they are now called by updateValues() synchronously with the main loop, just like the getters.
This commit is contained in:
Oblarg
2019-08-03 18:08:06 -04:00
committed by Peter Johnson
parent c67a488a09
commit fbe67c90c8
8 changed files with 194 additions and 23 deletions

View File

@@ -0,0 +1,41 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <functional>
#include <vector>
#include <wpi/mutex.h>
namespace frc::detail {
/**
* An executor for running listener tasks posted by Sendable listeners
* synchronously from the main application thread.
*
* @see Sendable
*/
class ListenerExecutor {
public:
/**
* Posts a task to the executor to be run synchronously from the main thread.
*
* @param task The task to run synchronously from the main thread.
*/
void Execute(std::function<void()> task);
/**
* Runs all posted tasks. Called periodically from main thread.
*/
void RunListenerTasks();
private:
std::vector<std::function<void()>> m_tasks;
std::vector<std::function<void()>> m_runningTasks;
wpi::mutex m_lock;
};
} // namespace frc::detail

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2011-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2011-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. */
@@ -14,6 +14,7 @@
#include <networktables/NetworkTableValue.h>
#include "frc/ErrorBase.h"
#include "frc/smartdashboard/ListenerExecutor.h"
#include "frc/smartdashboard/SendableBase.h"
namespace frc {
@@ -401,6 +402,15 @@ class SmartDashboard : public ErrorBase, public SendableBase {
*/
static std::shared_ptr<nt::Value> GetValue(wpi::StringRef keyName);
/**
* Posts a task from a listener to the ListenerExecutor, so that it can be run
* synchronously from the main loop on the next call to {@link
* SmartDashboard#updateValues()}.
*
* @param task The task to run synchronously from the main thread.
*/
static void PostListenerTask(std::function<void()> task);
/**
* Puts all sendable data to the dashboard.
*/
@@ -408,6 +418,8 @@ class SmartDashboard : public ErrorBase, public SendableBase {
private:
virtual ~SmartDashboard() = default;
static detail::ListenerExecutor listenerExecutor;
};
} // namespace frc