Skip to content

Commit

Permalink
Allow memory-mapped file to open for shared reading
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Apr 25, 2022
1 parent 054aabf commit 02c4fec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Microsoft.Windows.CsWin32/MetadataIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,12 @@ internal static MetadataIndex Get(string metadataPath, Platform? platform)
// Read the entire metadata file exactly once so that many MemoryStreams can share the memory.
if (!MetadataFiles.TryGetValue(metadataPath, out MemoryMappedFile? file))
{
file = MemoryMappedFile.CreateFromFile(metadataPath);
FileStream metadataStream = new FileStream(metadataPath, FileMode.Open, FileAccess.Read, FileShare.Read);
file = MemoryMappedFile.CreateFromFile(metadataStream, mapName: null, capacity: 0, MemoryMappedFileAccess.Read, HandleInheritability.None, leaveOpen: false);
MetadataFiles.Add(metadataPath, file);
}

metadataBytes = file.CreateViewStream();
metadataBytes = file.CreateViewStream(offset: 0, size: 0, MemoryMappedFileAccess.Read);
}

return new MetadataIndex(metadataPath, metadataBytes, platform);
Expand Down
8 changes: 8 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,14 @@ public void CocreatableStructs()
Assert.Contains(classDecl.AttributeLists, al => al.Attributes.Any(a => a.Name.ToString().Contains("ComImport")));
}

[Fact]
public void OpensMetadataForSharedReading()
{
using FileStream competingReader = File.OpenRead(MetadataPath);
this.generator = this.CreateGenerator();
Assert.True(this.generator.TryGenerate("CreateFile", CancellationToken.None));
}

private static string ConstructGlobalConfigString(bool omitDocs = false)
{
StringBuilder globalConfigBuilder = new();
Expand Down

0 comments on commit 02c4fec

Please sign in to comment.