mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
NetworkTable: Protect listeners with mutex.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#ifndef _NETWORKTABLE_H_
|
||||
#define _NETWORKTABLE_H_
|
||||
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include "tables/ITable.h"
|
||||
@@ -10,6 +11,7 @@ class NetworkTable : public ITable {
|
||||
struct private_init {};
|
||||
|
||||
std::string m_path;
|
||||
std::mutex m_mutex;
|
||||
typedef std::pair<ITableListener*, unsigned int> Listener;
|
||||
std::vector<Listener> m_listeners;
|
||||
|
||||
|
||||
@@ -65,6 +65,7 @@ void NetworkTable::AddTableListener(ITableListener* listener) {
|
||||
|
||||
void NetworkTable::AddTableListener(ITableListener* listener,
|
||||
bool immediateNotify) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
llvm::SmallString<128> path(m_path);
|
||||
path += PATH_SEPARATOR_CHAR;
|
||||
std::size_t prefix_len = path.size();
|
||||
@@ -83,6 +84,7 @@ void NetworkTable::AddTableListener(ITableListener* listener,
|
||||
void NetworkTable::AddTableListener(StringRef key,
|
||||
ITableListener* listener,
|
||||
bool immediateNotify) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
llvm::SmallString<128> path(m_path);
|
||||
path += PATH_SEPARATOR_CHAR;
|
||||
std::size_t prefix_len = path.size();
|
||||
@@ -99,6 +101,7 @@ void NetworkTable::AddTableListener(StringRef key,
|
||||
}
|
||||
|
||||
void NetworkTable::RemoveTableListener(ITableListener* listener) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
auto matches_begin =
|
||||
std::remove_if(m_listeners.begin(), m_listeners.end(),
|
||||
[=](const Listener& x) { return x.first == listener; });
|
||||
|
||||
Reference in New Issue
Block a user