Skip to content

Commit

Permalink
Merge pull request #1 from maxmind/greg/memory-mode-thread-fix
Browse files Browse the repository at this point in the history
Fixed for memory mode
  • Loading branch information
borisz committed Dec 23, 2013
2 parents 3591ca9 + 0571af5 commit b02b96d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
12 changes: 8 additions & 4 deletions MaxMind.Db.Test/ThreadingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ namespace MaxMind.Db.Test
public class ThreadingTest
{
[Test]
public void TestParallelFor()
[TestCase(FileAccessMode.MemoryMapped)]
[TestCase(FileAccessMode.Memory)]
public void TestParallelFor(FileAccessMode mode)
{
var reader = new Reader(Path.Combine("..", "..", "TestData", "GeoLite2-City.mmdb"), FileAccessMode.MemoryMapped);
var reader = new Reader(Path.Combine("..", "..", "TestData", "GeoLite2-City.mmdb"), mode);
var count = 0;
var ipsAndResults = new Dictionary<IPAddress, string>();
var rand = new Random();
Expand Down Expand Up @@ -45,12 +47,14 @@ public void TestParallelFor()
}

[Test]
[TestCase(FileAccessMode.MemoryMapped)]
[TestCase(FileAccessMode.Memory)]
[Category("BreaksMono")]
public void TestManyOpens()
public void TestManyOpens(FileAccessMode mode)
{
Parallel.For(0, 1000, i =>
{
var reader = new Reader(Path.Combine("..", "..", "TestData", "GeoLite2-City.mmdb"), FileAccessMode.MemoryMapped);
var reader = new Reader(Path.Combine("..", "..", "TestData", "GeoLite2-City.mmdb"), mode);
});
}
}
Expand Down
27 changes: 16 additions & 11 deletions MaxMind.Db/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private int IPV4Start
}
}

private static readonly Object _mmapLocker = new Object();
private static readonly Object _fileLocker = new Object();

private readonly ThreadLocal<Stream> _stream;

Expand All @@ -90,7 +90,7 @@ public Reader(string file, FileAccessMode mode)
{
var fileInfo = new FileInfo(file);
var mmfName = fileInfo.FullName.Replace("\\", "-");
lock (_mmapLocker)
lock (_fileLocker)
{
try
{
Expand All @@ -109,18 +109,23 @@ public Reader(string file, FileAccessMode mode)
}
}

_stream = new ThreadLocal<Stream>(() =>
if (mode == FileAccessMode.Memory)
{
Stream s;
if (mode == FileAccessMode.Memory) s = new MemoryStream(File.ReadAllBytes(_fileName));
else
byte[] fileBytes = File.ReadAllBytes(_fileName);
_stream = new ThreadLocal<Stream>(() =>
{
return new MemoryStream(fileBytes, false);
});
}
else
{
_stream = new ThreadLocal<Stream>(() =>
{
var fileLength = (int)new FileInfo(file).Length;
s = _memoryMappedFile.CreateViewStream(0, fileLength, MemoryMappedFileAccess.Read);
}

return s;
});
var fileLength = (int)new FileInfo(file).Length;
return _memoryMappedFile.CreateViewStream(0, fileLength, MemoryMappedFileAccess.Read);
});
}

var start = FindMetadataStart();
var metaDecode = new Decoder(_stream, start);
Expand Down

0 comments on commit b02b96d

Please sign in to comment.