mirror of
https://github.com/ppy/osu.git
synced 2025-01-07 19:22:58 +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:
parent
d76fabedf9
commit
aa68ae4ff2
24
osu.Game/Online/RealtimeMultiplayer/LockUntilDisposal.cs
Normal file
24
osu.Game/Online/RealtimeMultiplayer/LockUntilDisposal.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using JetBrains.Annotations;
|
|
||||||
|
|
||||||
namespace osu.Game.Online.RealtimeMultiplayer
|
namespace osu.Game.Online.RealtimeMultiplayer
|
||||||
{
|
{
|
||||||
@ -48,12 +47,8 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="action">The action to perform.</param>
|
public LockUntilDisposal LockForUpdate() => new LockUntilDisposal(writeLock);
|
||||||
public void PerformUpdate([InstantHandle] Action<MultiplayerRoom> action)
|
|
||||||
{
|
|
||||||
lock (writeLock) action(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user