From aa68ae4ff27713f25ad42545baa905d83e29de4f Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 8 Dec 2020 20:10:23 +0900 Subject: [PATCH] Change locking mechanism for multiplayer rooms to use using-disposal pattern Was required to lock over `await` calls server-side. --- .../RealtimeMultiplayer/LockUntilDisposal.cs | 24 +++++++++++++++++++ .../RealtimeMultiplayer/MultiplayerRoom.cs | 9 ++----- 2 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 osu.Game/Online/RealtimeMultiplayer/LockUntilDisposal.cs diff --git a/osu.Game/Online/RealtimeMultiplayer/LockUntilDisposal.cs b/osu.Game/Online/RealtimeMultiplayer/LockUntilDisposal.cs new file mode 100644 index 0000000000..cd16fce2ee --- /dev/null +++ b/osu.Game/Online/RealtimeMultiplayer/LockUntilDisposal.cs @@ -0,0 +1,24 @@ +// Copyright (c) ppy Pty Ltd . 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); + } + } +} diff --git a/osu.Game/Online/RealtimeMultiplayer/MultiplayerRoom.cs b/osu.Game/Online/RealtimeMultiplayer/MultiplayerRoom.cs index 3fbc01a2f5..0f18ab6c89 100644 --- a/osu.Game/Online/RealtimeMultiplayer/MultiplayerRoom.cs +++ b/osu.Game/Online/RealtimeMultiplayer/MultiplayerRoom.cs @@ -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 } /// - /// Perform an update on this room in a thread-safe manner. + /// Request a lock on this room to perform a thread-safe update. /// - /// The action to perform. - public void PerformUpdate([InstantHandle] Action action) - { - lock (writeLock) action(this); - } + public LockUntilDisposal LockForUpdate() => new LockUntilDisposal(writeLock); } }