From 855b6463ca3429213cd8472eb6240bac78cc4b89 Mon Sep 17 00:00:00 2001 From: Mark Sturgill Date: Sun, 13 Oct 2013 22:12:18 -0700 Subject: [PATCH] tilematrix thread-safety main waithandle timeout removed basecreature typo --- Scripts/Mobiles/BaseCreature.cs | 2 +- Server/Main.cs | 3 +- Server/Map.cs | 7 +- Server/TileMatrix.cs | 113 +++++++++++++++++++------------- Server/TileMatrixPatch.cs | 11 +++- 5 files changed, 83 insertions(+), 53 deletions(-) diff --git a/Scripts/Mobiles/BaseCreature.cs b/Scripts/Mobiles/BaseCreature.cs index ae47db511..8b547a40e 100644 --- a/Scripts/Mobiles/BaseCreature.cs +++ b/Scripts/Mobiles/BaseCreature.cs @@ -5648,7 +5648,7 @@ namespace Server.Mobiles return base.CanBeDamaged(); } - public virtual bool PlayerRangeSensitive{ get{ return (this.CurrentWayPoint != null); } } //If they are following a waypoint, they'll continue to follow it even if players aren't around + public virtual bool PlayerRangeSensitive{ get{ return (this.CurrentWayPoint == null); } } //If they are following a waypoint, they'll continue to follow it even if players aren't around /* until we are sure about who should be getting deleted, move them instead */ /* On OSI, they despawn */ diff --git a/Server/Main.cs b/Server/Main.cs index f110a64a2..44994b0a1 100644 --- a/Server/Main.cs +++ b/Server/Main.cs @@ -575,13 +575,12 @@ namespace Server const int sampleInterval = 100; const float ticksPerSecond = (float)(1000 * sampleInterval); - TimeSpan _oneMS = TimeSpan.FromMilliseconds( 1 ); long sample = 0; while( !m_Closing ) { - m_Signal.WaitOne( _oneMS ); + m_Signal.WaitOne(); Mobile.ProcessDeltaQueue(); Item.ProcessDeltaQueue(); diff --git a/Server/Map.cs b/Server/Map.cs index f02e8543f..27b99fe7f 100644 --- a/Server/Map.cs +++ b/Server/Map.cs @@ -933,12 +933,15 @@ namespace Server } } + private object tileLock = new object(); + public TileMatrix Tiles { get { - if ( m_Tiles == null ) - m_Tiles = new TileMatrix( this, m_FileIndex, m_MapID, m_Width, m_Height ); + lock (tileLock) + if ( m_Tiles == null ) + m_Tiles = new TileMatrix( this, m_FileIndex, m_MapID, m_Width, m_Height ); return m_Tiles; } diff --git a/Server/TileMatrix.cs b/Server/TileMatrix.cs index 1fe95840a..f0ffc1f4d 100644 --- a/Server/TileMatrix.cs +++ b/Server/TileMatrix.cs @@ -22,6 +22,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; namespace Server { @@ -51,13 +52,13 @@ namespace Server private int[][] m_StaticPatches; private int[][] m_LandPatches; - public Map Owner + /*public Map Owner { get { return m_Owner; } - } + }*/ public TileMatrixPatch Patch { @@ -83,7 +84,7 @@ namespace Server } } - public int Width + /*public int Width { get { @@ -97,7 +98,7 @@ namespace Server { return m_Height; } - } + }*/ public FileStream MapStream { @@ -105,10 +106,10 @@ namespace Server set{ m_Map = value; } } - public bool MapUOPPacked + /*public bool MapUOPPacked { get{ return ( m_MapIndex != null ); } - } + }*/ public FileStream IndexStream { @@ -138,18 +139,25 @@ namespace Server public TileMatrix( Map owner, int fileIndex, int mapID, int width, int height ) { - for ( int i = 0; i < m_Instances.Count; ++i ) - { - TileMatrix tm = m_Instances[i]; - - if ( tm.m_FileIndex == fileIndex ) + lock (m_Instances) { + for ( int i = 0; i < m_Instances.Count; ++i ) { - tm.m_FileShare.Add( this ); - m_FileShare.Add( tm ); + TileMatrix tm = m_Instances[i]; + + if ( tm.m_FileIndex == fileIndex ) + { + lock (m_FileShare) { + lock (tm.m_FileShare) { + tm.m_FileShare.Add( this ); + m_FileShare.Add( tm ); + } + } + } } + + m_Instances.Add( this ); } - m_Instances.Add( this ); m_FileIndex = fileIndex; m_Width = width; m_Height = height; @@ -219,6 +227,7 @@ namespace Server } } + [MethodImpl(MethodImplOptions.Synchronized)] public void SetStaticBlock( int x, int y, StaticTile[][][] value ) { if ( x < 0 || y < 0 || x >= m_BlockWidth || y >= m_BlockHeight ) @@ -235,6 +244,7 @@ namespace Server m_StaticPatches[x][y >> 5] |= 1 << (y & 0x1F); } + [MethodImpl(MethodImplOptions.Synchronized)] public StaticTile[][][] GetStaticBlock( int x, int y ) { if ( x < 0 || y < 0 || x >= m_BlockWidth || y >= m_BlockHeight || m_Statics == null || m_Index == null ) @@ -247,23 +257,27 @@ namespace Server if ( tiles == null ) { - for ( int i = 0; tiles == null && i < m_FileShare.Count; ++i ) - { - TileMatrix shared = m_FileShare[i]; - - if ( x >= 0 && x < shared.m_BlockWidth && y >= 0 && y < shared.m_BlockHeight ) + lock (m_FileShare) { + for ( int i = 0; tiles == null && i < m_FileShare.Count; ++i ) { - StaticTile[][][][] theirTiles = shared.m_StaticTiles[x]; + TileMatrix shared = m_FileShare[i]; - if ( theirTiles != null ) - tiles = theirTiles[y]; + lock (shared) { + if ( x >= 0 && x < shared.m_BlockWidth && y >= 0 && y < shared.m_BlockHeight ) + { + StaticTile[][][][] theirTiles = shared.m_StaticTiles[x]; - if ( tiles != null ) - { - int[] theirBits = shared.m_StaticPatches[x]; + if ( theirTiles != null ) + tiles = theirTiles[y]; - if ( theirBits != null && (theirBits[y >> 5] & (1 << (y & 0x1F))) != 0 ) - tiles = null; + if ( tiles != null ) + { + int[] theirBits = shared.m_StaticPatches[x]; + + if ( theirBits != null && (theirBits[y >> 5] & (1 << (y & 0x1F))) != 0 ) + tiles = null; + } + } } } } @@ -284,8 +298,9 @@ namespace Server return tiles[x & 0x7][y & 0x7]; } - private static TileList m_TilesList = new TileList(); + private TileList m_TilesList = new TileList(); + [MethodImpl(MethodImplOptions.Synchronized)] public StaticTile[] GetStaticTiles( int x, int y, bool multis ) { StaticTile[][][] tiles = GetStaticBlock( x >> 3, y >> 3 ); @@ -322,6 +337,7 @@ namespace Server } } + [MethodImpl(MethodImplOptions.Synchronized)] public void SetLandBlock( int x, int y, LandTile[] value ) { if ( x < 0 || y < 0 || x >= m_BlockWidth || y >= m_BlockHeight ) @@ -338,6 +354,7 @@ namespace Server m_LandPatches[x][y >> 5] |= 1 << (y & 0x1F); } + [MethodImpl(MethodImplOptions.Synchronized)] public LandTile[] GetLandBlock( int x, int y ) { if ( x < 0 || y < 0 || x >= m_BlockWidth || y >= m_BlockHeight || m_Map == null ) @@ -350,23 +367,27 @@ namespace Server if ( tiles == null ) { - for ( int i = 0; tiles == null && i < m_FileShare.Count; ++i ) - { - TileMatrix shared = m_FileShare[i]; - - if ( x >= 0 && x < shared.m_BlockWidth && y >= 0 && y < shared.m_BlockHeight ) + lock (m_FileShare) { + for ( int i = 0; tiles == null && i < m_FileShare.Count; ++i ) { - LandTile[][] theirTiles = shared.m_LandTiles[x]; + TileMatrix shared = m_FileShare[i]; - if ( theirTiles != null ) - tiles = theirTiles[y]; + lock (shared) { + if ( x >= 0 && x < shared.m_BlockWidth && y >= 0 && y < shared.m_BlockHeight ) + { + LandTile[][] theirTiles = shared.m_LandTiles[x]; - if ( tiles != null ) - { - int[] theirBits = shared.m_LandPatches[x]; + if ( theirTiles != null ) + tiles = theirTiles[y]; - if ( theirBits != null && (theirBits[y >> 5] & (1 << (y & 0x1F))) != 0 ) - tiles = null; + if ( tiles != null ) + { + int[] theirBits = shared.m_LandPatches[x]; + + if ( theirBits != null && (theirBits[y >> 5] & (1 << (y & 0x1F))) != 0 ) + tiles = null; + } + } } } } @@ -387,10 +408,11 @@ namespace Server return tiles[((y & 0x7) << 3) + (x & 0x7)]; } - private static TileList[][] m_Lists; + private TileList[][] m_Lists; - private static StaticTile[] m_TileBuffer = new StaticTile[128]; + private StaticTile[] m_TileBuffer = new StaticTile[128]; + [MethodImpl(MethodImplOptions.Synchronized)] private unsafe StaticTile[][][] ReadStaticBlock( int x, int y ) { try @@ -480,6 +502,7 @@ namespace Server throw new Exception(); } + [MethodImpl(MethodImplOptions.Synchronized)] private unsafe LandTile[] ReadLandBlock( int x, int y ) { try @@ -531,7 +554,7 @@ namespace Server } } - [System.Runtime.InteropServices.StructLayout( System.Runtime.InteropServices.LayoutKind.Sequential, Pack=1 )] + [StructLayout(LayoutKind.Sequential, Pack=1)] public struct LandTile { internal short m_ID; @@ -572,7 +595,7 @@ namespace Server } } - [System.Runtime.InteropServices.StructLayout( System.Runtime.InteropServices.LayoutKind.Sequential, Pack=1 )] + [StructLayout(LayoutKind.Sequential, Pack=1)] public struct StaticTile { internal ushort m_ID; diff --git a/Server/TileMatrixPatch.cs b/Server/TileMatrixPatch.cs index 7f4e3e050..df759d617 100644 --- a/Server/TileMatrixPatch.cs +++ b/Server/TileMatrixPatch.cs @@ -21,6 +21,7 @@ using System; using System.IO; using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; namespace Server { @@ -46,7 +47,8 @@ namespace Server { get { - return m_LandBlocks; + lock (this) + return m_LandBlocks; } } @@ -54,7 +56,8 @@ namespace Server { get { - return m_StaticBlocks; + lock (this) + return m_StaticBlocks; } } @@ -77,6 +80,7 @@ namespace Server m_StaticBlocks = PatchStatics( matrix, staDataPath, staIndexPath, staLookupPath ); } + [MethodImpl(MethodImplOptions.Synchronized)] private unsafe int PatchLand( TileMatrix matrix, string dataPath, string indexPath ) { using ( FileStream fsData = new FileStream( dataPath, FileMode.Open, FileAccess.Read, FileShare.Read ) ) @@ -116,8 +120,9 @@ namespace Server } } - private static StaticTile[] m_TileBuffer = new StaticTile[128]; + private StaticTile[] m_TileBuffer = new StaticTile[128]; + [MethodImpl(MethodImplOptions.Synchronized)] private unsafe int PatchStatics( TileMatrix matrix, string dataPath, string indexPath, string lookupPath ) { using ( FileStream fsData = new FileStream( dataPath, FileMode.Open, FileAccess.Read, FileShare.Read ) )