@@ -184,6 +184,7 @@ public static unsafe uint Callback(EventWaitable e, ContextTask* contextPtr, Act
184184 if ( waitables . Count == 0 )
185185 {
186186 Console . WriteLine ( $ "No more waitables for waitable { e . Waitable } in set { contextTaskPtr ->WaitableSetHandle } ") ;
187+ Console . WriteLine ( $ "taskReturn is null { taskReturn == null } ") ;
187188 if ( taskReturn != null )
188189 {
189190 taskReturn ( ) ;
@@ -206,7 +207,7 @@ internal static unsafe Task TaskFromStatus(uint status)
206207 var subtaskStatus = new SubtaskStatus ( status ) ;
207208 status = status & 0xF ;
208209
209- var tcs = new TaskCompletionSource ( ) ;
210+ var tcs = new TaskCompletionSource < int > ( ) ;
210211 if ( subtaskStatus . IsSubtaskStarting || subtaskStatus . IsSubtaskStarted )
211212 {
212213 ContextTask * contextTaskPtr = ContextGet ( ) ;
@@ -215,11 +216,13 @@ internal static unsafe Task TaskFromStatus(uint status)
215216 contextTaskPtr = AllocateAndSetNewContext ( ) ;
216217 }
217218
219+ AsyncSupport . Join ( subtaskStatus . Handle , contextTaskPtr ->WaitableSetHandle , new WaitableInfoState ( tcs ) ) ;
220+
218221 return tcs . Task ;
219222 }
220223 else if ( subtaskStatus . IsSubtaskReturned )
221224 {
222- tcs . SetResult ( ) ;
225+ tcs . SetResult ( 0 ) ;
223226 return Task . CompletedTask ;
224227 }
225228 else
@@ -232,24 +235,23 @@ internal static unsafe Task TaskFromStatus(uint status)
232235 public static unsafe Task < T > TaskFromStatus < T > ( uint status , Func < T > liftFunc )
233236 {
234237 var subtaskStatus = new SubtaskStatus ( status ) ;
235- status = status & 0xF ;
236238
237- // TODO join and complete the task somwhere.
238- var tcs = new TaskCompletionSource < T > ( ) ;
239239 if ( subtaskStatus . IsSubtaskStarting || subtaskStatus . IsSubtaskStarted )
240240 {
241241 ContextTask * contextTaskPtr = ContextGet ( ) ;
242242 if ( contextTaskPtr == null ) {
243- contextTaskPtr = ( ContextTask * ) Marshal . AllocHGlobal ( Marshal . SizeOf < ContextTask > ( ) ) ;
244- Console . WriteLine ( "TaskFromStatus<T> creating WaitableSet" ) ;
245- contextTaskPtr ->WaitableSetHandle = WaitableSetNew ( ) ;
246- ContextSet ( contextTaskPtr ) ;
243+ contextTaskPtr = AllocateAndSetNewContext ( ) ;
247244 }
248245
246+ var intTaskCompletionSource = new TaskCompletionSource < int > ( ) ;
247+ var tcs = new LiftingTaskCompletionSource < T > ( intTaskCompletionSource , liftFunc ) ;
248+ AsyncSupport . Join ( subtaskStatus . Handle , contextTaskPtr ->WaitableSetHandle , new WaitableInfoState ( intTaskCompletionSource ) ) ;
249+
249250 return tcs . Task ;
250251 }
251252 else if ( subtaskStatus . IsSubtaskReturned )
252253 {
254+ var tcs = new TaskCompletionSource < T > ( ) ;
253255 tcs . SetResult ( liftFunc ( ) ) ;
254256 return tcs . Task ;
255257 }
@@ -259,12 +261,24 @@ public static unsafe Task<T> TaskFromStatus<T>(uint status, Func<T> liftFunc)
259261 }
260262 }
261263
264+ internal class LiftingTaskCompletionSource < T > : TaskCompletionSource < T >
265+ {
266+ internal LiftingTaskCompletionSource ( TaskCompletionSource < int > innerTaskCompletionSource , Func < T > _liftFunc )
267+ {
268+ innerTaskCompletionSource . Task . ContinueWith ( t => {
269+ throw new NotImplementedException ( "lifting results from async functions not implemented yet" ) ;
270+ } ) ;
271+ }
272+ }
273+
262274 // unsafe because we are working with native memory.
263275 internal static unsafe ContextTask * AllocateAndSetNewContext ( )
264276 {
277+ Console . WriteLine ( "AllocateAndSetNewContext creating WaitableSet" ) ;
278+
265279 var contextTaskPtr = ( ContextTask * ) Marshal . AllocHGlobal ( Marshal . SizeOf < ContextTask > ( ) ) ;
266- contextTaskPtr ->WaitableSetHandle = AsyncSupport . WaitableSetNew ( ) ;
267- AsyncSupport . ContextSet ( contextTaskPtr ) ;
280+ contextTaskPtr ->WaitableSetHandle = WaitableSetNew ( ) ;
281+ ContextSet ( contextTaskPtr ) ;
268282 return contextTaskPtr ;
269283 }
270284}
@@ -878,16 +892,21 @@ internal WaitableInfoState(ComponentTask componentTask, IFutureStream futureStre
878892 FutureStream = futureStream ;
879893 }
880894
881- internal WaitableInfoState ( ComponentTask componentTask )
895+ internal WaitableInfoState ( TaskCompletionSource < int > taskCompletionSource )
882896 {
883- this . componentTask = componentTask ;
897+ this . taskCompletionSource = taskCompletionSource ;
884898 }
885899
886900 internal void SetResult ( int count )
887901 {
888- Console . WriteLine ( "WaitableInfoState SetResult" ) ;
902+ Console . WriteLine ( "WaitableInfoState SetResult" ) ;
889903
890- if ( componentTask != null )
904+ if ( taskCompletionSource != null )
905+ {
906+ Console . WriteLine ( "Setting result for Task" ) ;
907+ taskCompletionSource . SetResult ( count ) ;
908+ }
909+ else if ( componentTask != null )
891910 {
892911 Console . WriteLine ( "Setting result for void waitable completion source" ) ;
893912 componentTask . SetResult ( ) ;
@@ -916,9 +935,11 @@ internal void SetException(Exception e)
916935 }
917936 }
918937
919- private ComponentTask componentTask ;
920- private ComponentTask < int > componentTaskInt ;
921- internal IFutureStream FutureStream ;
938+ // We have a taskCompletionSource for an async function, a ComponentTask for a future or stream.
939+ private TaskCompletionSource < int > ? taskCompletionSource ;
940+ private ComponentTask ? componentTask ;
941+ private ComponentTask < int > ? componentTaskInt ;
942+ internal IFutureStream ? FutureStream ;
922943}
923944
924945public class StreamDroppedException : Exception
0 commit comments