Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/csharp/src/AsyncSupport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Helpers for the async support.
*/

public enum CallbackCode
{
Exit = 0,
Yield = 1,
}
59 changes: 59 additions & 0 deletions crates/csharp/src/FutureCommonSupport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Helpers for future support.
*/

public readonly struct WaitableStatus (int status)
{
public int State => status & 0xf;
public int Count => (int)(status >> 4);
public bool IsBlocked => status == -1;
public bool IsCompleted => State == 0;
public bool IsDropped => State == 1;
}

public enum EventCode
{
None,
Subtask,
StreamRead,
StreamWrite,
FutureRead,
FutureWrite,
Cancel,
}

public readonly struct EventWaitable
{
public EventWaitable(EventCode eventCode, int waitable, int code)
{
Event = eventCode;
Waitable = waitable;
Status = new WaitableStatus(code);
}
public readonly EventCode Event;
public readonly int Waitable;
public readonly int Code;

public readonly WaitableStatus Status;
}

public partial class WaitableSet(int handle) : IDisposable
{
public int Handle { get; } = handle;

void Dispose(bool _disposing)
{
{{interop_name}}.WaitableSetDrop(Handle);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~WaitableSet()
{
Dispose(false);
}
}
49 changes: 49 additions & 0 deletions crates/csharp/src/FutureReaderSupport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Helpers for future reader support.
*/

public abstract class FutureReader(int handle) : IDisposable // : TODO Waitable
{
public int Handle { get; } = handle;

// TODO: Generate per type for this instrinsic.
public Task Read()
{
// TODO: Generate for the interop name and the namespace.

var status = new WaitableStatus(ReadInternal());
if (status.IsBlocked)
{
//TODO: store somewhere so we can complete it later.
var tcs = new TaskCompletionSource();

return tcs.Task;
}
if (status.IsCompleted)
{
return Task.CompletedTask;
}

throw new NotImplementedException();
}

void Dispose(bool _disposing)
{
// Free unmanaged resources if any.
Drop();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~FutureReader()
{
Dispose(false);
}

protected abstract int ReadInternal();
protected abstract void Drop();
}
44 changes: 44 additions & 0 deletions crates/csharp/src/FutureWriterSupport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Helpers for future writer support.
*/

public abstract class FutureWriter(int handle) // : TODO Waitable
{
public int Handle { get; } = handle;

// TODO: Generate per type for this instrinsic.
public Task Write()
{
// TODO: Generate for the interop name.
var status = new WaitableStatus(Write(Handle, IntPtr.Zero));
if (status.IsBlocked)
{
//TODO: store somewhere so we can complete it later.
var tcs = new TaskCompletionSource();
return tcs.Task;
}

throw new NotImplementedException();
}

protected abstract void Drop();

void Dispose(bool _disposing)
{
// Free unmanaged resources if any.
Drop();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~FutureWriter()
{
Dispose(false);
}

protected abstract int Write(int handle, IntPtr buffer);
}
14 changes: 7 additions & 7 deletions crates/csharp/src/csproj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ impl CSProjectLLVMBuilder {
csproj.push_str(
&format!(
r#"
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM" Version="10.0.0-*" />
<PackageReference Include="runtime.{os}-x64.Microsoft.DotNet.ILCompiler.LLVM" Version="10.0.0-*" />
</ItemGroup>
"#),
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM" Version="10.0.0-*" />
<PackageReference Include="runtime.{os}-x64.Microsoft.DotNet.ILCompiler.LLVM" Version="10.0.0-*" />
</ItemGroup>"#),
);

fs::write(
Expand Down Expand Up @@ -153,8 +152,9 @@ impl CSProjectLLVMBuilder {
}

csproj.push_str(
r#"</Project>
"#,
r#"
</Project>
"#,
);

fs::write(self.dir.join(format!("{camel}.csproj")), csproj)?;
Expand Down
Loading
Loading