mirror of
https://github.com/ppy/osu.git
synced 2024-12-16 05:42:54 +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;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using osu.Framework.Allocation;
|
||||||
|
|
||||||
namespace osu.Game.Online.RealtimeMultiplayer
|
namespace osu.Game.Online.RealtimeMultiplayer
|
||||||
{
|
{
|
||||||
@ -48,10 +50,26 @@ namespace osu.Game.Online.RealtimeMultiplayer
|
|||||||
RoomID = roomId;
|
RoomID = roomId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private object updateLock = new object();
|
||||||
|
|
||||||
|
private ManualResetEventSlim freeForWrite = new ManualResetEventSlim(true);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Request a lock on this room to perform a thread-safe update.
|
/// Request a lock on this room to perform a thread-safe update.
|
||||||
/// </summary>
|
/// </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}]";
|
public override string ToString() => $"RoomID:{RoomID} Host:{Host?.UserID} Users:{Users.Count} State:{State} Settings: [{Settings}]";
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user