mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Replace std::lock_guard and std::lock with std::scoped_lock (#1758)
std::scoped_lock was introduced in C++17 and is strictly better than std::lock_guard as it supports locking any number of mutexes safely. It's also easier to use than std::lock for locking multiple mutexes at once.
This commit is contained in:
committed by
Peter Johnson
parent
24d31df55a
commit
62be0392b6
@@ -74,7 +74,7 @@ class CallbackThread : public wpi::SafeThread {
|
||||
struct Poller {
|
||||
void Terminate() {
|
||||
{
|
||||
std::lock_guard lock(poll_mutex);
|
||||
std::scoped_lock lock(poll_mutex);
|
||||
terminating = true;
|
||||
}
|
||||
poll_cond.notify_all();
|
||||
@@ -94,7 +94,7 @@ class CallbackThread : public wpi::SafeThread {
|
||||
auto poller = m_pollers[poller_uid];
|
||||
if (!poller) return;
|
||||
{
|
||||
std::lock_guard lock(poller->poll_mutex);
|
||||
std::scoped_lock lock(poller->poll_mutex);
|
||||
poller->poll_queue.emplace(std::forward<Args>(args)...);
|
||||
}
|
||||
poller->poll_cond.notify_one();
|
||||
@@ -286,7 +286,7 @@ class CallbackManager {
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard lock(poller->poll_mutex);
|
||||
std::scoped_lock lock(poller->poll_mutex);
|
||||
poller->cancelling = true;
|
||||
}
|
||||
poller->poll_cond.notify_one();
|
||||
|
||||
@@ -119,7 +119,7 @@ void DispatcherBase::StartServer(
|
||||
const Twine& persist_filename,
|
||||
std::unique_ptr<wpi::NetworkAcceptor> acceptor) {
|
||||
{
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
if (m_active) return;
|
||||
m_active = true;
|
||||
}
|
||||
@@ -151,7 +151,7 @@ void DispatcherBase::StartServer(
|
||||
|
||||
void DispatcherBase::StartClient() {
|
||||
{
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
if (m_active) return;
|
||||
m_active = true;
|
||||
}
|
||||
@@ -170,7 +170,7 @@ void DispatcherBase::Stop() {
|
||||
|
||||
// wake up client thread with a reconnect
|
||||
{
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
m_client_connector = nullptr;
|
||||
}
|
||||
ClientReconnect();
|
||||
@@ -184,7 +184,7 @@ void DispatcherBase::Stop() {
|
||||
|
||||
std::vector<std::shared_ptr<INetworkConnection>> conns;
|
||||
{
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
conns.swap(m_connections);
|
||||
}
|
||||
|
||||
@@ -202,14 +202,14 @@ void DispatcherBase::SetUpdateRate(double interval) {
|
||||
}
|
||||
|
||||
void DispatcherBase::SetIdentity(const Twine& name) {
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
m_identity = name.str();
|
||||
}
|
||||
|
||||
void DispatcherBase::Flush() {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
{
|
||||
std::lock_guard lock(m_flush_mutex);
|
||||
std::scoped_lock lock(m_flush_mutex);
|
||||
// don't allow flushes more often than every 10 ms
|
||||
if ((now - m_last_flush) < std::chrono::milliseconds(10)) return;
|
||||
m_last_flush = now;
|
||||
@@ -222,7 +222,7 @@ std::vector<ConnectionInfo> DispatcherBase::GetConnections() const {
|
||||
std::vector<ConnectionInfo> conns;
|
||||
if (!m_active) return conns;
|
||||
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
for (auto& conn : m_connections) {
|
||||
if (conn->state() != NetworkConnection::kActive) continue;
|
||||
conns.emplace_back(conn->info());
|
||||
@@ -234,7 +234,7 @@ std::vector<ConnectionInfo> DispatcherBase::GetConnections() const {
|
||||
bool DispatcherBase::IsConnected() const {
|
||||
if (!m_active) return false;
|
||||
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
for (auto& conn : m_connections) {
|
||||
if (conn->state() == NetworkConnection::kActive) return true;
|
||||
}
|
||||
@@ -245,7 +245,7 @@ bool DispatcherBase::IsConnected() const {
|
||||
unsigned int DispatcherBase::AddListener(
|
||||
std::function<void(const ConnectionNotification& event)> callback,
|
||||
bool immediate_notify) const {
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
unsigned int uid = m_notifier.Add(callback);
|
||||
// perform immediate notifications
|
||||
if (immediate_notify) {
|
||||
@@ -259,7 +259,7 @@ unsigned int DispatcherBase::AddListener(
|
||||
|
||||
unsigned int DispatcherBase::AddPolledListener(unsigned int poller_uid,
|
||||
bool immediate_notify) const {
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
unsigned int uid = m_notifier.AddPolled(poller_uid);
|
||||
// perform immediate notifications
|
||||
if (immediate_notify) {
|
||||
@@ -272,17 +272,17 @@ unsigned int DispatcherBase::AddPolledListener(unsigned int poller_uid,
|
||||
}
|
||||
|
||||
void DispatcherBase::SetConnector(Connector connector) {
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
m_client_connector = std::move(connector);
|
||||
}
|
||||
|
||||
void DispatcherBase::SetConnectorOverride(Connector connector) {
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
m_client_connector_override = std::move(connector);
|
||||
}
|
||||
|
||||
void DispatcherBase::ClearConnectorOverride() {
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
m_client_connector_override = nullptr;
|
||||
}
|
||||
|
||||
@@ -319,7 +319,7 @@ void DispatcherBase::DispatchThreadMain() {
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard user_lock(m_user_mutex);
|
||||
std::scoped_lock user_lock(m_user_mutex);
|
||||
bool reconnect = false;
|
||||
|
||||
if (++count > 10) {
|
||||
@@ -350,7 +350,7 @@ void DispatcherBase::DispatchThreadMain() {
|
||||
void DispatcherBase::QueueOutgoing(std::shared_ptr<Message> msg,
|
||||
INetworkConnection* only,
|
||||
INetworkConnection* except) {
|
||||
std::lock_guard user_lock(m_user_mutex);
|
||||
std::scoped_lock user_lock(m_user_mutex);
|
||||
for (auto& conn : m_connections) {
|
||||
if (conn.get() == except) continue;
|
||||
if (only && conn.get() != only) continue;
|
||||
@@ -392,7 +392,7 @@ void DispatcherBase::ServerThreadMain() {
|
||||
std::bind(&IStorage::ProcessIncoming, &m_storage, _1, _2,
|
||||
std::weak_ptr<NetworkConnection>(conn)));
|
||||
{
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
// reuse dead connection slots
|
||||
bool placed = false;
|
||||
for (auto& c : m_connections) {
|
||||
@@ -417,7 +417,7 @@ void DispatcherBase::ClientThreadMain() {
|
||||
|
||||
// get next server to connect to
|
||||
{
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
if (m_client_connector_override) {
|
||||
connect = m_client_connector_override;
|
||||
} else {
|
||||
@@ -469,7 +469,7 @@ bool DispatcherBase::ClientHandshake(
|
||||
// get identity
|
||||
std::string self_id;
|
||||
{
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
self_id = m_identity;
|
||||
}
|
||||
|
||||
@@ -576,7 +576,7 @@ bool DispatcherBase::ServerHandshake(
|
||||
|
||||
// Start with server hello. TODO: initial connection flag
|
||||
if (proto_rev >= 0x0300) {
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
outgoing.emplace_back(Message::ServerHello(0u, m_identity));
|
||||
}
|
||||
|
||||
@@ -633,7 +633,7 @@ bool DispatcherBase::ServerHandshake(
|
||||
void DispatcherBase::ClientReconnect(unsigned int proto_rev) {
|
||||
if ((m_networkMode & NT_NET_MODE_SERVER) != 0) return;
|
||||
{
|
||||
std::lock_guard lock(m_user_mutex);
|
||||
std::scoped_lock lock(m_user_mutex);
|
||||
m_reconnect_proto_rev = proto_rev;
|
||||
m_do_reconnect = true;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ InstanceImpl* InstanceImpl::Get(int inst) {
|
||||
}
|
||||
|
||||
// slow path
|
||||
std::lock_guard lock(s_mutex);
|
||||
std::scoped_lock lock(s_mutex);
|
||||
|
||||
// static fast-path block
|
||||
if (static_cast<unsigned int>(inst) <
|
||||
@@ -66,7 +66,7 @@ int InstanceImpl::GetDefaultIndex() {
|
||||
if (inst >= 0) return inst;
|
||||
|
||||
// slow path
|
||||
std::lock_guard lock(s_mutex);
|
||||
std::scoped_lock lock(s_mutex);
|
||||
|
||||
// double-check
|
||||
inst = s_default;
|
||||
@@ -79,7 +79,7 @@ int InstanceImpl::GetDefaultIndex() {
|
||||
}
|
||||
|
||||
int InstanceImpl::Alloc() {
|
||||
std::lock_guard lock(s_mutex);
|
||||
std::scoped_lock lock(s_mutex);
|
||||
return AllocImpl();
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ int InstanceImpl::AllocImpl() {
|
||||
}
|
||||
|
||||
void InstanceImpl::Destroy(int inst) {
|
||||
std::lock_guard lock(s_mutex);
|
||||
std::scoped_lock lock(s_mutex);
|
||||
if (inst < 0 || static_cast<unsigned int>(inst) >= s_instances.size()) return;
|
||||
|
||||
if (static_cast<unsigned int>(inst) <
|
||||
|
||||
@@ -49,7 +49,7 @@ void NetworkConnection::Start() {
|
||||
while (!m_outgoing.empty()) m_outgoing.pop();
|
||||
// reset shutdown flags
|
||||
{
|
||||
std::lock_guard lock(m_shutdown_mutex);
|
||||
std::scoped_lock lock(m_shutdown_mutex);
|
||||
m_read_shutdown = false;
|
||||
m_write_shutdown = false;
|
||||
}
|
||||
@@ -104,12 +104,12 @@ void NetworkConnection::set_proto_rev(unsigned int proto_rev) {
|
||||
}
|
||||
|
||||
NetworkConnection::State NetworkConnection::state() const {
|
||||
std::lock_guard lock(m_state_mutex);
|
||||
std::scoped_lock lock(m_state_mutex);
|
||||
return m_state;
|
||||
}
|
||||
|
||||
void NetworkConnection::set_state(State state) {
|
||||
std::lock_guard lock(m_state_mutex);
|
||||
std::scoped_lock lock(m_state_mutex);
|
||||
// Don't update state any more once we've died
|
||||
if (m_state == kDead) return;
|
||||
// One-shot notify state changes
|
||||
@@ -121,12 +121,12 @@ void NetworkConnection::set_state(State state) {
|
||||
}
|
||||
|
||||
std::string NetworkConnection::remote_id() const {
|
||||
std::lock_guard lock(m_remote_id_mutex);
|
||||
std::scoped_lock lock(m_remote_id_mutex);
|
||||
return m_remote_id;
|
||||
}
|
||||
|
||||
void NetworkConnection::set_remote_id(StringRef remote_id) {
|
||||
std::lock_guard lock(m_remote_id_mutex);
|
||||
std::scoped_lock lock(m_remote_id_mutex);
|
||||
m_remote_id = remote_id;
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ void NetworkConnection::ReadThreadMain() {
|
||||
done:
|
||||
// use condition variable to signal thread shutdown
|
||||
{
|
||||
std::lock_guard lock(m_shutdown_mutex);
|
||||
std::scoped_lock lock(m_shutdown_mutex);
|
||||
m_read_shutdown = true;
|
||||
m_read_shutdown_cv.notify_one();
|
||||
}
|
||||
@@ -214,14 +214,14 @@ void NetworkConnection::WriteThreadMain() {
|
||||
|
||||
// use condition variable to signal thread shutdown
|
||||
{
|
||||
std::lock_guard lock(m_shutdown_mutex);
|
||||
std::scoped_lock lock(m_shutdown_mutex);
|
||||
m_write_shutdown = true;
|
||||
m_write_shutdown_cv.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkConnection::QueueOutgoing(std::shared_ptr<Message> msg) {
|
||||
std::lock_guard lock(m_pending_mutex);
|
||||
std::scoped_lock lock(m_pending_mutex);
|
||||
|
||||
// Merge with previous. One case we don't combine: delete/assign loop.
|
||||
switch (msg->type()) {
|
||||
@@ -317,7 +317,7 @@ void NetworkConnection::QueueOutgoing(std::shared_ptr<Message> msg) {
|
||||
}
|
||||
|
||||
void NetworkConnection::PostOutgoing(bool keep_alive) {
|
||||
std::lock_guard lock(m_pending_mutex);
|
||||
std::scoped_lock lock(m_pending_mutex);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (m_pending_outgoing.empty()) {
|
||||
if (!keep_alive) return;
|
||||
|
||||
@@ -64,7 +64,7 @@ class RpcServerThread
|
||||
RpcIdPair lookup_uid{local_id, call_uid};
|
||||
callback(data);
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
auto i = m_response_map.find(lookup_uid);
|
||||
if (i != m_response_map.end()) {
|
||||
// post an empty response and erase it
|
||||
|
||||
@@ -30,7 +30,7 @@ Storage::~Storage() {
|
||||
}
|
||||
|
||||
void Storage::SetDispatcher(IDispatcher* dispatcher, bool server) {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_dispatcher = dispatcher;
|
||||
m_server = server;
|
||||
}
|
||||
@@ -38,7 +38,7 @@ void Storage::SetDispatcher(IDispatcher* dispatcher, bool server) {
|
||||
void Storage::ClearDispatcher() { m_dispatcher = nullptr; }
|
||||
|
||||
NT_Type Storage::GetMessageEntryType(unsigned int id) const {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (id >= m_idmap.size()) return NT_UNASSIGNED;
|
||||
Entry* entry = m_idmap[id];
|
||||
if (!entry || !entry->value) return NT_UNASSIGNED;
|
||||
@@ -374,7 +374,7 @@ void Storage::ProcessIncomingRpcResponse(std::shared_ptr<Message> msg,
|
||||
|
||||
void Storage::GetInitialAssignments(
|
||||
INetworkConnection& conn, std::vector<std::shared_ptr<Message>>* msgs) {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
conn.set_state(INetworkConnection::kSynchronized);
|
||||
for (auto& i : m_entries) {
|
||||
Entry* entry = i.getValue();
|
||||
@@ -476,14 +476,14 @@ void Storage::ApplyInitialAssignments(
|
||||
}
|
||||
|
||||
std::shared_ptr<Value> Storage::GetEntryValue(StringRef name) const {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
auto i = m_entries.find(name);
|
||||
if (i == m_entries.end()) return nullptr;
|
||||
return i->getValue()->value;
|
||||
}
|
||||
|
||||
std::shared_ptr<Value> Storage::GetEntryValue(unsigned int local_id) const {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (local_id >= m_localmap.size()) return nullptr;
|
||||
return m_localmap[local_id]->value;
|
||||
}
|
||||
@@ -654,14 +654,14 @@ void Storage::SetEntryFlagsImpl(Entry* entry, unsigned int flags,
|
||||
}
|
||||
|
||||
unsigned int Storage::GetEntryFlags(StringRef name) const {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
auto i = m_entries.find(name);
|
||||
if (i == m_entries.end()) return 0;
|
||||
return i->getValue()->flags;
|
||||
}
|
||||
|
||||
unsigned int Storage::GetEntryFlags(unsigned int local_id) const {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (local_id >= m_localmap.size()) return 0;
|
||||
return m_localmap[local_id]->flags;
|
||||
}
|
||||
@@ -781,7 +781,7 @@ std::vector<unsigned int> Storage::GetEntries(const Twine& prefix,
|
||||
unsigned int types) {
|
||||
wpi::SmallString<128> prefixBuf;
|
||||
StringRef prefixStr = prefix.toStringRef(prefixBuf);
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
std::vector<unsigned int> ids;
|
||||
for (auto& i : m_entries) {
|
||||
Entry* entry = i.getValue();
|
||||
@@ -839,7 +839,7 @@ std::vector<EntryInfo> Storage::GetEntryInfo(int inst, const Twine& prefix,
|
||||
unsigned int types) {
|
||||
wpi::SmallString<128> prefixBuf;
|
||||
StringRef prefixStr = prefix.toStringRef(prefixBuf);
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
std::vector<EntryInfo> infos;
|
||||
for (auto& i : m_entries) {
|
||||
Entry* entry = i.getValue();
|
||||
@@ -863,7 +863,7 @@ unsigned int Storage::AddListener(
|
||||
unsigned int flags) const {
|
||||
wpi::SmallString<128> prefixBuf;
|
||||
StringRef prefixStr = prefix.toStringRef(prefixBuf);
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
unsigned int uid = m_notifier.Add(callback, prefixStr, flags);
|
||||
// perform immediate notifications
|
||||
if ((flags & NT_NOTIFY_IMMEDIATE) != 0 && (flags & NT_NOTIFY_NEW) != 0) {
|
||||
@@ -881,7 +881,7 @@ unsigned int Storage::AddListener(
|
||||
unsigned int local_id,
|
||||
std::function<void(const EntryNotification& event)> callback,
|
||||
unsigned int flags) const {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
unsigned int uid = m_notifier.Add(callback, local_id, flags);
|
||||
// perform immediate notifications
|
||||
if ((flags & NT_NOTIFY_IMMEDIATE) != 0 && (flags & NT_NOTIFY_NEW) != 0 &&
|
||||
@@ -900,7 +900,7 @@ unsigned int Storage::AddPolledListener(unsigned int poller,
|
||||
unsigned int flags) const {
|
||||
wpi::SmallString<128> prefixBuf;
|
||||
StringRef prefixStr = prefix.toStringRef(prefixBuf);
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
unsigned int uid = m_notifier.AddPolled(poller, prefixStr, flags);
|
||||
// perform immediate notifications
|
||||
if ((flags & NT_NOTIFY_IMMEDIATE) != 0 && (flags & NT_NOTIFY_NEW) != 0) {
|
||||
@@ -918,7 +918,7 @@ unsigned int Storage::AddPolledListener(unsigned int poller,
|
||||
unsigned int Storage::AddPolledListener(unsigned int poller,
|
||||
unsigned int local_id,
|
||||
unsigned int flags) const {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
unsigned int uid = m_notifier.AddPolled(poller, local_id, flags);
|
||||
// perform immediate notifications
|
||||
if ((flags & NT_NOTIFY_IMMEDIATE) != 0 && (flags & NT_NOTIFY_NEW) != 0 &&
|
||||
@@ -939,7 +939,7 @@ bool Storage::GetPersistentEntries(
|
||||
const {
|
||||
// copy values out of storage as quickly as possible so lock isn't held
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
// for periodic, don't re-save unless something has changed
|
||||
if (periodic && !m_persistent_dirty) return false;
|
||||
m_persistent_dirty = false;
|
||||
@@ -969,7 +969,7 @@ bool Storage::GetEntries(
|
||||
StringRef prefixStr = prefix.toStringRef(prefixBuf);
|
||||
// copy values out of storage as quickly as possible so lock isn't held
|
||||
{
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
entries->reserve(m_entries.size());
|
||||
for (auto& i : m_entries) {
|
||||
Entry* entry = i.getValue();
|
||||
@@ -1055,7 +1055,7 @@ unsigned int Storage::CallRpc(unsigned int local_id, StringRef params) {
|
||||
unsigned int call_uid = msg->seq_num_uid();
|
||||
m_rpc_server.ProcessRpc(local_id, call_uid, name, msg->str(), conn_info,
|
||||
[=](StringRef result) {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_rpc_results.insert(std::make_pair(
|
||||
RpcIdPair{local_id, call_uid}, result));
|
||||
m_rpc_results_cond.notify_all();
|
||||
|
||||
@@ -206,7 +206,7 @@ NetworkTableInstance NetworkTable::GetInstance() const {
|
||||
NetworkTableEntry NetworkTable::GetEntry(const Twine& key) const {
|
||||
wpi::SmallString<128> keyBuf;
|
||||
StringRef keyStr = key.toStringRef(keyBuf);
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
NT_Entry& entry = m_entries[keyStr];
|
||||
if (entry == 0) {
|
||||
entry = nt::GetEntry(m_inst, m_path + Twine(PATH_SEPARATOR_CHAR) + keyStr);
|
||||
@@ -259,7 +259,7 @@ void NetworkTable::AddTableListener(ITableListener* listener,
|
||||
|
||||
void NetworkTable::AddTableListenerEx(ITableListener* listener,
|
||||
unsigned int flags) {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
wpi::SmallString<128> path(m_path);
|
||||
path += PATH_SEPARATOR_CHAR;
|
||||
size_t prefix_len = path.size();
|
||||
@@ -283,7 +283,7 @@ void NetworkTable::AddTableListener(StringRef key, ITableListener* listener,
|
||||
|
||||
void NetworkTable::AddTableListenerEx(StringRef key, ITableListener* listener,
|
||||
unsigned int flags) {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
size_t prefix_len = m_path.size() + 1;
|
||||
auto entry = GetEntry(key);
|
||||
NT_EntryListener id = nt::AddEntryListener(
|
||||
@@ -336,7 +336,7 @@ void NetworkTable::RemoveTableListener(NT_EntryListener listener) {
|
||||
|
||||
void NetworkTable::AddSubTableListener(ITableListener* listener,
|
||||
bool localNotify) {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
size_t prefix_len = m_path.size() + 1;
|
||||
|
||||
// The lambda needs to be copyable, but StringMap is not, so use
|
||||
@@ -362,7 +362,7 @@ void NetworkTable::AddSubTableListener(ITableListener* listener,
|
||||
}
|
||||
|
||||
void NetworkTable::RemoveTableListener(ITableListener* listener) {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
auto matches_begin =
|
||||
std::remove_if(m_listeners.begin(), m_listeners.end(),
|
||||
[=](const Listener& x) { return x.first == listener; });
|
||||
@@ -397,7 +397,7 @@ std::vector<std::string> NetworkTable::GetKeys(int types) const {
|
||||
std::vector<std::string> keys;
|
||||
size_t prefix_len = m_path.size() + 1;
|
||||
auto infos = GetEntryInfo(m_inst, m_path + Twine(PATH_SEPARATOR_CHAR), types);
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
for (auto& info : infos) {
|
||||
auto relative_key = StringRef(info.name).substr(prefix_len);
|
||||
if (relative_key.find(PATH_SEPARATOR_CHAR) != StringRef::npos) continue;
|
||||
|
||||
@@ -959,7 +959,7 @@ void SetLogger(LogFunc func, unsigned int min_level) {
|
||||
auto ii = InstanceImpl::GetDefault();
|
||||
static wpi::mutex mutex;
|
||||
static unsigned int logger = 0;
|
||||
std::lock_guard lock(mutex);
|
||||
std::scoped_lock lock(mutex);
|
||||
if (logger != 0) ii->logger_impl.Remove(logger);
|
||||
logger = ii->logger_impl.Add(
|
||||
[=](const LogMessage& msg) {
|
||||
|
||||
Reference in New Issue
Block a user