Improve mutex stuff?

idk i cant really find anything else wrong with it
This commit is contained in:
pizzaboxer 2023-08-23 23:22:42 +01:00
parent 7a963c53f7
commit 5741d82f3d
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
2 changed files with 10 additions and 7 deletions

View File

@ -119,6 +119,8 @@ namespace Bloxstrap
App.Logger.WriteLine(LOG_IDENT, "Performing connectivity check..."); App.Logger.WriteLine(LOG_IDENT, "Performing connectivity check...");
SetStatus("Connecting to Roblox...");
try try
{ {
await RobloxDeployment.GetInfo(RobloxDeployment.DefaultChannel); await RobloxDeployment.GetInfo(RobloxDeployment.DefaultChannel);
@ -158,8 +160,9 @@ namespace Bloxstrap
try try
{ {
Mutex.OpenExisting("Bloxstrap_BootstrapperMutex").Close(); Mutex.OpenExisting("Bloxstrap_SingletonMutex").Close();
App.Logger.WriteLine(LOG_IDENT, "Bloxstrap_BootstrapperMutex mutex exists, waiting..."); App.Logger.WriteLine(LOG_IDENT, "Bloxstrap_SingletonMutex mutex exists, waiting...");
SetStatus("Waiting for other instances...");
mutexExists = true; mutexExists = true;
} }
catch (Exception) catch (Exception)
@ -168,7 +171,7 @@ namespace Bloxstrap
} }
// wait for mutex to be released if it's not yet // 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); await mutex.AcquireAsync(_cancelTokenSource.Token);
// reload our configs since they've likely changed by now // reload our configs since they've likely changed by now
@ -219,8 +222,6 @@ namespace Bloxstrap
{ {
const string LOG_IDENT = "Bootstrapper::CheckLatestVersion"; const string LOG_IDENT = "Bootstrapper::CheckLatestVersion";
SetStatus("Connecting to Roblox...");
ClientVersion clientVersion; ClientVersion clientVersion;
try try

View File

@ -4,13 +4,15 @@
public sealed class AsyncMutex : IAsyncDisposable public sealed class AsyncMutex : IAsyncDisposable
{ {
private readonly bool _initiallyOwned;
private readonly string _name; private readonly string _name;
private Task? _mutexTask; private Task? _mutexTask;
private ManualResetEventSlim? _releaseEvent; private ManualResetEventSlim? _releaseEvent;
private CancellationTokenSource? _cancellationTokenSource; private CancellationTokenSource? _cancellationTokenSource;
public AsyncMutex(string name) public AsyncMutex(bool initiallyOwned, string name)
{ {
_initiallyOwned = initiallyOwned;
_name = name; _name = name;
} }
@ -31,7 +33,7 @@
try try
{ {
CancellationToken cancellationToken = _cancellationTokenSource.Token; CancellationToken cancellationToken = _cancellationTokenSource.Token;
using var mutex = new Mutex(false, _name); using var mutex = new Mutex(_initiallyOwned, _name);
try try
{ {
// Wait for either the mutex to be acquired, or cancellation // Wait for either the mutex to be acquired, or cancellation