Skip to content

Commit

Permalink
Improve mutex stuff?
Browse files Browse the repository at this point in the history
idk i cant really find anything else wrong with it
  • Loading branch information
pizzaboxer committed Aug 23, 2023
1 parent 7a963c5 commit 5741d82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 6 additions & 5 deletions Bloxstrap/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public async Task Run()

App.Logger.WriteLine(LOG_IDENT, "Performing connectivity check...");

SetStatus("Connecting to Roblox...");

try
{
await RobloxDeployment.GetInfo(RobloxDeployment.DefaultChannel);
Expand Down Expand Up @@ -158,8 +160,9 @@ public async Task Run()

try
{
Mutex.OpenExisting("Bloxstrap_BootstrapperMutex").Close();
App.Logger.WriteLine(LOG_IDENT, "Bloxstrap_BootstrapperMutex mutex exists, waiting...");
Mutex.OpenExisting("Bloxstrap_SingletonMutex").Close();
App.Logger.WriteLine(LOG_IDENT, "Bloxstrap_SingletonMutex mutex exists, waiting...");
SetStatus("Waiting for other instances...");
mutexExists = true;
}
catch (Exception)
Expand All @@ -168,7 +171,7 @@ public async Task Run()
}

// wait for mutex to be released if it's not yet
await using AsyncMutex mutex = new("Bloxstrap_BootstrapperMutex");
await using var mutex = new AsyncMutex(true, "Bloxstrap_SingletonMutex");
await mutex.AcquireAsync(_cancelTokenSource.Token);

// reload our configs since they've likely changed by now
Expand Down Expand Up @@ -219,8 +222,6 @@ private async Task CheckLatestVersion()
{
const string LOG_IDENT = "Bootstrapper::CheckLatestVersion";

SetStatus("Connecting to Roblox...");

ClientVersion clientVersion;

try
Expand Down
6 changes: 4 additions & 2 deletions Bloxstrap/Utility/AsyncMutex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

public sealed class AsyncMutex : IAsyncDisposable
{
private readonly bool _initiallyOwned;
private readonly string _name;
private Task? _mutexTask;
private ManualResetEventSlim? _releaseEvent;
private CancellationTokenSource? _cancellationTokenSource;

public AsyncMutex(string name)
public AsyncMutex(bool initiallyOwned, string name)
{
_initiallyOwned = initiallyOwned;
_name = name;
}

Expand All @@ -31,7 +33,7 @@ public Task AcquireAsync(CancellationToken cancellationToken)
try
{
CancellationToken cancellationToken = _cancellationTokenSource.Token;
using var mutex = new Mutex(false, _name);
using var mutex = new Mutex(_initiallyOwned, _name);
try
{
// Wait for either the mutex to be acquired, or cancellation
Expand Down

0 comments on commit 5741d82

Please sign in to comment.