feat(Core/Threading): replace ace threading (#4821)

This commit is contained in:
Kargatum
2021-04-17 00:45:29 +07:00
committed by GitHub
parent b9e84d8278
commit b2861be1cd
50 changed files with 300 additions and 342 deletions

View File

@@ -26,6 +26,35 @@
#include "WorldPacket.h"
#include <cmath>
template<class T>
void HashMapHolder<T>::Insert(T* o)
{
std::unique_lock<std::shared_mutex> lock(*GetLock());
m_objectMap[o->GetGUID()] = o;
}
template<class T>
void HashMapHolder<T>::Remove(T* o)
{
std::unique_lock<std::shared_mutex> lock(*GetLock());
m_objectMap.erase(o->GetGUID());
}
template<class T>
T* HashMapHolder<T>::Find(uint64 guid)
{
std::shared_lock<std::shared_mutex> lock(*GetLock());
typename MapType::iterator itr = m_objectMap.find(guid);
return (itr != m_objectMap.end()) ? itr->second : nullptr;
}
template<class T>
std::shared_mutex* HashMapHolder<T>::GetLock()
{
static std::shared_mutex _lock;
return &_lock;
}
ObjectAccessor::ObjectAccessor()
{
}
@@ -190,20 +219,6 @@ Player* ObjectAccessor::FindConnectedPlayer(uint64 const& guid)
Player* ObjectAccessor::FindPlayerByName(std::string const& name, bool checkInWorld)
{
/*ACORE_READ_GUARD(HashMapHolder<Player>::LockType, *HashMapHolder<Player>::GetLock());
std::string nameStr = name;
std::transform(nameStr.begin(), nameStr.end(), nameStr.begin(), ::tolower);
HashMapHolder<Player>::MapType const& m = GetPlayers();
for (HashMapHolder<Player>::MapType::const_iterator iter = m.begin(); iter != m.end(); ++iter)
{
if (!iter->second->IsInWorld())
continue;
std::string currentName = iter->second->GetName();
std::transform(currentName.begin(), currentName.end(), currentName.begin(), ::tolower);
if (nameStr.compare(currentName) == 0)
return iter->second;
}*/
// pussywizard: optimization
std::string nameStr = name;
std::transform(nameStr.begin(), nameStr.end(), nameStr.begin(), ::tolower);
@@ -217,7 +232,8 @@ Player* ObjectAccessor::FindPlayerByName(std::string const& name, bool checkInWo
void ObjectAccessor::SaveAllPlayers()
{
ACORE_READ_GUARD(HashMapHolder<Player>::LockType, *HashMapHolder<Player>::GetLock());
std::shared_lock<std::shared_mutex> lock(*HashMapHolder<Player>::GetLock());
HashMapHolder<Player>::MapType const& m = GetPlayers();
for (HashMapHolder<Player>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr)
itr->second->SaveToDB(false, false);
@@ -225,7 +241,7 @@ void ObjectAccessor::SaveAllPlayers()
Corpse* ObjectAccessor::GetCorpseForPlayerGUID(uint64 guid)
{
ACORE_READ_GUARD(ACE_RW_Thread_Mutex, i_corpseLock);
std::shared_lock<std::shared_mutex> lock(i_corpseLock);
Player2CorpsesMapType::iterator iter = i_player2corpse.find(guid);
if (iter == i_player2corpse.end())
@@ -242,7 +258,7 @@ void ObjectAccessor::RemoveCorpse(Corpse* corpse, bool final)
if (!final)
{
ACORE_WRITE_GUARD(ACE_RW_Thread_Mutex, i_corpseLock);
std::unique_lock<std::shared_mutex> guard(i_corpseLock);
Player2CorpsesMapType::iterator iter = i_player2corpse.find(corpse->GetOwnerGUID());
if (iter == i_player2corpse.end())
return;
@@ -269,7 +285,7 @@ void ObjectAccessor::RemoveCorpse(Corpse* corpse, bool final)
// Critical section
{
ACORE_WRITE_GUARD(ACE_RW_Thread_Mutex, i_corpseLock);
std::unique_lock<std::shared_mutex> guard(i_corpseLock);
// build mapid*cellid -> guid_set map
CellCoord cellCoord = acore::ComputeCellCoord(corpse->GetPositionX(), corpse->GetPositionY());
@@ -285,7 +301,7 @@ void ObjectAccessor::AddCorpse(Corpse* corpse)
// Critical section
{
ACORE_WRITE_GUARD(ACE_RW_Thread_Mutex, i_corpseLock);
std::unique_lock<std::shared_mutex> guard(i_corpseLock);
ASSERT(i_player2corpse.find(corpse->GetOwnerGUID()) == i_player2corpse.end());
i_player2corpse[corpse->GetOwnerGUID()] = corpse;
@@ -298,7 +314,7 @@ void ObjectAccessor::AddCorpse(Corpse* corpse)
void ObjectAccessor::AddCorpsesToGrid(GridCoord const& gridpair, GridType& grid, Map* map)
{
ACORE_READ_GUARD(ACE_RW_Thread_Mutex, i_corpseLock);
std::shared_lock<std::shared_mutex> guard(i_corpseLock);
for (Player2CorpsesMapType::iterator iter = i_player2corpse.begin(); iter != i_player2corpse.end(); ++iter)
{
@@ -386,7 +402,7 @@ Corpse* ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia
}
// pussywizard: for deleting bones
ACORE_WRITE_GUARD(ACE_RW_Thread_Mutex, i_corpseLock);
std::unique_lock<std::shared_mutex> guard(i_corpseLock);
i_playerBones.push_back(bones->GetGUID());
}
@@ -413,7 +429,7 @@ void ObjectAccessor::RemoveOldCorpses()
// pussywizard: for deleting bones
std::list<uint64>::iterator next2;
ACORE_WRITE_GUARD(ACE_RW_Thread_Mutex, i_corpseLock);
std::unique_lock<std::shared_mutex> guard(i_corpseLock);
for (std::list<uint64>::iterator itr = i_playerBones.begin(); itr != i_playerBones.end(); itr = next2)
{
next2 = itr;
@@ -446,13 +462,13 @@ void ObjectAccessor::RemoveOldCorpses()
void ObjectAccessor::AddDelayedCorpseAction(Corpse* corpse, uint8 action, uint32 mapId, uint32 instanceId)
{
ACORE_GUARD(ACE_Thread_Mutex, DelayedCorpseLock);
std::lock_guard<std::mutex> guard(DelayedCorpseLock);
i_delayedCorpseActions.push_back(DelayedCorpseAction(corpse, action, mapId, instanceId));
}
void ObjectAccessor::ProcessDelayedCorpseActions()
{
ACORE_GUARD(ACE_Thread_Mutex, DelayedCorpseLock);
std::lock_guard<std::mutex> guard(DelayedCorpseLock);
for (std::list<DelayedCorpseAction>::iterator itr = i_delayedCorpseActions.begin(); itr != i_delayedCorpseActions.end(); ++itr)
{
DelayedCorpseAction a = (*itr);

View File

@@ -11,8 +11,9 @@
#include "GridDefines.h"
#include "Object.h"
#include "UpdateData.h"
#include <ace/Thread_Mutex.h>
#include <mutex>
#include <set>
#include <shared_mutex>
#include <unordered_map>
class Creature;
@@ -33,43 +34,24 @@ class HashMapHolder
{
public:
typedef std::unordered_map<uint64, T*> MapType;
typedef ACE_RW_Thread_Mutex LockType;
static void Insert(T* o)
{
ACORE_WRITE_GUARD(LockType, i_lock);
m_objectMap[o->GetGUID()] = o;
}
static void Remove(T* o)
{
ACORE_WRITE_GUARD(LockType, i_lock);
m_objectMap.erase(o->GetGUID());
}
static T* Find(uint64 guid)
{
ACORE_READ_GUARD(LockType, i_lock);
typename MapType::iterator itr = m_objectMap.find(guid);
return (itr != m_objectMap.end()) ? itr->second : nullptr;
}
static void Insert(T* o);
static void Remove(T* o);
static T* Find(uint64 guid);
static MapType& GetContainer() { return m_objectMap; }
static LockType* GetLock() { return &i_lock; }
static std::shared_mutex* GetLock();
private:
//Non instanceable only static
HashMapHolder() = default;
static LockType i_lock;
static MapType m_objectMap;
static MapType m_objectMap;
};
/// Define the static members of HashMapHolder
template <class T> std::unordered_map< uint64, T* > HashMapHolder<T>::m_objectMap;
template <class T> typename HashMapHolder<T>::LockType HashMapHolder<T>::i_lock;
// pussywizard:
class DelayedCorpseAction
@@ -226,7 +208,7 @@ public:
//non-static functions
void AddUpdateObject(Object* obj)
{
ACORE_GUARD(ACE_Thread_Mutex, i_objectLock);
std::lock_guard<std::mutex> guard(i_objectLock);
if (obj->GetTypeId() < TYPEID_UNIT) // these are not in map: TYPEID_OBJECT, TYPEID_ITEM, TYPEID_CONTAINER
i_objects.insert(obj);
else
@@ -235,7 +217,7 @@ public:
void RemoveUpdateObject(Object* obj)
{
ACORE_GUARD(ACE_Thread_Mutex, i_objectLock);
std::lock_guard<std::mutex> guard(i_objectLock);
if (obj->GetTypeId() < TYPEID_UNIT) // these are not in map: TYPEID_OBJECT, TYPEID_ITEM, TYPEID_CONTAINER
i_objects.erase(obj);
else
@@ -270,10 +252,10 @@ private:
Player2CorpsesMapType i_player2corpse;
std::list<uint64> i_playerBones;
ACE_Thread_Mutex i_objectLock;
ACE_RW_Thread_Mutex i_corpseLock;
std::mutex i_objectLock;
std::shared_mutex i_corpseLock;
std::list<DelayedCorpseAction> i_delayedCorpseActions;
mutable ACE_Thread_Mutex DelayedCorpseLock;
mutable std::mutex DelayedCorpseLock;
};
#define sObjectAccessor ObjectAccessor::instance()

View File

@@ -6508,7 +6508,7 @@ uint32 ObjectMgr::GenerateMailID()
sLog->outError("Mail ids overflow!! Can't continue, shutting down server. ");
World::StopNow(ERROR_EXIT_CODE);
}
ACORE_GUARD(ACE_Thread_Mutex, _mailIdMutex);
std::lock_guard<std::mutex> guard(_mailIdMutex);
return _mailId++;
}
@@ -6519,25 +6519,25 @@ uint32 ObjectMgr::GenerateLowGuid(HighGuid guidhigh)
case HIGHGUID_ITEM:
{
ASSERT(_hiItemGuid < 0xFFFFFFFE && "Item guid overflow!");
ACORE_GUARD(ACE_Thread_Mutex, _hiItemGuidMutex);
std::lock_guard<std::mutex> guard(_hiItemGuidMutex);
return _hiItemGuid++;
}
case HIGHGUID_UNIT:
{
ASSERT(_hiCreatureGuid < 0x00FFFFFE && "Creature guid overflow!");
ACORE_GUARD(ACE_Thread_Mutex, _hiCreatureGuidMutex);
std::lock_guard<std::mutex> guard(_hiCreatureGuidMutex);
return _hiCreatureGuid++;
}
case HIGHGUID_PET:
{
ASSERT(_hiPetGuid < 0x00FFFFFE && "Pet guid overflow!");
ACORE_GUARD(ACE_Thread_Mutex, _hiPetGuidMutex);
std::lock_guard<std::mutex> guard(_hiPetGuidMutex);
return _hiPetGuid++;
}
case HIGHGUID_VEHICLE:
{
ASSERT(_hiVehicleGuid < 0x00FFFFFF && "Vehicle guid overflow!");
ACORE_GUARD(ACE_Thread_Mutex, _hiVehicleGuidMutex);
std::lock_guard<std::mutex> guard(_hiVehicleGuidMutex);
return _hiVehicleGuid++;
}
case HIGHGUID_PLAYER:
@@ -6548,25 +6548,25 @@ uint32 ObjectMgr::GenerateLowGuid(HighGuid guidhigh)
case HIGHGUID_GAMEOBJECT:
{
ASSERT(_hiGoGuid < 0x00FFFFFE && "Gameobject guid overflow!");
ACORE_GUARD(ACE_Thread_Mutex, _hiGoGuidMutex);
std::lock_guard<std::mutex> guard(_hiGoGuidMutex);
return _hiGoGuid++;
}
case HIGHGUID_CORPSE:
{
ASSERT(_hiCorpseGuid < 0xFFFFFFFE && "Corpse guid overflow!");
ACORE_GUARD(ACE_Thread_Mutex, _hiCorpseGuidMutex);
std::lock_guard<std::mutex> guard(_hiCorpseGuidMutex);
return _hiCorpseGuid++;
}
case HIGHGUID_DYNAMICOBJECT:
{
ASSERT(_hiDoGuid < 0xFFFFFFFE && "DynamicObject guid overflow!");
ACORE_GUARD(ACE_Thread_Mutex, _hiDoGuidMutex);
std::lock_guard<std::mutex> guard(_hiDoGuidMutex);
return _hiDoGuid++;
}
case HIGHGUID_MO_TRANSPORT:
{
ASSERT(_hiMoTransGuid < 0xFFFFFFFE && "MO Transport guid overflow!");
ACORE_GUARD(ACE_Thread_Mutex, _hiMoTransGuidMutex);
std::lock_guard<std::mutex> guard(_hiMoTransGuidMutex);
return _hiMoTransGuid++;
}
default:
@@ -7047,7 +7047,7 @@ std::string ObjectMgr::GeneratePetName(uint32 entry)
uint32 ObjectMgr::GeneratePetNumber()
{
ACORE_GUARD(ACE_Thread_Mutex, _hiPetNumberMutex);
std::lock_guard<std::mutex> guard(_hiPetNumberMutex);
return ++_hiPetNumber;
}

View File

@@ -1353,28 +1353,28 @@ private:
uint64 _equipmentSetGuid; // pussywizard: accessed by a single thread
uint32 _itemTextId; // pussywizard: unused? xD
uint32 _mailId;
ACE_Thread_Mutex _mailIdMutex;
std::mutex _mailIdMutex;
uint32 _hiPetNumber;
ACE_Thread_Mutex _hiPetNumberMutex;
std::mutex _hiPetNumberMutex;
// first free low guid for selected guid type
uint32 _hiCharGuid; // pussywizard: accessed by a single thread
uint32 _hiCreatureGuid;
ACE_Thread_Mutex _hiCreatureGuidMutex;
std::mutex _hiCreatureGuidMutex;
uint32 _hiPetGuid;
ACE_Thread_Mutex _hiPetGuidMutex;
std::mutex _hiPetGuidMutex;
uint32 _hiVehicleGuid;
ACE_Thread_Mutex _hiVehicleGuidMutex;
std::mutex _hiVehicleGuidMutex;
uint32 _hiItemGuid;
ACE_Thread_Mutex _hiItemGuidMutex;
std::mutex _hiItemGuidMutex;
uint32 _hiGoGuid;
ACE_Thread_Mutex _hiGoGuidMutex;
std::mutex _hiGoGuidMutex;
uint32 _hiDoGuid;
ACE_Thread_Mutex _hiDoGuidMutex;
std::mutex _hiDoGuidMutex;
uint32 _hiCorpseGuid;
ACE_Thread_Mutex _hiCorpseGuidMutex;
std::mutex _hiCorpseGuidMutex;
uint32 _hiMoTransGuid;
ACE_Thread_Mutex _hiMoTransGuidMutex;
std::mutex _hiMoTransGuidMutex;
uint32 _hiCreatureRecycledGuidMax;
uint32 _hiCreatureRecycledGuid;