Skip to content

Commit 0a85bfe

Browse files
CHEROratkosrb
authored andcommitted
Improve cell and map object guid guards.
1 parent 92a943b commit 0a85bfe

4 files changed

Lines changed: 56 additions & 28 deletions

File tree

src/game/Maps/MapPersistentStateMgr.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ class MapPersistentState
107107
bool IsSpawnedPoolObject(uint32 db_guid_or_pool_id) { return GetSpawnedPoolData().IsSpawnedObject<T>(db_guid_or_pool_id); }
108108

109109
// grid objects (Dynamic map/instance specific added/removed grid spawns from pool system/etc)
110-
MapCellObjectGuids const& GetCellObjectGuids(uint32 cell_id) { return m_gridObjectGuids[cell_id]; }
110+
MapCellObjectGuids const* GetCellObjectGuids(uint32 cell_id) const
111+
{
112+
auto itr = m_gridObjectGuids.find(cell_id);
113+
if (itr != m_gridObjectGuids.end())
114+
return &itr->second;
115+
116+
return nullptr;
117+
}
111118
void AddCreatureToGrid(uint32 guid, CreatureData const* data);
112119
void RemoveCreatureFromGrid(uint32 guid, CreatureData const* data);
113120
void AddGameobjectToGrid(uint32 guid, GameObjectData const* data);

src/game/ObjectGridLoader.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,19 @@ ObjectGridLoader::Visit(GameObjectMapType& m)
223223
uint32 y = (i_cell.GridY() * MAX_NUMBER_OF_CELLS) + i_cell.CellY();
224224
CellPair cell_pair(x, y);
225225
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
226-
227-
CellObjectGuids const& cell_guids = sObjectMgr.GetCellObjectGuids(i_map->GetId(), cell_id);
228-
229226
GridType& grid = (*i_map->getNGrid(i_cell.GridX(), i_cell.GridY()))(i_cell.CellX(), i_cell.CellY());
230-
LoadHelper(cell_guids.gameobjects, cell_pair, m, i_gameObjects, i_map, grid);
231-
std::shared_lock<std::shared_timed_mutex> lock(i_map->GetPersistentState()->GetCellObjectGuidsMutex());
232-
LoadHelper(i_map->GetPersistentState()->GetCellObjectGuids(cell_id).gameobjects, cell_pair, m, i_gameObjects, i_map, grid);
227+
228+
{
229+
std::shared_lock<std::shared_timed_mutex> lock(sObjectMgr.GetCellLoadingObjectsMutex());
230+
if (auto const& cellGuids = sObjectMgr.GetCellObjectGuids(i_map->GetId(), cell_id))
231+
LoadHelper(cellGuids->gameobjects, cell_pair, m, i_gameObjects, i_map, grid);
232+
}
233+
234+
{
235+
std::shared_lock<std::shared_timed_mutex> lock(i_map->GetPersistentState()->GetCellObjectGuidsMutex());
236+
if (auto const& cellGuids = i_map->GetPersistentState()->GetCellObjectGuids(cell_id))
237+
LoadHelper(cellGuids->gameobjects, cell_pair, m, i_gameObjects, i_map, grid);
238+
}
233239
}
234240

