Compare commits
2
Commits
b08fbe13ee
...
2ce631940d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ce631940d | ||
|
|
a42b3393d4 |
@@ -1,11 +1,14 @@
|
||||
using SharedMemory;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
using var numbers = new SharedArray<int>("MySharedArray");
|
||||
Console.WriteLine(numbers.Length);
|
||||
Console.WriteLine(numbers[0]);
|
||||
Console.WriteLine(numbers[^1]);
|
||||
using var consumer = new WriterApp.SharedBuffer<int>("MySharedBuffer");
|
||||
consumer.BufferAction((span) =>
|
||||
{
|
||||
for (var i = 0; i < span.Length; i++)
|
||||
{
|
||||
Console.WriteLine($"Read {span[i]} from the shared buffer");
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
|
||||
@@ -11,4 +11,8 @@
|
||||
<PackageReference Include="SharedMemory" Version="2.3.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\WriterApp\WriterApp.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
+17
-4
@@ -1,8 +1,21 @@
|
||||
using SharedMemory;
|
||||
using DotNext;
|
||||
using System.Diagnostics;
|
||||
|
||||
Console.WriteLine("WriterApp.SharedBuffer:");
|
||||
using var producer = new WriterApp.SharedBuffer<int>("MySharedBuffer", length: 10);
|
||||
|
||||
var intArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
producer.Write(intArray);
|
||||
using var consumer = new WriterApp.SharedBuffer<int>("MySharedBuffer");
|
||||
consumer.BufferAction((span) =>
|
||||
{
|
||||
for (var i = 0; i < span.Length; i++)
|
||||
{
|
||||
Debug.Assert(span[i] == intArray[i]);
|
||||
}
|
||||
});
|
||||
|
||||
using var numbers = new SharedArray<int>("MySharedArray", length: 10);
|
||||
numbers[0] = 123;
|
||||
numbers[^1] = 456;
|
||||
|
||||
Console.WriteLine("Start the second program, then press Enter here to close the array");
|
||||
Console.ReadLine();
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using DotNext.IO.MemoryMappedFiles;
|
||||
using SharedMemory;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
|
||||
namespace WriterApp;
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="SharedBuffer"/> that creates no events, so that the lifetime of the shared memory
|
||||
/// can be tested apart from the lifetime of named events. Compiled into the tests and the peer.
|
||||
/// </summary>
|
||||
public class SharedBuffer<T> : BufferReadWrite where T : struct
|
||||
{
|
||||
public SharedBuffer(string name, int length) : base(name, length * Unsafe.SizeOf<T>())
|
||||
{
|
||||
}
|
||||
public SharedBuffer(string name) : base(name)
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// Prevents write operations from deadlocking by throwing a TimeoutException if the WriteWaitEvent is not available within <see cref="ReadWriteTimeout"/> milliseconds
|
||||
/// </summary>
|
||||
private void WriteWait()
|
||||
{
|
||||
if (!WriteWaitEvent.WaitOne(ReadWriteTimeout))
|
||||
throw new TimeoutException("The write operation timed out waiting for the write lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock.");
|
||||
}
|
||||
/// <summary>
|
||||
/// Prevents read operations from deadlocking by throwing a TimeoutException if the ReadWaitEvent is not available within <see cref="ReadWriteTimeout"/> milliseconds
|
||||
/// </summary>
|
||||
private void ReadWait()
|
||||
{
|
||||
if (!ReadWaitEvent.WaitOne(ReadWriteTimeout))
|
||||
throw new TimeoutException("The read operation timed out waiting for the read lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock.");
|
||||
}
|
||||
private Span<byte> ByteSpan
|
||||
{
|
||||
get
|
||||
{
|
||||
return Mmf.CreateDirectAccessor(BufferOffset, BufferSize).Bytes;
|
||||
}
|
||||
}
|
||||
private Span<T> DataSpan
|
||||
{
|
||||
get
|
||||
{
|
||||
var spanT = MemoryMarshal.Cast<byte, T>(ByteSpan);
|
||||
return spanT;
|
||||
}
|
||||
}
|
||||
public void Write(T[] array)
|
||||
{
|
||||
WriteWait();
|
||||
var span = DataSpan;
|
||||
if (span.Length < array.Length)
|
||||
throw new ArgumentException($"The array length {array.Length} is greater than the buffer length {span.Length}");
|
||||
array.AsSpan().CopyTo(span);
|
||||
}
|
||||
public void Read(T[] array)
|
||||
{
|
||||
ReadWait();
|
||||
var span = DataSpan;
|
||||
if (span.Length > array.Length)
|
||||
throw new ArgumentException($"The array length {array.Length} is less than the buffer length {span.Length}");
|
||||
span.CopyTo(array.AsSpan());
|
||||
}
|
||||
public void Write(ReadOnlySpan<T> span)
|
||||
{
|
||||
WriteWait();
|
||||
var dataSpan = DataSpan;
|
||||
if (dataSpan.Length < span.Length)
|
||||
throw new ArgumentException($"The span length {span.Length} is greater than the buffer length {dataSpan.Length}");
|
||||
span.CopyTo(dataSpan);
|
||||
}
|
||||
|
||||
public void Read(Span<T> span)
|
||||
{
|
||||
ReadWait();
|
||||
var dataSpan = DataSpan;
|
||||
if (dataSpan.Length > span.Length)
|
||||
throw new ArgumentException($"The span length {span.Length} is less than the buffer length {dataSpan.Length}");
|
||||
dataSpan.CopyTo(span);
|
||||
}
|
||||
public void BufferAction(Action<Span<T>> action)
|
||||
{
|
||||
ReadWait();
|
||||
var dataSpan = DataSpan;
|
||||
action(dataSpan);
|
||||
}
|
||||
public T First => DataSpan[0];
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DotNext.Unsafe" Version="6.12.1" />
|
||||
<PackageReference Include="SharedMemory" Version="2.3.4" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user