Impl Thread Safety for static methods in EcsWorld

This commit is contained in:
Mikhail 2024-09-09 14:25:32 +08:00
parent 44a8894d65
commit effa02e660
2 changed files with 127 additions and 99 deletions

View File

@ -133,6 +133,8 @@ namespace DCFApixels.DragonECS
#region Constructors/Destroy #region Constructors/Destroy
public EcsWorld(EcsWorldConfig config, short worldID = -1) : this(config == null ? ConfigContainer.Empty : new ConfigContainer().Set(config), worldID) { } public EcsWorld(EcsWorldConfig config, short worldID = -1) : this(config == null ? ConfigContainer.Empty : new ConfigContainer().Set(config), worldID) { }
public EcsWorld(IConfigContainer configs = null, short worldID = -1) public EcsWorld(IConfigContainer configs = null, short worldID = -1)
{
lock (_lock)
{ {
if (configs == null) { configs = ConfigContainer.Empty; } if (configs == null) { configs = ConfigContainer.Empty; }
bool nullWorld = this is NullWorld; bool nullWorld = this is NullWorld;
@ -175,7 +177,10 @@ namespace DCFApixels.DragonECS
GetComponentTypeID<NullComponent>(); GetComponentTypeID<NullComponent>();
} }
}
public void Destroy() public void Destroy()
{
lock (_lock)
{ {
if (_isDestroyed) if (_isDestroyed)
{ {
@ -206,6 +211,7 @@ namespace DCFApixels.DragonECS
} }
//_entities - не обнуляется для работы entlong.IsAlive //_entities - не обнуляется для работы entlong.IsAlive
} }
}
//public void Clear() { } //public void Clear() { }
#endregion #endregion

View File

@ -26,29 +26,30 @@ namespace DCFApixels.DragonECS
private const int DEL_ENT_BUFFER_MIN_SIZE = 64; private const int DEL_ENT_BUFFER_MIN_SIZE = 64;
private static EcsWorld[] _worlds = Array.Empty<EcsWorld>(); private static EcsWorld[] _worlds = Array.Empty<EcsWorld>();
private static IdDispenser _worldIdDispenser = new IdDispenser(4, 0, OnWorldIdDispenser); private static IdDispenser _worldIdDispenser = new IdDispenser(4, 0, n => Array.Resize(ref _worlds, n));
private static List<DataReleaser> _dataReleaseres = new List<DataReleaser>(); private static List<DataReleaser> _dataReleaseres = new List<DataReleaser>();
//public static int Copacity => Worlds.Length; //public static int Copacity => Worlds.Length;
private static readonly object _lock = new object();
static EcsWorld() static EcsWorld()
{ {
_worlds[NULL_WORLD_ID] = new NullWorld(); _worlds[NULL_WORLD_ID] = new NullWorld();
} }
private static void ReleaseData(int worldID) private static void ReleaseData(int worldID)
{// ts
lock (_lock)
{ {
for (int i = 0, iMax = _dataReleaseres.Count; i < iMax; i++) for (int i = 0, iMax = _dataReleaseres.Count; i < iMax; i++)
{ {
_dataReleaseres[i].Release(worldID); _dataReleaseres[i].Release(worldID);
} }
} }
public static void OnWorldIdDispenser(int newSize)
{
Array.Resize(ref _worlds, newSize);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsWorld GetWorld(short worldID) public static EcsWorld GetWorld(short worldID)
{ {// ts
return _worlds[worldID]; return _worlds[worldID];
} }
@ -63,6 +64,7 @@ namespace DCFApixels.DragonECS
return ref WorldComponentPool<T>.GetForWorldUnchecked(worldID); return ref WorldComponentPool<T>.GetForWorldUnchecked(worldID);
} }
private abstract class DataReleaser private abstract class DataReleaser
{ {
public abstract void Release(int worldID); public abstract void Release(int worldID);
@ -76,32 +78,45 @@ namespace DCFApixels.DragonECS
private static short _recycledItemsCount; private static short _recycledItemsCount;
private static IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance; private static IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance;
private static readonly object _lock = new object();
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T Get(int itemIndex) public static ref T Get(int itemIndex)
{ {// ts
return ref _items[itemIndex]; return ref _items[itemIndex];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetForWorld(int worldID) public static ref T GetForWorld(int worldID)
{ {// зависит от GetItemIndex
int index = GetItemIndex(worldID); return ref Get(GetItemIndex(worldID));
return ref _items[index];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetForWorldUnchecked(int worldID) public static ref T GetForWorldUnchecked(int worldID)
{ {// ts
#if (DEBUG && !DISABLE_DEBUG) #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (_mapping[worldID] <= 0) { Throw.UndefinedException(); } if (_mapping[worldID] <= 0) { Throw.UndefinedException(); }
#endif #endif
return ref _items[_mapping[worldID]]; return ref _items[_mapping[worldID]];
} }
public static int GetItemIndex(int worldID) public static int GetItemIndex(int worldID)
{// ts
if (_mapping.Length < _worlds.Length)
{
lock (_lock)
{ {
if (_mapping.Length < _worlds.Length) if (_mapping.Length < _worlds.Length)
{ {
Array.Resize(ref _mapping, _worlds.Length); Array.Resize(ref _mapping, _worlds.Length);
} }
ref short itemIndex = ref _mapping[worldID]; }
}
short itemIndex = _mapping[worldID];
if (itemIndex == 0)
{
lock (_lock)
{
itemIndex = _mapping[worldID];
if (itemIndex <= 0) if (itemIndex <= 0)
{ {
if (_recycledItemsCount > 0) if (_recycledItemsCount > 0)
@ -113,6 +128,8 @@ namespace DCFApixels.DragonECS
{ {
itemIndex = ++_count; itemIndex = ++_count;
} }
_mapping[worldID] = itemIndex;
if (_items.Length <= itemIndex) if (_items.Length <= itemIndex)
{ {
Array.Resize(ref _items, _items.Length << 1); Array.Resize(ref _items, _items.Length << 1);
@ -120,9 +137,13 @@ namespace DCFApixels.DragonECS
_interface.Init(ref _items[itemIndex], _worlds[worldID]); _interface.Init(ref _items[itemIndex], _worlds[worldID]);
_dataReleaseres.Add(new Releaser()); _dataReleaseres.Add(new Releaser());
} }
}
}
return itemIndex; return itemIndex;
} }
private static void Release(int worldID) private static void Release(int worldID)
{// ts
lock (_lock)
{ {
if (_mapping.Length < _worlds.Length) if (_mapping.Length < _worlds.Length)
{ {
@ -136,6 +157,7 @@ namespace DCFApixels.DragonECS
itemIndex = 0; itemIndex = 0;
} }
} }
}
private sealed class Releaser : DataReleaser private sealed class Releaser : DataReleaser
{ {
public sealed override void Release(int worldID) public sealed override void Release(int worldID)