Thread safety
This commit is contained in:
@@ -9,21 +9,27 @@ public abstract class JsonDataContainer
|
||||
|
||||
public virtual HashSet<T>? Set<T>(bool allowCreate) where T : class, IDataObject
|
||||
{
|
||||
var list = sets.OfType<HashSet<T>>().FirstOrDefault();
|
||||
|
||||
if (list == null && allowCreate)
|
||||
lock (sets)
|
||||
{
|
||||
list = [];
|
||||
sets.Add(list);
|
||||
}
|
||||
var list = sets.OfType<HashSet<T>>().FirstOrDefault();
|
||||
|
||||
return list;
|
||||
if (list == null && allowCreate)
|
||||
{
|
||||
list = [];
|
||||
sets.Add(list);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void CopyTo(JsonDataContainer container)
|
||||
{
|
||||
foreach (var set in sets)
|
||||
container.sets.Add(set);
|
||||
lock (sets)
|
||||
{
|
||||
foreach (var set in sets)
|
||||
container.sets.Add(set);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual JsonSerializer GetSerializer()
|
||||
|
||||
@@ -21,9 +21,7 @@ public class JsonDataManager(JsonDataContainer container) : DataManager
|
||||
|
||||
protected override void UpdateEntity<T>(T obj)
|
||||
{
|
||||
var set = container.Set<T>(true);
|
||||
if (!set.Contains(obj))
|
||||
set.Add(obj);
|
||||
container.Set<T>(true).Add(obj);
|
||||
}
|
||||
|
||||
protected override void SaveChanges()
|
||||
|
||||
@@ -2,15 +2,23 @@
|
||||
|
||||
public class JsonFileContainer(string filePath) : JsonDataContainer
|
||||
{
|
||||
protected readonly object _lock_FileAction = new();
|
||||
|
||||
public override void Read()
|
||||
{
|
||||
using var sw = new StreamReader(filePath);
|
||||
GetSerializer().Populate(sw, this);
|
||||
lock (_lock_FileAction)
|
||||
{
|
||||
using var sw = new StreamReader(filePath);
|
||||
GetSerializer().Populate(sw, this);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
using var sw = new StreamWriter(filePath, false);
|
||||
GetSerializer().Serialize(sw, this);
|
||||
lock (_lock_FileAction)
|
||||
{
|
||||
using var sw = new StreamWriter(filePath, false);
|
||||
GetSerializer().Serialize(sw, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,18 +2,25 @@
|
||||
|
||||
public class JsonStreamContainer(Stream stream) : JsonDataContainer
|
||||
{
|
||||
protected readonly object _lock_StreamAction = new();
|
||||
public override void Read()
|
||||
{
|
||||
using var sw = new StreamReader(stream);
|
||||
stream.Position = 0;
|
||||
GetSerializer().Populate(sw, this);
|
||||
lock (_lock_StreamAction)
|
||||
{
|
||||
using var sw = new StreamReader(stream);
|
||||
stream.Position = 0;
|
||||
GetSerializer().Populate(sw, this);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
using var sw = new StreamWriter(stream);
|
||||
stream.Position = 0;
|
||||
GetSerializer().Serialize(sw, this);
|
||||
stream.SetLength(stream.Position);
|
||||
lock (_lock_StreamAction)
|
||||
{
|
||||
using var sw = new StreamWriter(stream);
|
||||
stream.Position = 0;
|
||||
GetSerializer().Serialize(sw, this);
|
||||
stream.SetLength(stream.Position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>annotations</Nullable>
|
||||
<Version>2.4.2</Version>
|
||||
<Version>2.4.3</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user