235241
void
@@ -239,13 +245,19 @@ ObjectGridLoader::Visit(CreatureMapType& m)
239245
uint32 y = (i_cell.GridY() * MAX_NUMBER_OF_CELLS) + i_cell.CellY();
240246
CellPair cell_pair(x, y);
241247
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
242-
243-
CellObjectGuids const& cell_guids = sObjectMgr.GetCellObjectGuids(i_map->GetId(), cell_id);
244-
245248
GridType& grid = (*i_map->getNGrid(i_cell.GridX(), i_cell.GridY()))(i_cell.CellX(), i_cell.CellY());
246-
LoadHelper(cell_guids.creatures, cell_pair, m, i_creatures, i_map, grid);
247-
std::shared_lock<std::shared_timed_mutex> lock(i_map->GetPersistentState()->GetCellObjectGuidsMutex());
248-
LoadHelper(i_map->GetPersistentState()->GetCellObjectGuids(cell_id).creatures, cell_pair, m, i_creatures, i_map, grid);
249+
250+
{
251+
std::shared_lock<std::shared_timed_mutex> lock(sObjectMgr.GetCellLoadingObjectsMutex());
252+
if (auto const& cellGuids = sObjectMgr.GetCellObjectGuids(i_map->GetId(), cell_id))
253+
LoadHelper(cellGuids->creatures, cell_pair, m, i_creatures, i_map, grid);
254+
}
255+
256+
{
257+
std::shared_lock<std::shared_timed_mutex> lock(i_map->GetPersistentState()->GetCellObjectGuidsMutex());
258+
if (auto const& cellGuids = i_map->GetPersistentState()->GetCellObjectGuids(cell_id))
259+
LoadHelper(cellGuids->creatures, cell_pair, m, i_creatures, i_map, grid);
260+
}
249261
}
250262

251263
void
@@ -255,10 +267,13 @@ ObjectWorldLoader::Visit(CorpseMapType& m)
255267
uint32 y = (i_cell.GridY() * MAX_NUMBER_OF_CELLS) + i_cell.CellY();
256268
CellPair cell_pair(x, y);
257269
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
258-
259-
CellObjectGuids const& cell_guids = sObjectMgr.GetCellObjectGuids(i_map->GetId(), cell_id);
260270
GridType& grid = (*i_map->getNGrid(i_cell.GridX(), i_cell.GridY()))(i_cell.CellX(), i_cell.CellY());
261-
LoadHelper(cell_guids.corpses, cell_pair, m, i_corpses, i_map, grid);
271+
272+
{
273+
std::shared_lock<std::shared_timed_mutex> lock(sObjectMgr.GetCellLoadingObjectsMutex());
274+
if (auto const& cellGuids = sObjectMgr.GetCellObjectGuids(i_map->GetId(), cell_id))
275+
LoadHelper(cellGuids->corpses, cell_pair, m, i_corpses, i_map, grid);
276+
}
262277
}
263278

264279
void

src/game/ObjectMgr.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,7 +2483,7 @@ void ObjectMgr::AddCreatureToGrid(uint32 guid, CreatureData const* data)
24832483
CellPair cell_pair = MaNGOS::ComputeCellPair(data->position.x, data->position.y);
24842484
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
24852485

2486-
std::unique_lock<std::mutex> lock(m_MapObjectGuids_lock);
2486+
std::unique_lock<std::shared_timed_mutex> lock(m_MapObjectGuids_lock);
24872487
CellObjectGuids& cell_guids = m_MapObjectGuids[data->position.mapId][cell_id];
24882488
cell_guids.creatures.insert(guid);
24892489
}
@@ -2493,7 +2493,7 @@ void ObjectMgr::RemoveCreatureFromGrid(uint32 guid, CreatureData const* data)
24932493
CellPair cell_pair = MaNGOS::ComputeCellPair(data->position.x, data->position.y);
24942494
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
24952495

2496-
std::unique_lock<std::mutex> lock(m_MapObjectGuids_lock);
2496+
std::unique_lock<std::shared_timed_mutex> lock(m_MapObjectGuids_lock);
24972497
CellObjectGuids& cell_guids = m_MapObjectGuids[data->position.mapId][cell_id];
24982498
cell_guids.creatures.erase(guid);
24992499
}
@@ -2656,7 +2656,7 @@ void ObjectMgr::AddGameobjectToGrid(uint32 guid, GameObjectData const* data)
26562656
CellPair cell_pair = MaNGOS::ComputeCellPair(data->position.x, data->position.y);
26572657
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
26582658

