mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Add thead safety to terrain data (#1999)
Allow safely reading terrain data from other threads
This commit is contained in:
parent
d6220ff779
commit
aeca6a8f53
3 changed files with 101 additions and 25 deletions
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace MinecraftClient.Mapping
|
||||
{
|
||||
|
|
@ -19,6 +20,11 @@ namespace MinecraftClient.Mapping
|
|||
/// </summary>
|
||||
private readonly Block[,,] blocks = new Block[SizeX, SizeY, SizeZ];
|
||||
|
||||
/// <summary>
|
||||
/// Lock for thread safety
|
||||
/// </summary>
|
||||
private readonly ReaderWriterLockSlim blockLock = new ReaderWriterLockSlim();
|
||||
|
||||
/// <summary>
|
||||
/// Read, or set the specified block
|
||||
/// </summary>
|
||||
|
|
@ -36,7 +42,16 @@ namespace MinecraftClient.Mapping
|
|||
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
|
||||
if (blockZ < 0 || blockZ >= SizeZ)
|
||||
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
|
||||
return blocks[blockX, blockY, blockZ];
|
||||
|
||||
blockLock.EnterReadLock();
|
||||
try
|
||||
{
|
||||
return blocks[blockX, blockY, blockZ];
|
||||
}
|
||||
finally
|
||||
{
|
||||
blockLock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
|
|
@ -46,7 +61,16 @@ namespace MinecraftClient.Mapping
|
|||
throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
|
||||
if (blockZ < 0 || blockZ >= SizeZ)
|
||||
throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
|
||||
blocks[blockX, blockY, blockZ] = value;
|
||||
|
||||
blockLock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
blocks[blockX, blockY, blockZ] = value;
|
||||
}
|
||||
finally
|
||||
{
|
||||
blockLock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue