Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Lock vs ReaderWriterLockSlim #4154

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
385 changes: 70 additions & 315 deletions Source/ACE.Database/Models/Shard/CharacterExtensions.cs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Source/ACE.Database/SerializedShardDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void GetSequenceGaps(uint min, uint limitAvailableIDsReturned, Action<Lis
}


public void SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock, Action<bool> callback)
public void SaveBiota(ACE.Entity.Models.Biota biota, Object rwLock, Action<bool> callback)
{
_queue.Add(new Task(() =>
{
Expand All @@ -126,7 +126,7 @@ public void SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock
}


public void SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> biotas, Action<bool> callback, bool doNotAddToCache = false)
public void SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, Object rwLock)> biotas, Action<bool> callback, bool doNotAddToCache = false)
{
_queue.Add(new Task(() =>
{
Expand Down Expand Up @@ -220,7 +220,7 @@ public void GetCharacter(uint characterId, Action<Character> callback)
}));
}

public void SaveCharacter(Character character, ReaderWriterLockSlim rwLock, Action<bool> callback)
public void SaveCharacter(Character character, Object rwLock, Action<bool> callback)
{
_queue.Add(new Task(() =>
{
Expand All @@ -245,7 +245,7 @@ public void SetCharacterAccessLevelByName(string name, AccessLevel accessLevel,
}


public void AddCharacterInParallel(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim biotaLock, IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> possessions, Character character, ReaderWriterLockSlim characterLock, Action<bool> callback)
public void AddCharacterInParallel(ACE.Entity.Models.Biota biota, Object biotaLock, IEnumerable<(ACE.Entity.Models.Biota biota, Object rwLock)> possessions, Character character, Object characterLock, Action<bool> callback)
{
_queue.Add(new Task(() =>
{
Expand Down
31 changes: 8 additions & 23 deletions Source/ACE.Database/ShardDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,13 @@ protected bool DoSaveBiota(ShardDbContext context, Biota biota)
}
}

public virtual bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock, bool doNotAddToCache = false)
public virtual bool SaveBiota(ACE.Entity.Models.Biota biota, Object rwLock, bool doNotAddToCache = false)
{
using (var context = new ShardDbContext())
{
var existingBiota = GetBiota(context, biota.Id, doNotAddToCache);

rwLock.EnterReadLock();
try
lock (rwLock)
{
if (existingBiota == null)
{
Expand All @@ -329,16 +328,12 @@ public virtual bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSli
ACE.Database.Adapter.BiotaUpdater.UpdateDatabaseBiota(context, biota, existingBiota);
}
}
finally
{
rwLock.ExitReadLock();
}

return DoSaveBiota(context, existingBiota);
}
}

public bool SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> biotas, bool doNotAddToCache = false)
public bool SaveBiotasInParallel(IEnumerable<(ACE.Entity.Models.Biota biota, Object rwLock)> biotas, bool doNotAddToCache = false)
{
var result = true;

Expand Down Expand Up @@ -635,12 +630,11 @@ public Character GetCharacterStubByGuid(uint guid)
return result;
}

public bool SaveCharacter(Character character, ReaderWriterLockSlim rwLock)
public bool SaveCharacter(Character character, Object rwLock)
{
if (CharacterContexts.TryGetValue(character, out var cachedContext))
{
rwLock.EnterReadLock();
try
lock (rwLock)
{
Exception firstException = null;
retry:
Expand Down Expand Up @@ -668,18 +662,13 @@ public bool SaveCharacter(Character character, ReaderWriterLockSlim rwLock)
return false;
}
}
finally
{
rwLock.ExitReadLock();
}
}
}

var context = new ShardDbContext();

CharacterContexts.Add(character, context);

rwLock.EnterReadLock();
try
lock (rwLock)
{
context.Character.Add(character);

Expand Down Expand Up @@ -709,14 +698,10 @@ public bool SaveCharacter(Character character, ReaderWriterLockSlim rwLock)
return false;
}
}
finally
{
rwLock.ExitReadLock();
}
}


public bool AddCharacterInParallel(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim biotaLock, IEnumerable<(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock)> possessions, Character character, ReaderWriterLockSlim characterLock)
public bool AddCharacterInParallel(ACE.Entity.Models.Biota biota, Object biotaLock, IEnumerable<(ACE.Entity.Models.Biota biota, Object rwLock)> possessions, Character character, Object characterLock)
{
if (!SaveBiota(biota, biotaLock))
return false; // Biota save failed which mean Character fails.
Expand Down
16 changes: 3 additions & 13 deletions Source/ACE.Database/ShardDatabaseWithCaching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public override Biota GetBiota(uint id, bool doNotAddToCache = false)
return base.GetBiota(id, doNotAddToCache);
}

public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSlim rwLock, bool doNotAddToCache = false)
public override bool SaveBiota(ACE.Entity.Models.Biota biota, Object rwLock, bool doNotAddToCache = false)
{
CacheObject<Biota> cachedBiota;

Expand All @@ -145,15 +145,10 @@ public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSl
{
cachedBiota.LastSeen = DateTime.UtcNow;

rwLock.EnterReadLock();
try
lock (rwLock)
{
ACE.Database.Adapter.BiotaUpdater.UpdateDatabaseBiota(cachedBiota.Context, biota, cachedBiota.CachedObject);
}
finally
{
rwLock.ExitReadLock();
}

return DoSaveBiota(cachedBiota.Context, cachedBiota.CachedObject);
}
Expand All @@ -164,8 +159,7 @@ public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSl

var existingBiota = base.GetBiota(context, biota.Id, doNotAddToCache);

rwLock.EnterReadLock();
try
lock (rwLock)
{
if (existingBiota == null)
{
Expand All @@ -178,10 +172,6 @@ public override bool SaveBiota(ACE.Entity.Models.Biota biota, ReaderWriterLockSl
ACE.Database.Adapter.BiotaUpdater.UpdateDatabaseBiota(context, biota, existingBiota);
}
}
finally
{
rwLock.ExitReadLock();
}

if (DoSaveBiota(context, existingBiota))
{
Expand Down
Loading