Thread safety

This commit is contained in:
Pilzinsel64
2025-07-31 06:37:25 +02:00
parent 231546facd
commit f4530aee4f
5 changed files with 43 additions and 24 deletions

View File

@@ -9,21 +9,27 @@ public abstract class JsonDataContainer
public virtual HashSet<T>? Set<T>(bool allowCreate) where T : class, IDataObject public virtual HashSet<T>? Set<T>(bool allowCreate) where T : class, IDataObject
{ {
var list = sets.OfType<HashSet<T>>().FirstOrDefault(); lock (sets)
if (list == null && allowCreate)
{ {
list = []; var list = sets.OfType<HashSet<T>>().FirstOrDefault();
sets.Add(list);
}
return list; if (list == null && allowCreate)
{
list = [];
sets.Add(list);
}
return list;
}
} }
public virtual void CopyTo(JsonDataContainer container) public virtual void CopyTo(JsonDataContainer container)
{ {
foreach (var set in sets) lock (sets)
container.sets.Add(set); {
foreach (var set in sets)
container.sets.Add(set);
}
} }
protected virtual JsonSerializer GetSerializer() protected virtual JsonSerializer GetSerializer()

View File

@@ -21,9 +21,7 @@ public class JsonDataManager(JsonDataContainer container) : DataManager
protected override void UpdateEntity<T>(T obj) protected override void UpdateEntity<T>(T obj)
{ {
var set = container.Set<T>(true); container.Set<T>(true).Add(obj);
if (!set.Contains(obj))
set.Add(obj);
} }
protected override void SaveChanges() protected override void SaveChanges()

View File

@@ -2,15 +2,23 @@
public class JsonFileContainer(string filePath) : JsonDataContainer public class JsonFileContainer(string filePath) : JsonDataContainer
{ {
protected readonly object _lock_FileAction = new();
public override void Read() public override void Read()
{ {
using var sw = new StreamReader(filePath); lock (_lock_FileAction)
GetSerializer().Populate(sw, this); {
using var sw = new StreamReader(filePath);
GetSerializer().Populate(sw, this);
}
} }
public override void Flush() public override void Flush()
{ {
using var sw = new StreamWriter(filePath, false); lock (_lock_FileAction)
GetSerializer().Serialize(sw, this); {
using var sw = new StreamWriter(filePath, false);
GetSerializer().Serialize(sw, this);
}
} }
} }

View File

@@ -2,18 +2,25 @@
public class JsonStreamContainer(Stream stream) : JsonDataContainer public class JsonStreamContainer(Stream stream) : JsonDataContainer
{ {
protected readonly object _lock_StreamAction = new();
public override void Read() public override void Read()
{ {
using var sw = new StreamReader(stream); lock (_lock_StreamAction)
stream.Position = 0; {
GetSerializer().Populate(sw, this); using var sw = new StreamReader(stream);
stream.Position = 0;
GetSerializer().Populate(sw, this);
}
} }
public override void Flush() public override void Flush()
{ {
using var sw = new StreamWriter(stream); lock (_lock_StreamAction)
stream.Position = 0; {
GetSerializer().Serialize(sw, this); using var sw = new StreamWriter(stream);
stream.SetLength(stream.Position); stream.Position = 0;
GetSerializer().Serialize(sw, this);
stream.SetLength(stream.Position);
}
} }
} }

View File

@@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>annotations</Nullable> <Nullable>annotations</Nullable>
<Version>2.4.2</Version> <Version>2.4.3</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>