Made sure that all access to sectorFree is moved to inside the protected area as well.

This commit is contained in:
Fredrik Blom 2012-11-15 16:02:36 +01:00
parent 6326bdac82
commit 258eff946b

View file

@ -26,6 +26,7 @@ namespace Substrate.Core
/// The file lock used so that we do not seek in different areas /// The file lock used so that we do not seek in different areas
/// of the same file at the same time. All file access should lock this /// of the same file at the same time. All file access should lock this
/// object before moving the file pointer. /// object before moving the file pointer.
/// The lock should also surround all access to the sectorFree free variable.
/// </summary> /// </summary>
private object fileLock = new object(); private object fileLock = new object();
@ -245,12 +246,13 @@ namespace Substrate.Core
int sectorNumber = offset >> 8; int sectorNumber = offset >> 8;
int numSectors = offset & 0xFF; int numSectors = offset & 0xFF;
lock (this.fileLock)
{
if (sectorNumber + numSectors > sectorFree.Count) { if (sectorNumber + numSectors > sectorFree.Count) {
Debugln("READ", x, z, "invalid sector"); Debugln("READ", x, z, "invalid sector");
return null; return null;
} }
lock (this.fileLock) {
file.Seek(sectorNumber * SectorBytes, SeekOrigin.Begin); file.Seek(sectorNumber * SectorBytes, SeekOrigin.Begin);
byte[] lengthBytes = new byte[4]; byte[] lengthBytes = new byte[4];
file.Read(lengthBytes, 0, 4); file.Read(lengthBytes, 0, 4);
@ -381,6 +383,7 @@ namespace Substrate.Core
else { else {
/* we need to allocate new sectors */ /* we need to allocate new sectors */
lock (this.fileLock) {
/* mark the sectors previously used for this chunk as free */ /* mark the sectors previously used for this chunk as free */
for (int i = 0; i < sectorsAllocated; ++i) { for (int i = 0; i < sectorsAllocated; ++i) {
sectorFree[sectorNumber + i] = true; sectorFree[sectorNumber + i] = true;
@ -420,7 +423,6 @@ namespace Substrate.Core
* no free space large enough found -- we need to grow the * no free space large enough found -- we need to grow the
* file * file
*/ */
lock (this.fileLock) {
Debug("SAVE", x, z, length, "grow"); Debug("SAVE", x, z, length, "grow");
file.Seek(0, SeekOrigin.End); file.Seek(0, SeekOrigin.End);
sectorNumber = sectorFree.Count; sectorNumber = sectorFree.Count;