I have been wondering for a while about what script hooks are thread safe and what are not.
I know for example that from void ScriptedAI::UpdateAI(uint32 diff) it is not safe to modify (and access) a global std::map as then multiple creatures on different maps could access the map at the same time.
For just accessing it could be safe as it is said(?) that a map is safe to go through if it is not modified, but not if edited in multithreaded environment.
When one is making a script or contemplating on how the system works, there is no information about what is thread safe and what is not.
A thought was first that the creature AI hooks would not be safe to be used, as they are (all?) processed on map update, which is done for different maps simultaneously.
But this fell as I noticed that OnPlayerKilledByCreature hook triggers on kill. This means that it is likely also processed on map update.
Obviously any function /can/ be called at will, but the base structure is unclear.
Is there any base idea/structure?
Would this be needed to considered individually for each hook?
Does anyone know / have an idea about what hooks are executed in safe environment and what are not?
I believe thread safety among custom scripts is most likely lacking a lot in many cases.
ps. just realized this should be in TrinityCore Code Discussion
I think this problem can be somewhat alleviated if we forbid storing WorldObject* in scripts, i.e, replace all Creature*, Player*, etc by their guids and when you actually need a WO* use the guid accessors “in-place”.
I thought that was not safe (with null mutex) and thus the mutex locks were implemented on the hash tables in object accessor but guess that was wrong assumption.
Anyways, the question was more about the current structure, not about a workaround.
So currently I guess the answer is that the safe and unsafe hooks are mixed.