コード例 #1
0
        public async Task <int> AsyncSemaphore_ConcurrentLoadAsync()
        {
            var tasks = Enumerable.Range(0, CC_LOAD).Select(async _ =>
            {
                int success = 0;
                for (int t = 0; t < CC_LOAD; t++)
                {
                    await _asyncSemaphore.WaitAsync().ConfigureAwait(false);
                    try
                    {
                        success++;
                        await Task.Yield();
                    }
                    finally
                    {
                        _asyncSemaphore.Release();
                    }
                    await Task.Yield();
                }
                return(success);
            }).ToArray();

            await Task.WhenAll(tasks).ConfigureAwait(false);

            int total = tasks.Sum(x => x.Result);

            return(total.AssertIs(CC_LOAD * CC_LOAD));
        }
コード例 #2
0
        internal static async Task <TResult> ImplementationAsync <TResult>(
            Func <CancellationToken, Task <TResult> > action,
            Context context,
            Func <Context, Task> onBulkheadRejectedAsync,
            SemaphoreSlim maxParallelizationSemaphore,
            SemaphoreSlim maxQueuedActionsSemaphore,
            CancellationToken cancellationToken,
            bool continueOnCapturedContext)
        {
            if (!await maxQueuedActionsSemaphore.WaitAsync(TimeSpan.Zero, cancellationToken).ConfigureAwait(continueOnCapturedContext))
            {
                await onBulkheadRejectedAsync(context).ConfigureAwait(continueOnCapturedContext);

                throw new BulkheadRejectedException();
            }
            try
            {
                await maxParallelizationSemaphore.WaitAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext);

                try
                {
                    return(await action(cancellationToken).ConfigureAwait(continueOnCapturedContext));
                }
                finally
                {
                    maxParallelizationSemaphore.Release();
                }
            }
            finally
            {
                maxQueuedActionsSemaphore.Release();
            }
        }