-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a lock on most interactions with game objects
We see rare problems where multiple messages on the same game arriving at the same game cause issues. This commit adds a coarse-grained lock on all interactions with a game by adding a binary semaphore. Because manually adding try{} finally{} blocks seems verbose, I added a RAII-style LockGuard that locks the semaphore on creation and drops it when disposed.
- Loading branch information
1 parent
c592bd1
commit a544425
Showing
5 changed files
with
83 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Impostor.Api.Net | ||
{ | ||
/// <summary>RAII-style lock guard for use in Impostor.</summary> | ||
/// <remarks> | ||
/// To lock a semaphore, use the LockAsync method and await the result. | ||
/// To unlock the semaphore, dispose of the LockGuard. | ||
/// </remarks> | ||
public sealed class LockGuard : IDisposable | ||
{ | ||
private readonly SemaphoreSlim _semaphore; | ||
|
||
private bool _disposedValue; | ||
|
||
private LockGuard(SemaphoreSlim semaphore) | ||
{ | ||
_semaphore = semaphore; | ||
} | ||
|
||
private void Dispose(bool disposing) | ||
{ | ||
if (!_disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
_semaphore.Release(); | ||
} | ||
|
||
_disposedValue = true; | ||
} | ||
} | ||
|
||
/// <summary>Create a RAII-style lock guard for an instance of <see cref="SemaphoreSlim"/>.</summary> | ||
/// <param name="semaphore">The semaphore to Wait and Release on.</param> | ||
/// <param name="timeout">The amount of milliseconds to wait for the lock becoming available.</param> | ||
/// <exception cref="ImpostorException">When the timeout expires.</exception> | ||
/// <returns>The lock guard if the semaphore could be locked succesfully.</returns> | ||
public static async Task<LockGuard> LockAsync(SemaphoreSlim semaphore, int timeout = 1000) | ||
{ | ||
var success = await semaphore.WaitAsync(timeout); | ||
if (success) | ||
{ | ||
return new LockGuard(semaphore); | ||
} | ||
else | ||
{ | ||
throw new ImpostorException("Could not lock semaphore"); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters