mirror of
https://github.com/ppy/osu.git
synced 2024-12-16 03:42:58 +08:00
Change locking method to better allow cross-thread locking
This commit is contained in:
parent
719b08b22f
commit
d3b2e2b36e
@ -1,24 +0,0 @@
|
||||
// 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,9 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Newtonsoft.Json;
|
||||
using osu.Framework.Allocation;
|
||||
|
||||
namespace osu.Game.Online.RealtimeMultiplayer
|
||||
{
|
||||
@ -48,10 +50,26 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
||||
RoomID = roomId;
|
||||
}
|
||||
|
||||
private object updateLock = new object();
|
||||
|
||||
private ManualResetEventSlim freeForWrite = new ManualResetEventSlim(true);
|
||||
|
||||
/// <summary>
|
||||
/// Request a lock on this room to perform a thread-safe update.
|
||||
/// </summary>
|
||||
public LockUntilDisposal LockForUpdate() => new LockUntilDisposal(writeLock);
|
||||
public IDisposable LockForUpdate()
|
||||
{
|
||||
// ReSharper disable once InconsistentlySynchronizedField
|
||||
freeForWrite.Wait();
|
||||
|
||||
lock (updateLock)
|
||||
{
|
||||
freeForWrite.Wait();
|
||||
freeForWrite.Reset();
|
||||
|
||||
return new ValueInvokeOnDisposal<MultiplayerRoom>(this, r => freeForWrite.Set());
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString() => $"RoomID:{RoomID} Host:{Host?.UserID} Users:{Users.Count} State:{State} Settings: [{Settings}]";
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user