-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
795 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
using System.Collections; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Threading.Tasks.Sources; | ||
|
||
namespace DotNext.IO; | ||
|
||
using Intrinsics = Runtime.Intrinsics; | ||
|
||
public partial class FileBufferingWriter : IDynamicInterfaceCastable | ||
{ | ||
private readonly Action writeCallback, writeAndFlushCallback, writeAndCopyCallback; | ||
private ReadOnlyMemory<byte> secondBuffer; | ||
private ManualResetValueTaskSourceCore<byte> source; | ||
private ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter awaiter; | ||
|
||
private ReadOnlyMemory<byte> GetBuffer(int index) => index switch | ||
{ | ||
0 => WrittenMemory, | ||
1 => secondBuffer, | ||
_ => ReadOnlyMemory<byte>.Empty, | ||
}; | ||
|
||
private IEnumerator<ReadOnlyMemory<byte>> EnumerateBuffers() | ||
{ | ||
yield return WrittenMemory; | ||
yield return secondBuffer; | ||
} | ||
|
||
[DynamicInterfaceCastableImplementation] | ||
private interface IBufferList : IReadOnlyList<ReadOnlyMemory<byte>> | ||
{ | ||
int IReadOnlyCollection<ReadOnlyMemory<byte>>.Count => 2; | ||
|
||
ReadOnlyMemory<byte> IReadOnlyList<ReadOnlyMemory<byte>>.this[int index] | ||
=> Unsafe.As<FileBufferingWriter>(this).GetBuffer(index); | ||
|
||
IEnumerator<ReadOnlyMemory<byte>> IEnumerable<ReadOnlyMemory<byte>>.GetEnumerator() | ||
=> Unsafe.As<FileBufferingWriter>(this).EnumerateBuffers(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
=> Unsafe.As<FileBufferingWriter>(this).EnumerateBuffers(); | ||
} | ||
|
||
private void GetAsyncResult(short token) | ||
{ | ||
try | ||
{ | ||
source.GetResult(token); | ||
} | ||
finally | ||
{ | ||
source.Reset(); | ||
} | ||
} | ||
|
||
private void OnWrite() | ||
{ | ||
var awaiter = this.awaiter; | ||
this.awaiter = default; | ||
|
||
var secondBuffer = this.secondBuffer; | ||
this.secondBuffer = default; | ||
|
||
try | ||
{ | ||
awaiter.GetResult(); | ||
|
||
filePosition += secondBuffer.Length + position; | ||
position = 0; | ||
} | ||
catch (Exception e) | ||
{ | ||
source.SetException(e); | ||
return; | ||
} | ||
|
||
source.SetResult(0); | ||
} | ||
|
||
private void OnWriteAndFlush() | ||
{ | ||
Debug.Assert(fileBackend is not null); | ||
|
||
var awaiter = this.awaiter; | ||
this.awaiter = default; | ||
|
||
var secondBuffer = this.secondBuffer; | ||
this.secondBuffer = default; | ||
|
||
try | ||
{ | ||
awaiter.GetResult(); | ||
|
||
filePosition += secondBuffer.Length + position; | ||
position = 0; | ||
RandomAccess.FlushToDisk(fileBackend); | ||
} | ||
catch (Exception e) | ||
{ | ||
source.SetException(e); | ||
return; | ||
} | ||
|
||
source.SetResult(0); | ||
} | ||
|
||
private void OnWriteAndCopy() | ||
{ | ||
var awaiter = this.awaiter; | ||
this.awaiter = default; | ||
|
||
var secondBuffer = this.secondBuffer; | ||
this.secondBuffer = default; | ||
|
||
try | ||
{ | ||
awaiter.GetResult(); | ||
|
||
filePosition += position; | ||
secondBuffer.CopyTo(buffer.Memory); | ||
position = secondBuffer.Length; | ||
} | ||
catch (Exception e) | ||
{ | ||
source.SetException(e); | ||
return; | ||
} | ||
|
||
source.SetResult(0); | ||
} | ||
|
||
private ValueTask Submit(ValueTask task, Action callback) | ||
{ | ||
awaiter = task.ConfigureAwait(false).GetAwaiter(); | ||
if (awaiter.IsCompleted) | ||
{ | ||
callback(); | ||
} | ||
else | ||
{ | ||
awaiter.UnsafeOnCompleted(callback); | ||
} | ||
|
||
return new((IValueTaskSource)this.As<IDynamicInterfaceCastable>(), source.Version); | ||
} | ||
|
||
[DynamicInterfaceCastableImplementation] | ||
private interface IProxyValueTaskSource : IValueTaskSource | ||
{ | ||
ValueTaskSourceStatus IValueTaskSource.GetStatus(short token) | ||
{ | ||
ref var source = ref Unsafe.As<FileBufferingWriter>(this).source; | ||
return source.GetStatus(token); | ||
} | ||
|
||
void IValueTaskSource.GetResult(short token) | ||
=> Unsafe.As<FileBufferingWriter>(this).GetAsyncResult(token); | ||
|
||
void IValueTaskSource.OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) | ||
{ | ||
ref var source = ref Unsafe.As<FileBufferingWriter>(this).source; | ||
source.OnCompleted(continuation, state, token, flags); | ||
} | ||
} | ||
|
||
[ExcludeFromCodeCoverage] | ||
bool IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented) | ||
{ | ||
if (interfaceType.IsOneOf([Intrinsics.TypeOf<IReadOnlyList<ReadOnlyMemory<byte>>>(), Intrinsics.TypeOf<IValueTaskSource>()])) | ||
return true; | ||
|
||
return throwIfNotImplemented ? throw new InvalidCastException() : false; | ||
} | ||
|
||
[ExcludeFromCodeCoverage] | ||
RuntimeTypeHandle IDynamicInterfaceCastable.GetInterfaceImplementation(RuntimeTypeHandle interfaceType) | ||
{ | ||
if (interfaceType.IsOneOf([Intrinsics.TypeOf<IReadOnlyList<ReadOnlyMemory<byte>>>(), Intrinsics.TypeOf<IReadOnlyCollection<ReadOnlyMemory<byte>>>()])) | ||
return Intrinsics.TypeOf<IBufferList>(); | ||
|
||
if (interfaceType.Equals(Intrinsics.TypeOf<IValueTaskSource>())) | ||
return Intrinsics.TypeOf<IProxyValueTaskSource>(); | ||
|
||
throw new InvalidCastException(); | ||
} | ||
} |
Oops, something went wrong.