Example #1
0
 static async void AddOne(WritableChannel <int> output, ReadableChannel <int> input)
 {
     await output.WriteAsync(1 + await input.ReadAsync());
 }
 public static Task <T> ReadAsync <T>(this ReadableChannel <T> channel)
 {
     return(channel.ReadAsync(CancellationToken.None, acceptanceTest => true));
 }
        private async Task ProcessCaseAsync <T>(ReadableChannel <T> channel, Func <T, Task> callback, Func <T, bool> additionalAcceptanceTest)
        {
            try {
                while (!cts.IsCancellationRequested)
                {
                    bool isFinalDispatch = false;
                    T    item;
                    try {
                        item = await channel.ReadAsync(
                            cts.Token,
                            x => {
                            if (!additionalAcceptanceTest(x))
                            {
                                return(false);
                            }
                            if (Interlocked.CompareExchange(ref dispatchesRemaining, 0, 0) == kTimesInfinite)
                            {
                                return(true);
                            }
                            else
                            {
                                var spinner = new SpinWait();
                                while (true)
                                {
                                    var capturedDispatchesRemaining = Interlocked.CompareExchange(ref dispatchesRemaining, 0, 0);
                                    var nextDispatchesRemaining     = capturedDispatchesRemaining - 1;

                                    if (nextDispatchesRemaining < 0)
                                    {
                                        return(false);
                                    }

                                    if (Interlocked.CompareExchange(ref dispatchesRemaining, nextDispatchesRemaining, capturedDispatchesRemaining) == capturedDispatchesRemaining)
                                    {
                                        isFinalDispatch = nextDispatchesRemaining == 0;
                                        return(true);
                                    }
                                    spinner.SpinOnce();
                                }
                            }
                        }).ConfigureAwait(false);
                    } catch (OperationCanceledException) {
                        //Exit processing loop - no dispatches remaining
                        break;
                    }

                    // Signal other case workers to exit
                    if (isFinalDispatch)
                    {
                        cts.Cancel();
                    }

                    // Execute callback
                    await callback(item).ConfigureAwait(false);

                    // Mark dispatcher as completed, signal awaiters
                    if (isFinalDispatch)
                    {
                        IsCompleted = true;
                        completionBox.SetResult(true);
                    }
                }
            } catch (Exception ex) {
                // Signal all other case workers to exit
                cts.Cancel();

                // Bubble all exceptions up to dispatcher awaiters
                IsCompleted = true;
                completionBox.SetException(ex);
            }
        }
 public static T Read <T>(this ReadableChannel <T> channel)
 {
     return(channel.ReadAsync().Result);
 }
Example #5
0
 static async void AddOne(WritableChannel <int> output, ReadableChannel <int> input)
 {
     await output.WriteAsync(1 + await input.ReadAsync().ConfigureAwait(false));
 }