mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Misc: Use std::scoped_lock instead of unique_lock where possible (and old lock_guard)
This commit is contained in:
@@ -273,7 +273,7 @@ namespace VMAP
|
||||
std::shared_ptr<ManagedModel> worldmodel; // this is intentionally declared before lock so that it is destroyed after it to prevent deadlocks in releaseModelInstance
|
||||
|
||||
//! Critical section, thread safe access to iLoadedModelFiles
|
||||
std::lock_guard lock(LoadedModelFilesLock);
|
||||
std::scoped_lock lock(LoadedModelFilesLock);
|
||||
|
||||
auto& [key, model] = *iLoadedModelFiles.try_emplace(filename).first;
|
||||
worldmodel = model.lock();
|
||||
@@ -296,7 +296,7 @@ namespace VMAP
|
||||
void VMapManager::releaseModelInstance(std::string const& filename)
|
||||
{
|
||||
//! Critical section, thread safe access to iLoadedModelFiles
|
||||
std::lock_guard lock(LoadedModelFilesLock);
|
||||
std::scoped_lock lock(LoadedModelFilesLock);
|
||||
|
||||
TC_LOG_DEBUG("maps", "VMapManager: unloading file '{}'", filename);
|
||||
|
||||
|
||||
@@ -137,7 +137,7 @@ namespace
|
||||
bool ConfigMgr::LoadInitial(std::string file, std::vector<std::string> args,
|
||||
std::string& error)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configLock);
|
||||
std::scoped_lock lock(_configLock);
|
||||
|
||||
_filename = std::move(file);
|
||||
_args = std::move(args);
|
||||
@@ -158,7 +158,7 @@ bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::str
|
||||
if (!LoadFile(file, fullTree, error))
|
||||
return false;
|
||||
|
||||
std::lock_guard<std::mutex> lock(_configLock);
|
||||
std::scoped_lock lock(_configLock);
|
||||
|
||||
for (bpt::ptree::value_type const& child : fullTree.begin()->second)
|
||||
_config.put_child(bpt::ptree::path_type(child.first, '/'), child.second);
|
||||
@@ -197,7 +197,7 @@ bool ConfigMgr::LoadAdditionalDir(std::string const& dir, bool keepOnReload, std
|
||||
|
||||
std::vector<std::string> ConfigMgr::OverrideWithEnvVariablesIfAny()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configLock);
|
||||
std::scoped_lock lock(_configLock);
|
||||
|
||||
std::vector<std::string> overriddenKeys;
|
||||
|
||||
@@ -346,7 +346,7 @@ float ConfigMgr::GetFloatDefault(std::string_view name, float def, bool quiet) c
|
||||
|
||||
std::string const& ConfigMgr::GetFilename()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configLock);
|
||||
std::scoped_lock lock(_configLock);
|
||||
return _filename;
|
||||
}
|
||||
|
||||
@@ -357,7 +357,7 @@ std::vector<std::string> const& ConfigMgr::GetArguments() const
|
||||
|
||||
std::vector<std::string> ConfigMgr::GetKeysByString(std::string const& name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configLock);
|
||||
std::scoped_lock lock(_configLock);
|
||||
|
||||
std::vector<std::string> keys;
|
||||
|
||||
|
||||
@@ -39,14 +39,14 @@ public:
|
||||
//! Adds an item to the queue.
|
||||
void add(T const& item)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
_queue.push_back(item);
|
||||
}
|
||||
|
||||
//! Adds an item to the queue.
|
||||
void add(T&& item)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
_queue.push_back(std::move(item));
|
||||
}
|
||||
|
||||
@@ -54,14 +54,14 @@ public:
|
||||
template<std::input_iterator Iterator>
|
||||
void readd(Iterator begin, Iterator end)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
_queue.insert(_queue.begin(), begin, end);
|
||||
}
|
||||
|
||||
//! Gets the next result in the queue, if any.
|
||||
bool next(T& result)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
if (_queue.empty())
|
||||
return false;
|
||||
|
||||
@@ -73,7 +73,7 @@ public:
|
||||
template<class Checker>
|
||||
bool next(T& result, Checker& check)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
if (_queue.empty())
|
||||
return false;
|
||||
|
||||
@@ -89,28 +89,28 @@ public:
|
||||
//! Cancels the queue.
|
||||
void cancel()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
_canceled = true;
|
||||
}
|
||||
|
||||
//! Checks if the queue is cancelled.
|
||||
bool cancelled()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
return _canceled;
|
||||
}
|
||||
|
||||
///! Calls pop_front of the queue
|
||||
void pop_front()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
_queue.pop_front();
|
||||
}
|
||||
|
||||
///! Checks if we're empty or not with locks held
|
||||
bool empty()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
return _queue.empty();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -18,10 +18,10 @@
|
||||
#ifndef TRINITY_PRODUCER_CONSUMER_QUEUE_H
|
||||
#define TRINITY_PRODUCER_CONSUMER_QUEUE_H
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <atomic>
|
||||
#include <type_traits>
|
||||
|
||||
template <typename T>
|
||||
@@ -39,7 +39,7 @@ public:
|
||||
|
||||
void Push(T const& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_queueLock);
|
||||
std::scoped_lock lock(_queueLock);
|
||||
_queue.push(value);
|
||||
|
||||
_condition.notify_one();
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
|
||||
void Push(T&& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_queueLock);
|
||||
std::scoped_lock lock(_queueLock);
|
||||
_queue.push(std::move(value));
|
||||
|
||||
_condition.notify_one();
|
||||
@@ -55,21 +55,21 @@ public:
|
||||
|
||||
bool Empty() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_queueLock);
|
||||
std::scoped_lock lock(_queueLock);
|
||||
|
||||
return _queue.empty();
|
||||
}
|
||||
|
||||
size_t Size() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_queueLock);
|
||||
std::scoped_lock lock(_queueLock);
|
||||
|
||||
return _queue.size();
|
||||
}
|
||||
|
||||
bool Pop(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_queueLock);
|
||||
std::scoped_lock lock(_queueLock);
|
||||
|
||||
if (_queue.empty() || _shutdown)
|
||||
return false;
|
||||
@@ -83,12 +83,9 @@ public:
|
||||
|
||||
void WaitAndPop(T& value)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(_queueLock);
|
||||
std::unique_lock lock(_queueLock);
|
||||
|
||||
// we could be using .wait(lock, predicate) overload here but it is broken
|
||||
// https://connect.microsoft.com/VisualStudio/feedback/details/1098841
|
||||
while (_queue.empty() && !_shutdown)
|
||||
_condition.wait(lock);
|
||||
_condition.wait(lock, [&] { return !_queue.empty() || _shutdown; });
|
||||
|
||||
if (_queue.empty() || _shutdown)
|
||||
return;
|
||||
@@ -100,7 +97,7 @@ public:
|
||||
|
||||
void Cancel()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(_queueLock);
|
||||
std::scoped_lock lock(_queueLock);
|
||||
|
||||
while (!_queue.empty())
|
||||
{
|
||||
|
||||
@@ -130,7 +130,7 @@ void SessionService::InitAndStoreSessionState(std::shared_ptr<SessionState> stat
|
||||
|
||||
// Generate session id
|
||||
{
|
||||
std::unique_lock lock{ _sessionsMutex };
|
||||
std::scoped_lock lock{ _sessionsMutex };
|
||||
|
||||
while (state->Id.is_nil() || _sessions.contains(state->Id))
|
||||
std::copy_n(Trinity::Crypto::GetRandomBytes<16>().begin(), 16, state->Id.begin());
|
||||
@@ -157,11 +157,11 @@ void SessionService::Stop()
|
||||
{
|
||||
_inactiveSessionsKillTimer = nullptr;
|
||||
{
|
||||
std::unique_lock lock{ _sessionsMutex };
|
||||
std::scoped_lock lock{ _sessionsMutex };
|
||||
_sessions.clear();
|
||||
}
|
||||
{
|
||||
std::unique_lock lock{ _inactiveSessionsMutex };
|
||||
std::scoped_lock lock{ _inactiveSessionsMutex };
|
||||
_inactiveSessions.clear();
|
||||
}
|
||||
}
|
||||
@@ -190,7 +190,7 @@ std::shared_ptr<SessionState> SessionService::FindAndRefreshSessionState(std::st
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock inactiveSessionsLock{ _inactiveSessionsMutex };
|
||||
std::scoped_lock inactiveSessionsLock{ _inactiveSessionsMutex };
|
||||
_inactiveSessions.erase(state->Id);
|
||||
}
|
||||
|
||||
@@ -201,7 +201,7 @@ void SessionService::MarkSessionInactive(boost::uuids::uuid const& id)
|
||||
{
|
||||
bool wasActive = true;
|
||||
{
|
||||
std::unique_lock inactiveSessionsLock{ _inactiveSessionsMutex };
|
||||
std::scoped_lock inactiveSessionsLock{ _inactiveSessionsMutex };
|
||||
wasActive = _inactiveSessions.insert(id).second;
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ void SessionService::KillInactiveSessions()
|
||||
std::set<boost::uuids::uuid> inactiveSessions;
|
||||
|
||||
{
|
||||
std::unique_lock lock{ _inactiveSessionsMutex };
|
||||
std::scoped_lock lock{ _inactiveSessionsMutex };
|
||||
std::swap(_inactiveSessions, inactiveSessions);
|
||||
}
|
||||
|
||||
@@ -230,7 +230,7 @@ void SessionService::KillInactiveSessions()
|
||||
TimePoint now = TimePoint::clock::now();
|
||||
std::size_t inactiveSessionsCount = inactiveSessions.size();
|
||||
|
||||
std::unique_lock lock{ _sessionsMutex };
|
||||
std::scoped_lock lock{ _sessionsMutex };
|
||||
for (auto itr = inactiveSessions.begin(); itr != inactiveSessions.end(); )
|
||||
{
|
||||
auto sessionItr = _sessions.find(*itr);
|
||||
@@ -248,7 +248,7 @@ void SessionService::KillInactiveSessions()
|
||||
|
||||
{
|
||||
// restore sessions not killed to inactive queue
|
||||
std::unique_lock lock{ _inactiveSessionsMutex };
|
||||
std::scoped_lock lock{ _inactiveSessionsMutex };
|
||||
for (auto itr = inactiveSessions.begin(); itr != inactiveSessions.end(); )
|
||||
{
|
||||
auto node = inactiveSessions.extract(itr++);
|
||||
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
|
||||
void AddSocket(std::shared_ptr<SocketType> sock)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_newSocketsLock);
|
||||
std::scoped_lock lock(_newSocketsLock);
|
||||
|
||||
++_connections;
|
||||
SocketAdded(_newSockets.emplace_back(std::move(sock)));
|
||||
@@ -98,7 +98,7 @@ protected:
|
||||
|
||||
void AddNewSockets()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_newSocketsLock);
|
||||
std::scoped_lock lock(_newSocketsLock);
|
||||
|
||||
if (_newSockets.empty())
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user