Skip to content

Commit a8879c1

Browse files
committed
address feedback, remove redundant parameters and duplicate Callback function
1 parent 7f6a5b1 commit a8879c1

3 files changed

Lines changed: 20 additions & 94 deletions

File tree

crates/csharp/src/AsyncSupport.cs

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ public static unsafe void ContextSet(ContextTask* contextTask)
133133
}
134134

135135
// unsafe because we are using pointers.
136-
public static unsafe int Callback(EventWaitable e, ContextTask* contextPtr)
136+
public static unsafe int Callback(EventWaitable e)
137137
{
138-
Console.WriteLine("Callback");
139138
ContextTask* contextTaskPtr = ContextGet();
140139

141140
var waitables = pendingTasks[contextTaskPtr->WaitableSetHandle];
@@ -152,77 +151,21 @@ public static unsafe int Callback(EventWaitable e, ContextTask* contextPtr)
152151
waitables.Remove(e.Waitable, out _);
153152
if (e.IsSubtask)
154153
{
155-
if (e.SubtaskStatus.IsStarting)
154+
switch (e.SubtaskStatus)
156155
{
157-
throw new Exception("unexpected subtask status Starting " + e.Code);
158-
}
159-
if (e.SubtaskStatus.IsStarted || e.SubtaskStatus.IsReturned)
160-
{
161-
waitableInfoState.SetResult(e.WaitableCount);
162-
Interop.SubtaskDrop(e.Waitable);
163-
}
164-
else
165-
{
166-
throw new Exception("TODO: subtask status " + e.Code);
167-
}
168-
}
169-
else
170-
{
171-
if (e.IsDropped)
172-
{
173-
waitableInfoState.SetException(new StreamDroppedException());
174-
}
175-
else
176-
{
177-
// This may add a new waitable to the set.
178-
waitableInfoState.SetResult(e.WaitableCount);
179-
}
180-
}
156+
case { IsStarting: true }:
157+
throw new Exception("unexpected subtask status Starting " + e.Code);
181158

182-
if (waitables.Count == 0)
183-
{
184-
ContextSet(null);
185-
Marshal.FreeHGlobal((IntPtr)contextTaskPtr);
186-
return (int)CallbackCode.Exit;
187-
}
159+
case { IsStarted: true }:
160+
break;
188161

189-
return (int)CallbackCode.Wait | (int)(contextTaskPtr->WaitableSetHandle << 4);
190-
}
162+
case { IsReturned: true }:
163+
waitableInfoState.SetResult(e.WaitableCount);
164+
Interop.SubtaskDrop(e.Waitable);
165+
break;
191166

192-
throw new NotImplementedException($"WaitableStatus not implemented {e.WaitableStatus.State} in set {contextTaskPtr->WaitableSetHandle}");
193-
}
194-
195-
// unsafe because we are using pointers.
196-
public static unsafe int Callback<T>(EventWaitable e, Func<T> liftFunc)
197-
{
198-
Console.WriteLine("Callback");
199-
ContextTask* contextTaskPtr = ContextGet();
200-
201-
var waitables = pendingTasks[contextTaskPtr->WaitableSetHandle];
202-
var waitableInfoState = waitables[e.Waitable];
203-
204-
if (e.IsDropped)
205-
{
206-
waitableInfoState.FutureStream!.OtherSideDropped();
207-
}
208-
209-
if (e.IsCompleted || e.IsDropped)
210-
{
211-
// The operation is complete so we can free the buffer and remove the waitable from our dicitonary
212-
waitables.Remove(e.Waitable, out _);
213-
if (e.IsSubtask)
214-
{
215-
if (e.SubtaskStatus.IsStarting)
216-
{
217-
throw new Exception("unexpected subtask status Starting " + e.Code);
218-
}
219-
if (e.SubtaskStatus.IsStarted || e.SubtaskStatus.IsReturned)
220-
{
221-
Interop.SubtaskDrop(e.Waitable);
222-
}
223-
else
224-
{
225-
throw new Exception("TODO: subtask status " + e.Code);
167+
default:
168+
throw new Exception("TODO: subtask status " + e.Code);
226169
}
227170
}
228171
else
@@ -294,10 +237,6 @@ public static unsafe Task<T> TaskFromStatus<T>(uint status, Func<T> liftFunc)
294237
}
295238

296239
var intTaskCompletionSource = new TaskCompletionSource<int>();
297-
intTaskCompletionSource.Task.ContinueWith( t =>
298-
{
299-
Console.WriteLine("intTaskCompletionSource continuewith");
300-
}, TaskContinuationOptions.ExecuteSynchronously);
301240
var tcs = new LiftingTaskCompletionSource<T>(intTaskCompletionSource, liftFunc);
302241
Join(subtaskStatus.Handle, contextTaskPtr->WaitableSetHandle, new WaitableInfoState(intTaskCompletionSource));
303242

crates/csharp/src/FutureCommonSupport.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public readonly struct EventWaitable
2727
{
2828
public EventWaitable(EventCode eventCode, uint waitable, uint code)
2929
{
30-
Console.WriteLine($"EventWaitable with code {code}");
3130
EventCode = eventCode;
3231
Waitable = (int)waitable;
3332
Code = code;

crates/csharp/src/interface.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ impl InterfaceGenerator<'_> {
432432
public static unsafe void {future_stream_name}Lower{upper_camel_future_type}(object toLower, List<Action> cleanups)
433433
{{
434434
var ptr = InteropReturnArea.returnArea.AddressOfReturnArea();
435-
Console.WriteLine("type is " + toLower.GetType());
436435
var typedToLower = ({qualified_generic_type_name})toLower;
437436
{lower_code}
438437
}}
@@ -963,30 +962,18 @@ var {async_status_var} = {raw_name}({wasm_params});
963962
[global::System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(EntryPoint = "[callback]{export_name}")]
964963
public static unsafe uint {camel_name}Callback(int eventRaw, uint waitable, uint code)
965964
{{
966-
Console.WriteLine("Callback for {export_name} creating EventWaitable with code " + eventRaw + " for waitable " + waitable + " with code " + code);
967965
EventWaitable e = new EventWaitable((EventCode)eventRaw, waitable, code);
968966
969967
"#
970968
);
971969

972-
// TODO: Get the results from a static dictionary?
973-
if sig.results.len() > 0 {
974-
uwriteln!(
975-
self.csharp_interop_src,
976-
r#"
977-
return (uint)AsyncSupport.Callback(e, (ContextTask *)IntPtr.Zero);
978-
}}
979-
"#
980-
);
981-
} else {
982-
uwriteln!(
983-
self.csharp_interop_src,
984-
r#"
985-
return (uint)AsyncSupport.Callback(e, (ContextTask *)IntPtr.Zero);
986-
}}
987-
"#
988-
);
989-
}
970+
uwriteln!(
971+
self.csharp_interop_src,
972+
r#"
973+
return (uint)AsyncSupport.Callback(e);
974+
}}
975+
"#
976+
);
990977
}
991978

992979
if abi::guest_export_needs_post_return(self.resolve, func) {
@@ -1963,6 +1950,7 @@ impl<'a> CoreInterfaceGenerator<'a> for InterfaceGenerator<'a> {
19631950

19641951
// TODO: Is this not publicly available elsewhere, a function that says if a type needs to be returned as a pointer
19651952
fn needs_ptr(ty: &Type) -> bool {
1953+
// TODO: This list is not complete, e.g. handles should be here.
19661954
match *ty {
19671955
Type::Bool
19681956
| Type::S8

0 commit comments

Comments
 (0)