Add thead safety to terrain data (#1999)

Allow safely reading terrain data from other threads
This commit is contained in:
ORelio 2022-04-23 12:00:50 +02:00
parent d6220ff779
commit aeca6a8f53
3 changed files with 101 additions and 25 deletions

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MinecraftClient.Mapping
{
@ -17,6 +18,11 @@ namespace MinecraftClient.Mapping
/// </summary>
private readonly Chunk[] chunks = new Chunk[ColumnSize];
/// <summary>
/// Lock for thread safety
/// </summary>
private readonly ReaderWriterLockSlim chunkLock = new ReaderWriterLockSlim();
/// <summary>
/// Get or set the specified chunk column
/// </summary>
@ -27,11 +33,27 @@ namespace MinecraftClient.Mapping
{
get
{
return chunks[chunkY];
chunkLock.EnterReadLock();
try
{
return chunks[chunkY];
}
finally
{
chunkLock.ExitReadLock();
}
}
set
{
chunks[chunkY] = value;
chunkLock.EnterWriteLock();
try
{
chunks[chunkY] = value;
}
finally
{
chunkLock.ExitWriteLock();
}
}
}