2659-
std::unique_lock<std::mutex> lock(m_MapObjectGuids_lock);
2659+
std::unique_lock<std::shared_timed_mutex> lock(m_MapObjectGuids_lock);
26602660
CellObjectGuids& cell_guids = m_MapObjectGuids[data->position.mapId][cell_id];
26612661
cell_guids.gameobjects.insert(guid);
26622662
}
@@ -2666,7 +2666,7 @@ void ObjectMgr::RemoveGameobjectFromGrid(uint32 guid, GameObjectData const* data
26662666
CellPair cell_pair = MaNGOS::ComputeCellPair(data->position.x, data->position.y);
26672667
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
26682668

2669-
std::unique_lock<std::mutex> lock(m_MapObjectGuids_lock);
2669+
std::unique_lock<std::shared_timed_mutex> lock(m_MapObjectGuids_lock);
26702670
CellObjectGuids& cell_guids = m_MapObjectGuids[data->position.mapId][cell_id];
26712671
cell_guids.gameobjects.erase(guid);
26722672
}
@@ -8868,14 +8868,15 @@ void ObjectMgr::DeleteGOData(uint32 guid)
88688868
void ObjectMgr::AddCorpseCellData(uint32 mapid, uint32 cellid, uint32 player_guid, uint32 instance)
88698869
{
88708870
// corpses are always added to spawn mode 0 and they are spawned by their instance id
8871-
std::unique_lock<std::mutex> lock(m_MapObjectGuids_lock);
8871+
std::unique_lock<std::shared_timed_mutex> lock(m_MapObjectGuids_lock);
88728872
CellObjectGuids& cell_guids = m_MapObjectGuids[mapid][cellid];
88738873
cell_guids.corpses[player_guid] = instance;
88748874
}
88758875

88768876
void ObjectMgr::DeleteCorpseCellData(uint32 mapid, uint32 cellid, uint32 player_guid)
88778877
{
88788878
// corpses are always added to spawn mode 0 and they are spawned by their instance id
8879+
std::unique_lock<std::shared_timed_mutex> lock(m_MapObjectGuids_lock);
88798880
CellObjectGuids& cell_guids = m_MapObjectGuids[mapid][cellid];
88808881
cell_guids.corpses.erase(player_guid);
88818882
}

src/game/ObjectMgr.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,13 +1176,18 @@ class ObjectMgr
11761176
}
11771177

11781178
// global grid objects state (static DB spawns, global spawn mods from gameevent system)
1179-
CellObjectGuids const& GetCellObjectGuids(uint16 mapid, uint32 cell_id)
1180-
{
1181-
std::unique_lock<std::mutex> lock(m_MapObjectGuids_lock);
1182-
CellObjectGuids const& guids = m_MapObjectGuids[mapid][cell_id];
1183-
return guids;
1179+
CellObjectGuids const* GetCellObjectGuids(uint16 mapid, uint32 cell_id) const
1180+
{
1181+
auto itr = m_MapObjectGuids.find(mapid);
1182+
if (itr != m_MapObjectGuids.end())
1183+
{
1184+
auto itr2 = itr->second.find(cell_id);
1185+
if (itr2 != itr->second.end())
1186+
return &itr2->second;
1187+
}
1188+
return nullptr;
11841189
}
1185-
std::mutex& GetCellLoadingObjectsMutex() // TODO: Mutex per cell?
1190+
std::shared_timed_mutex& GetCellLoadingObjectsMutex() // TODO: Mutex per cell?
11861191
{
11871192
return m_MapObjectGuids_lock;
11881193
}
@@ -1573,7 +1578,7 @@ class ObjectMgr
15731578
HalfNameMap m_PetHalfNameMap1;
15741579

15751580
MapObjectGuids m_MapObjectGuids;
1576-
std::mutex m_MapObjectGuids_lock;
1581+
std::shared_timed_mutex m_MapObjectGuids_lock;
15771582

15781583
AreaTriggerLocaleMap m_AreaTriggerLocaleMap;
15791584
CreatureDataMap m_CreatureDataMap;

0 commit comments

Comments
 (0)