[ampr] allow concurrent reads

This commit is contained in:
ParantezTech
2026-07-18 23:01:51 +03:00
parent 59059dfd2c
commit 7548911413
+10 -17
View File
@@ -6,6 +6,7 @@ using SharpEmu.Libs.Kernel;
using System.Buffers; using System.Buffers;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using Microsoft.Win32.SafeHandles;
namespace SharpEmu.Libs.Ampr; namespace SharpEmu.Libs.Ampr;
@@ -43,17 +44,17 @@ public static class AmprExports
{ {
public CachedHostFile(string path) public CachedHostFile(string path)
{ {
Stream = new FileStream( Handle = File.OpenHandle(
path, path,
FileMode.Open, FileMode.Open,
FileAccess.Read, FileAccess.Read,
FileShare.ReadWrite | FileShare.Delete, FileShare.ReadWrite | FileShare.Delete,
bufferSize: 1024 * 1024,
FileOptions.RandomAccess); FileOptions.RandomAccess);
Length = RandomAccess.GetLength(Handle);
} }
public object Gate { get; } = new(); public SafeFileHandle Handle { get; }
public FileStream Stream { get; } public long Length { get; }
} }
[SysAbiExport( [SysAbiExport(
@@ -735,13 +736,7 @@ public static class AmprExports
return openResult; return openResult;
} }
long fileLength; if (fileOffset >= (ulong)cachedFile.Length)
lock (cachedFile.Gate)
{
fileLength = cachedFile.Stream.Length;
}
if (fileOffset >= (ulong)fileLength)
{ {
return (int)OrbisGen2Result.ORBIS_GEN2_OK; return (int)OrbisGen2Result.ORBIS_GEN2_OK;
} }
@@ -760,12 +755,10 @@ public static class AmprExports
} }
var request = (int)Math.Min((ulong)buffer.Length, size - bytesRead); var request = (int)Math.Min((ulong)buffer.Length, size - bytesRead);
int read; var read = RandomAccess.Read(
lock (cachedFile.Gate) cachedFile.Handle,
{ buffer.AsSpan(0, request),
cachedFile.Stream.Position = unchecked((long)absoluteOffset); unchecked((long)absoluteOffset));
read = cachedFile.Stream.Read(buffer, 0, request);
}
if (read <= 0) if (read <= 0)
{ {