1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 21:03:08 +08:00

Change locking mechanism for multiplayer rooms to use using-disposal pattern

Was required to lock over `await` calls server-side.
This commit is contained in:
Dean Herbert 2020-12-08 20:10:23 +09:00
parent d76fabedf9
commit aa68ae4ff2
2 changed files with 26 additions and 7 deletions

View File

@ -0,0 +1,24 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Threading;
namespace osu.Game.Online.RealtimeMultiplayer
{
public readonly struct LockUntilDisposal : IDisposable
{
private readonly object lockTarget;
public LockUntilDisposal(object lockTarget)
{
this.lockTarget = lockTarget;
Monitor.Enter(lockTarget);
}
public void Dispose()
{
Monitor.Exit(lockTarget);
}
}
}

View File

@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
namespace osu.Game.Online.RealtimeMultiplayer
{
@ -48,12 +47,8 @@ namespace osu.Game.Online.RealtimeMultiplayer
}
/// <summary>
/// Perform an update on this room in a thread-safe manner.
/// Request a lock on this room to perform a thread-safe update.
/// </summary>
/// <param name="action">The action to perform.</param>
public void PerformUpdate([InstantHandle] Action<MultiplayerRoom> action)
{
lock (writeLock) action(this);
}
public LockUntilDisposal LockForUpdate() => new LockUntilDisposal(writeLock);
}
}