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;
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include <mysqld_error.h>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
|
||||
std::mutex TransactionTask::_deadlockLock;
|
||||
|
||||
@@ -73,7 +73,7 @@ bool TransactionTask::Execute(MySQLConnection* conn, std::shared_ptr<Transaction
|
||||
}();
|
||||
|
||||
// Make sure only 1 async thread retries a transaction so they don't keep dead-locking each other
|
||||
std::lock_guard<std::mutex> lock(_deadlockLock);
|
||||
std::scoped_lock lock(_deadlockLock);
|
||||
|
||||
for (uint32 loopDuration = 0, startMSTime = getMSTime(); loopDuration <= DEADLOCK_MAX_RETRY_TIME_MS; loopDuration = GetMSTimeDiffToNow(startMSTime))
|
||||
{
|
||||
@@ -94,7 +94,7 @@ bool TransactionTask::Execute(MySQLConnection* conn, std::shared_ptr<Transaction
|
||||
|
||||
int TransactionTask::TryExecute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans)
|
||||
{
|
||||
return conn->ExecuteTransaction(trans);
|
||||
return conn->ExecuteTransaction(std::move(trans));
|
||||
}
|
||||
|
||||
bool TransactionCallback::InvokeIfReady()
|
||||
|
||||
@@ -35,7 +35,7 @@ void HashMapHolder<T>::Insert(T* o)
|
||||
static_assert(std::is_same<Player, T>::value,
|
||||
"Only Player can be registered in global HashMapHolder");
|
||||
|
||||
std::unique_lock<std::shared_mutex> lock(*GetLock());
|
||||
std::scoped_lock lock(*GetLock());
|
||||
|
||||
GetContainer()[o->GetGUID()] = o;
|
||||
}
|
||||
@@ -43,7 +43,7 @@ void HashMapHolder<T>::Insert(T* o)
|
||||
template<class T>
|
||||
void HashMapHolder<T>::Remove(T* o)
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> lock(*GetLock());
|
||||
std::scoped_lock lock(*GetLock());
|
||||
|
||||
GetContainer().erase(o->GetGUID());
|
||||
}
|
||||
@@ -51,7 +51,7 @@ void HashMapHolder<T>::Remove(T* o)
|
||||
template<class T>
|
||||
T* HashMapHolder<T>::Find(ObjectGuid guid)
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(*GetLock());
|
||||
std::shared_lock lock(*GetLock());
|
||||
|
||||
typename MapType::iterator itr = GetContainer().find(guid);
|
||||
return (itr != GetContainer().end()) ? itr->second : nullptr;
|
||||
@@ -290,7 +290,7 @@ HashMapHolder<Player>::MapType const& ObjectAccessor::GetPlayers()
|
||||
|
||||
void ObjectAccessor::SaveAllPlayers()
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(*HashMapHolder<Player>::GetLock());
|
||||
std::shared_lock lock(*HashMapHolder<Player>::GetLock());
|
||||
|
||||
HashMapHolder<Player>::MapType const& m = GetPlayers();
|
||||
for (HashMapHolder<Player>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr)
|
||||
|
||||
@@ -280,7 +280,7 @@ InstanceLock* InstanceLockMgr::UpdateInstanceLockForPlayer(CharacterDatabaseTran
|
||||
InstanceLock* instanceLock = FindActiveInstanceLock(playerGuid, entries, true, true);
|
||||
if (!instanceLock)
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> guard(_locksMutex);
|
||||
std::scoped_lock guard(_locksMutex);
|
||||
|
||||
// Move lock from temporary storage if it exists there
|
||||
// This is to avoid destroying expired locks before any boss is killed in a fresh lock
|
||||
@@ -323,7 +323,7 @@ InstanceLock* InstanceLockMgr::UpdateInstanceLockForPlayer(CharacterDatabaseTran
|
||||
GetNextResetTime(entries), updateEvent.InstanceId);
|
||||
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> guard(_locksMutex);
|
||||
std::scoped_lock guard(_locksMutex);
|
||||
|
||||
_instanceLocksByPlayer[playerGuid][entries.GetKey()].reset(instanceLock);
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ bool LootItemStorage::LoadStoredLoot(Item* item, Player* player)
|
||||
|
||||
// read
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(*GetLock());
|
||||
std::shared_lock lock(*GetLock());
|
||||
|
||||
auto itr = _lootItemStore.find(item->GetGUID().GetCounter());
|
||||
if (itr == _lootItemStore.end())
|
||||
@@ -199,7 +199,7 @@ bool LootItemStorage::LoadStoredLoot(Item* item, Player* player)
|
||||
void LootItemStorage::RemoveStoredMoneyForContainer(uint64 containerId)
|
||||
{
|
||||
// write
|
||||
std::unique_lock<std::shared_mutex> lock(*GetLock());
|
||||
std::scoped_lock lock(*GetLock());
|
||||
|
||||
auto itr = _lootItemStore.find(containerId);
|
||||
if (itr == _lootItemStore.end())
|
||||
@@ -212,7 +212,7 @@ void LootItemStorage::RemoveStoredLootForContainer(uint64 containerId)
|
||||
{
|
||||
// write
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> lock(*GetLock());
|
||||
std::scoped_lock lock(*GetLock());
|
||||
_lootItemStore.erase(containerId);
|
||||
}
|
||||
|
||||
@@ -231,7 +231,7 @@ void LootItemStorage::RemoveStoredLootForContainer(uint64 containerId)
|
||||
void LootItemStorage::RemoveStoredLootItemForContainer(uint64 containerId, LootItemType type, uint32 itemId, uint32 count, uint32 itemIndex)
|
||||
{
|
||||
// write
|
||||
std::unique_lock<std::shared_mutex> lock(*GetLock());
|
||||
std::scoped_lock lock(*GetLock());
|
||||
|
||||
auto itr = _lootItemStore.find(containerId);
|
||||
if (itr == _lootItemStore.end())
|
||||
@@ -248,7 +248,7 @@ void LootItemStorage::AddNewStoredLoot(uint64 containerId, Loot* loot, Player* p
|
||||
|
||||
// read
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(*GetLock());
|
||||
std::shared_lock lock(*GetLock());
|
||||
|
||||
auto itr = _lootItemStore.find(containerId);
|
||||
if (itr != _lootItemStore.end())
|
||||
@@ -290,7 +290,7 @@ void LootItemStorage::AddNewStoredLoot(uint64 containerId, Loot* loot, Player* p
|
||||
|
||||
// write
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> lock(*GetLock());
|
||||
std::scoped_lock lock(*GetLock());
|
||||
_lootItemStore.emplace(containerId, std::move(container));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ struct TileCache
|
||||
private:
|
||||
void OnCacheCleanupTimerTick(boost::system::error_code const& error)
|
||||
{
|
||||
if (error)
|
||||
if (error || !_builderThread.joinable() /*shutting down*/)
|
||||
return;
|
||||
|
||||
TimePoint now = GameTime::Now();
|
||||
@@ -176,7 +176,7 @@ private:
|
||||
|
||||
void RemoveOldCacheEntries(TimePoint oldestPreservedEntryTimestamp)
|
||||
{
|
||||
std::lock_guard lock(TilesMutex);
|
||||
std::scoped_lock lock(TilesMutex);
|
||||
Trinity::Containers::EraseIf(Tiles, [=](std::unordered_map<TileCacheKey, Tile>::value_type const& kv)
|
||||
{
|
||||
return kv.second.LastAccessed < oldestPreservedEntryTimestamp;
|
||||
@@ -306,7 +306,7 @@ std::weak_ptr<DynamicTileBuilder::AsyncTileResult> DynamicTileBuilder::BuildTile
|
||||
cacheKey.CachedHash = std::hash<TileCacheKey>::Compute(cacheKey);
|
||||
|
||||
TileCache* tileCache = TileCache::Instance();
|
||||
std::lock_guard lock(tileCache->TilesMutex);
|
||||
std::scoped_lock lock(tileCache->TilesMutex);
|
||||
auto [itr, isNew] = tileCache->Tiles.try_emplace(std::move(cacheKey));
|
||||
itr->second.LastAccessed = GameTime::Now();
|
||||
if (!isNew)
|
||||
|
||||
@@ -163,7 +163,7 @@ Map* MapManager::CreateMap(uint32 mapId, Player* player, Optional<uint32> lfgDun
|
||||
if (!entry)
|
||||
return nullptr;
|
||||
|
||||
std::unique_lock<std::shared_mutex> lock(_mapsLock);
|
||||
std::scoped_lock lock(_mapsLock);
|
||||
|
||||
Map* map = nullptr;
|
||||
uint32 newInstanceId = 0; // instanceId of the resulting map
|
||||
|
||||
@@ -20,8 +20,6 @@
|
||||
#include "Map.h"
|
||||
#include "Metric.h"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
class MapUpdateRequest
|
||||
{
|
||||
private:
|
||||
@@ -48,9 +46,7 @@ class MapUpdateRequest
|
||||
void MapUpdater::activate(size_t num_threads)
|
||||
{
|
||||
for (size_t i = 0; i < num_threads; ++i)
|
||||
{
|
||||
_workerThreads.push_back(std::thread(&MapUpdater::WorkerThread, this));
|
||||
}
|
||||
_workerThreads.emplace_back(&MapUpdater::WorkerThread, this);
|
||||
}
|
||||
|
||||
void MapUpdater::deactivate()
|
||||
@@ -62,38 +58,33 @@ void MapUpdater::deactivate()
|
||||
_queue.Cancel();
|
||||
|
||||
for (auto& thread : _workerThreads)
|
||||
{
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void MapUpdater::wait()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(_lock);
|
||||
std::unique_lock lock(_lock);
|
||||
|
||||
while (pending_requests > 0)
|
||||
_condition.wait(lock);
|
||||
|
||||
lock.unlock();
|
||||
_condition.wait(lock, [&] { return pending_requests == 0; });
|
||||
}
|
||||
|
||||
void MapUpdater::schedule_update(Map& map, uint32 diff)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
|
||||
++pending_requests;
|
||||
|
||||
_queue.Push(new MapUpdateRequest(map, *this, diff));
|
||||
}
|
||||
|
||||
bool MapUpdater::activated()
|
||||
bool MapUpdater::activated() const
|
||||
{
|
||||
return _workerThreads.size() > 0;
|
||||
return !_workerThreads.empty();
|
||||
}
|
||||
|
||||
void MapUpdater::update_finished()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
std::scoped_lock lock(_lock);
|
||||
|
||||
--pending_requests;
|
||||
|
||||
@@ -107,7 +98,7 @@ void MapUpdater::WorkerThread()
|
||||
WorldDatabase.WarnAboutSyncQueries(true);
|
||||
HotfixDatabase.WarnAboutSyncQueries(true);
|
||||
|
||||
while (1)
|
||||
while (true)
|
||||
{
|
||||
MapUpdateRequest* request = nullptr;
|
||||
|
||||
|
||||
@@ -19,10 +19,11 @@
|
||||
#define _MAP_UPDATER_H_INCLUDED
|
||||
|
||||
#include "Define.h"
|
||||
#include "ProducerConsumerQueue.h"
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include "ProducerConsumerQueue.h"
|
||||
#include <vector>
|
||||
|
||||
class MapUpdateRequest;
|
||||
class Map;
|
||||
@@ -44,7 +45,7 @@ class TC_GAME_API MapUpdater
|
||||
|
||||
void deactivate();
|
||||
|
||||
bool activated();
|
||||
bool activated() const;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
@@ -158,7 +158,7 @@ void TerrainInfo::LoadMapAndVMap(int32 gx, int32 gy)
|
||||
if (++_referenceCountFromMap[gx][gy] != 1) // check if already loaded
|
||||
return;
|
||||
|
||||
std::lock_guard<std::mutex> lock(_loadMutex);
|
||||
std::scoped_lock lock(_loadMutex);
|
||||
LoadMapAndVMapImpl(gx, gy);
|
||||
}
|
||||
|
||||
@@ -301,7 +301,7 @@ GridMap* TerrainInfo::GetGrid(uint32 mapId, float x, float y, bool loadIfMissing
|
||||
// ensure GridMap is loaded
|
||||
if (!(_loadedGrids[gx] & (UI64LIT(1) << gy)) && loadIfMissing)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_loadMutex);
|
||||
std::scoped_lock lock(_loadMutex);
|
||||
LoadMapAndVMapImpl(gx, gy);
|
||||
}
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ void PacketLog::Initialize()
|
||||
|
||||
void PacketLog::LogPacket(WorldPacket const& packet, Direction direction, boost::asio::ip::address const& addr, uint16 port, ConnectionType connectionType)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_logPacketLock);
|
||||
std::scoped_lock lock(_logPacketLock);
|
||||
|
||||
PacketHeader header;
|
||||
header.Direction = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
|
||||
|
||||
@@ -522,21 +522,18 @@ bool WorldSession::Update(uint32 diff, PacketFilter& updater)
|
||||
LogoutPlayer(true);
|
||||
|
||||
///- Cleanup socket pointer if need
|
||||
if ((m_Socket[CONNECTION_TYPE_REALM] && !m_Socket[CONNECTION_TYPE_REALM]->IsOpen()) ||
|
||||
(m_Socket[CONNECTION_TYPE_INSTANCE] && !m_Socket[CONNECTION_TYPE_INSTANCE]->IsOpen()))
|
||||
if (std::ranges::any_of(m_Socket, [](std::shared_ptr<WorldSocket> const& s) { return s && !s->IsOpen(); }))
|
||||
{
|
||||
expireTime -= expireTime > diff ? diff : expireTime;
|
||||
if (expireTime < diff || forceExit || !GetPlayer())
|
||||
{
|
||||
if (m_Socket[CONNECTION_TYPE_REALM])
|
||||
for (std::shared_ptr<WorldSocket>& socket : m_Socket)
|
||||
{
|
||||
m_Socket[CONNECTION_TYPE_REALM]->CloseSocket();
|
||||
m_Socket[CONNECTION_TYPE_REALM].reset();
|
||||
}
|
||||
if (m_Socket[CONNECTION_TYPE_INSTANCE])
|
||||
{
|
||||
m_Socket[CONNECTION_TYPE_INSTANCE]->CloseSocket();
|
||||
m_Socket[CONNECTION_TYPE_INSTANCE].reset();
|
||||
if (socket)
|
||||
{
|
||||
socket->CloseSocket();
|
||||
socket.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,7 +253,7 @@ void WorldSocket::SendAuthSession()
|
||||
void WorldSocket::OnClose()
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> sessionGuard(_worldSessionLock);
|
||||
std::scoped_lock sessionGuard(_worldSessionLock);
|
||||
_worldSession = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -323,7 +323,7 @@ void WorldSocket::QueueQuery(QueryCallback&& queryCallback)
|
||||
|
||||
void WorldSocket::SetWorldSession(WorldSession* session)
|
||||
{
|
||||
std::lock_guard<std::mutex> sessionGuard(_worldSessionLock);
|
||||
std::scoped_lock sessionGuard(_worldSessionLock);
|
||||
_worldSession = session;
|
||||
_authed = true;
|
||||
}
|
||||
@@ -1053,7 +1053,7 @@ bool WorldSocket::HandlePing(WorldPackets::Auth::Ping& ping)
|
||||
{
|
||||
bool ignoresOverspeedPingsLimit = [&]
|
||||
{
|
||||
std::lock_guard<std::mutex> sessionGuard(_worldSessionLock);
|
||||
std::scoped_lock sessionGuard(_worldSessionLock);
|
||||
return _worldSession && _worldSession->HasPermission(rbac::RBAC_PERM_SKIP_CHECK_OVERSPEED_PING);
|
||||
}();
|
||||
|
||||
@@ -1072,7 +1072,7 @@ bool WorldSocket::HandlePing(WorldPackets::Auth::Ping& ping)
|
||||
|
||||
bool success = [&]
|
||||
{
|
||||
std::lock_guard<std::mutex> sessionGuard(_worldSessionLock);
|
||||
std::scoped_lock sessionGuard(_worldSessionLock);
|
||||
if (_worldSession)
|
||||
{
|
||||
_worldSession->SetLatency(ping.Latency);
|
||||
|
||||
@@ -268,7 +268,7 @@ std::vector<std::string> const& World::GetMotd() const
|
||||
void World::TriggerGuidWarning()
|
||||
{
|
||||
// Lock this only to prevent multiple maps triggering at the same time
|
||||
std::lock_guard<std::mutex> lock(_guidAlertLock);
|
||||
std::scoped_lock lock(_guidAlertLock);
|
||||
|
||||
time_t gameTime = GameTime::GetGameTime();
|
||||
time_t today = (gameTime / DAY) * DAY;
|
||||
@@ -287,7 +287,7 @@ void World::TriggerGuidWarning()
|
||||
void World::TriggerGuidAlert()
|
||||
{
|
||||
// Lock this only to prevent multiple maps triggering at the same time
|
||||
std::lock_guard<std::mutex> lock(_guidAlertLock);
|
||||
std::scoped_lock lock(_guidAlertLock);
|
||||
|
||||
DoGuidAlertRestart();
|
||||
_guidAlert = true;
|
||||
|
||||
@@ -187,7 +187,7 @@ void RealmList::UpdateRealms()
|
||||
TC_LOG_INFO("realmlist", "Removed realm \"{}\".", itr->second);
|
||||
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> lock(_realmsMutex);
|
||||
std::scoped_lock lock(_realmsMutex);
|
||||
|
||||
_subRegions.swap(newSubRegions);
|
||||
_realms.swap(newRealms);
|
||||
@@ -213,7 +213,7 @@ void RealmList::UpdateRealms()
|
||||
|
||||
std::shared_ptr<Realm const> RealmList::GetRealm(Battlenet::RealmHandle const& id) const
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(_realmsMutex);
|
||||
std::shared_lock lock(_realmsMutex);
|
||||
return Trinity::Containers::MapGetValuePtr(_realms, id);
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ std::shared_ptr<Realm const> RealmList::GetCurrentRealm() const
|
||||
|
||||
void RealmList::WriteSubRegions(bgs::protocol::game_utilities::v1::GetAllValuesForAttributeResponse* response) const
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(_realmsMutex);
|
||||
std::shared_lock lock(_realmsMutex);
|
||||
for (std::string const& subRegion : _subRegions)
|
||||
response->add_attribute_value()->set_string_value(subRegion);
|
||||
}
|
||||
@@ -301,7 +301,7 @@ std::vector<uint8> RealmList::GetRealmList(uint32 build, AccountTypes accountSec
|
||||
{
|
||||
JSON::RealmList::RealmListUpdates realmList;
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(_realmsMutex);
|
||||
std::shared_lock lock(_realmsMutex);
|
||||
for (auto const& [_, realm] : _realms)
|
||||
{
|
||||
if (realm->Id.GetSubRegionAddress() != subRegion)
|
||||
|
||||
@@ -93,7 +93,7 @@ void SecretMgr::Initialize(SecretOwner owner)
|
||||
{
|
||||
if (secret_info[i].flags() & SECRET_FLAG_DEFER_LOAD)
|
||||
continue;
|
||||
std::unique_lock<std::mutex> lock(_secrets[i].lock);
|
||||
std::scoped_lock lock(_secrets[i].lock);
|
||||
AttemptLoad(Secrets(i), LOG_LEVEL_FATAL, lock);
|
||||
if (!_secrets[i].IsAvailable())
|
||||
ABORT(); // load failed
|
||||
@@ -102,14 +102,14 @@ void SecretMgr::Initialize(SecretOwner owner)
|
||||
|
||||
SecretMgr::Secret const& SecretMgr::GetSecret(Secrets i)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(_secrets[i].lock);
|
||||
std::scoped_lock lock(_secrets[i].lock);
|
||||
|
||||
if (_secrets[i].state == Secret::NOT_LOADED_YET)
|
||||
AttemptLoad(i, LOG_LEVEL_ERROR, lock);
|
||||
return _secrets[i];
|
||||
}
|
||||
|
||||
void SecretMgr::AttemptLoad(Secrets i, LogLevel errorLevel, std::unique_lock<std::mutex> const&)
|
||||
void SecretMgr::AttemptLoad(Secrets i, LogLevel errorLevel, std::scoped_lock<std::mutex> const&)
|
||||
{
|
||||
auto const& info = secret_info[i];
|
||||
Optional<std::string> oldDigest;
|
||||
|
||||
@@ -78,7 +78,7 @@ class TC_SHARED_API SecretMgr
|
||||
Secret const& GetSecret(Secrets i);
|
||||
|
||||
private:
|
||||
void AttemptLoad(Secrets i, LogLevel errorLevel, std::unique_lock<std::mutex> const&);
|
||||
void AttemptLoad(Secrets i, LogLevel errorLevel, std::scoped_lock<std::mutex> const&);
|
||||
Optional<std::string> AttemptTransition(Secrets i, Optional<BigNumber> const& newSecret, Optional<BigNumber> const& oldSecret, bool hadOldSecret) const;
|
||||
|
||||
std::array<Secret, NUM_SECRETS> _secrets;
|
||||
|
||||
@@ -182,7 +182,7 @@ uint32 GenerateUniqueObjectId(uint32 clientId, uint16 clientDoodadId, bool isWmo
|
||||
{
|
||||
// WMO client ids must be preserved, they are used in DB2 files
|
||||
uint32 newId = isWmo ? clientId : UniqueObjectIdGenerator--;
|
||||
std::lock_guard lock(UniqueObjectIdsMutex);
|
||||
std::scoped_lock lock(UniqueObjectIdsMutex);
|
||||
return UniqueObjectIds.emplace(std::make_pair(clientId, clientDoodadId), newId).first->second;
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ std::unordered_map<std::string, ExtractedModelData> ExtractedModels;
|
||||
|
||||
std::pair<ExtractedModelData*, bool> BeginModelExtraction(std::string const& outputName)
|
||||
{
|
||||
std::lock_guard lock(ExtractedModelsMutex);
|
||||
std::scoped_lock lock(ExtractedModelsMutex);
|
||||
auto [itr, isNew] = ExtractedModels.try_emplace(outputName);
|
||||
return { &itr->second, isNew };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user