private async Task add(T value, IAccessContext <CSScope> context) { await TaskCollector.With(async tc => { CSScope pessimisticScope = new CSScope(RWScope.ReadWrite, InfiniteIntervalScope.Create( RWScope.ReadWrite, 0, null), $"pessimistic scope for add-{value}"); await context.InChildWithin(pessimisticScope, async childContext => { Tuple <node, int> lastNodeInfo = await getLastNode(childContext); /* At this point, we don’t require write access * to the nodes from 0 to count - 1 anymore, * but IInfiniteIntervalScope is not granular enough * to represent that. * Read tasks for lower indexes could proceed. */ node newNode = new node { Successor = null, Value = value }; tc.Add(appendToLastNode( newNode, lastNodeInfo, childContext)); }); }); }
public static async Task Add3(IAsyncBox <int> box) { await TaskCollector.With(async tc => { int value = await tc.Adding(box.Get()); await box.Set((value + 3).ToFuture()); }); }
public static async Task FibonacciTest() { await TaskCollector.With(tasks => { Console.WriteLine("Fibonacci:"); var fiboNums = new ContextualStream <BigInteger>(); tasks.Add(addFibonacciNumbers(fiboNums)); tasks.Add(PrintValues(fiboNums)); }); }
public IReactive <bool> MoveNextAsync() { return(Reactive.From <bool>(async resultResolver => { await TaskCollector.With(async tc => { using (ContextualStream <T> childStream = stream.CreateChildWithin(new CSScope(valueScope: InfiniteIntervalScope.Create( RWScope.Read, 0, null)))) using (IAccessContext <enumeratorScope> childEnumContext = context.CreateChild(new enumeratorScope( RWScope.ReadWrite, RWScope.ReadWrite))) { long curNextIndex = await readNextIndex(childEnumContext); CSScope readNextNodeScope = new CSScope(valueScope: InfiniteIntervalScope.Create(RWScope.Read, curNextIndex, curNextIndex + 1)); childStream.Context.RequiredScope = readNextNodeScope; node nextNode; if (curNextIndex == 0) { /* Erste Iteration. */ tc.Add(incrementNextIndex(childEnumContext)); nextNode = await childStream.getFirstNode( childStream.Context); } else { node curCurrentNode = await readCurrentNode(childEnumContext); if (curCurrentNode == null) { throw new InvalidOperationException( "The stream has already " + "been iterated through."); } tc.Add(incrementNextIndex(childEnumContext)); await childStream.Context.UntilAvailable(); nextNode = curCurrentNode.Successor; } resultResolver.Resolve(nextNode != null); childStream.Dispose(); tc.Add(setCurrentNode(nextNode, childEnumContext)); } }); })); }
public static async Task RunAsync() { await TaskCollector.With(tc => { IAsyncBox <int> box = AsyncBox.Create(2); Console.WriteLine(tc.Adding(box.Get()).Value); tc.Add(box.Set(3.ToFuture())); Console.WriteLine(tc.Adding(box.Get()).Value); tc.Add(Add3(box)); Console.WriteLine(tc.Adding(box.Get()).Value); tc.Add(DoSet(box, 4.ToFuture())); Console.WriteLine(tc.Adding(box.Get()).Value); }); }
public static async Task SimpleTest() { await TaskCollector.With(async tasks => { ContextualStream <int> stream = new ContextualStream <int>(); tasks.Add(stream.Add(3)); AssertEquals(await stream.Get(0), 3); tasks.Add(stream.Add(5)); tasks.Add(stream.Add(7)); AssertEquals(stream.Get(2).Result, 7); AssertEquals(stream.Get(0).Result, 3); await PrintValues(stream); }); }
public static Task DoCombined(this MyComponent parent, string name) { // calculate access Scope long scope = 3; return(TaskCollector.With(tc => { parent.InChild(scope, component => { tc.Add(component.DoA(name + ".1")); tc.Add(component.DoB(name + ".2")); }); })); }
private static async Task addFibonacciNumbers( ContextualStream <BigInteger> stream, int count = 30) { await TaskCollector.With(async tc => { await stream.InChild(new CSScope( RWScope.ReadWrite, InfiniteIntervalScope.Create( RWScope.ReadWrite, 0, null)), async childStream => { await Task.Yield(); IAppender <BigInteger> appender = childStream.GetAppender(); BigInteger a = 1, b = 1; tc.Add(appender.Append(a)); /* FIXME: Users of appender generally don’t know * the internals of ContextualStream. * Poss. solution: IAppender.MaxRequiredScope. * Then it would also be useful, if (the) access scopes * could be merged. */ childStream.Context.RequiredScope = new CSScope( RWScope.ReadWrite, InfiniteIntervalScope.Create( RWScope.ReadWrite, 1, null)); tc.Add(appender.Append(b)); childStream.Context.RequiredScope = new CSScope( RWScope.ReadWrite, InfiniteIntervalScope.Create( RWScope.ReadWrite, 2, null)); for (int i = 2; i < count; i++) { BigInteger c = a + b; await Task.Delay(250); a = b; b = c; tc.Add(appender.Append(c)); childStream.Context.RequiredScope = new CSScope( RWScope.ReadWrite, InfiniteIntervalScope.Create( RWScope.ReadWrite, i + 1, null)); } }); }); }
public async Task Append(T value) { await TaskCollector.With(async tc => { using (var lknChild = lastKnownNode.CreateChild(RWScope.ReadWrite)) using (var lknIndexChild = lastKnownNodeIndex.CreateChild(RWScope.ReadWrite)) using (var streamChildContext = stream.Context.CreateChildWithin( new CSScope( RWScope.ReadWrite, InfiniteIntervalScope.Create( RWScope.ReadWrite, 0, null)))) { node lkn = await tc.Adding(lknChild.Get()); int lknIndex; if (lkn == null) { lknIndex = -1; } else { lknIndex = await tc.Adding(lknIndexChild.Get()); } Tuple <node, int> lastNodeInfo = await stream.getLastNode( streamChildContext, lkn, lknIndex); node newNode = new node { Value = value, Successor = null, }; tc.Add(stream.appendToLastNode( newNode, lastNodeInfo, streamChildContext)); streamChildContext.Dispose(); tc.Add(lknChild.Set(newNode.ToFuture())); tc.Add(lknIndexChild.Set( ((lastNodeInfo?.Item2 ?? -1) + 1).ToFuture())); } }); }
public static async Task PrintValues <T>(IChildContextualCreator < CSScope, ContextualStream <T> > subContextFactory) { await subContextFactory.InChild(new CSScope(valueScope: InfiniteIntervalScope.Create(RWScope.Read, 0, null)), async ints => { await TaskCollector.With(async tc => { IAsyncEnumerator <T> enumerator = ints.GetAsyncEnumerator(); while (await tc.Adding(enumerator.MoveNextAsync())) { T value = await tc.Adding(enumerator.GetCurrentAsync()); Console.WriteLine(value); } }); }); }
private static async Task run() { Console.WriteLine("\nasynchronous test"); DateTime start = DateTime.Now; await TaskCollector.With(tc => { using (var comp = new MyComponent("test component 1")) { tc.Add(comp.DoC("t0")); tc.Add(comp.DoA("t1")); tc.Add(comp.DoB("t2")); tc.Add(comp.DoCombined("t3")); tc.Add(comp.DoB("t4")); tc.Add(comp.DoC("t5")); } }); Console.WriteLine($"duration: {DateTime.Now - start}"); Console.WriteLine("\nsynchronous test"); start = DateTime.Now; using (var comp = new MyComponent("test component 2")) { await comp.DoC("t0"); await comp.DoA("t1"); await comp.DoB("t2"); await comp.DoCombinedSync("t3"); await comp.DoB("t4"); await comp.DoC("t5"); } Console.WriteLine($"duration: {DateTime.Now - start}"); }