Ejemplo n.º 1
0
        public void CompletionTaskIsDoneWhenCompletedWith0ConcurrencyIsCalled(bool useChannelBasedImpl)
        {
            var actionBlock = ActionBlockSlim.Create <int>(0, n => { }, useChannelBasedImpl: useChannelBasedImpl);
            var task        = actionBlock.CompletionAsync();

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Construct a WorkerAnalyzer.
 /// </summary>
 public ConcurrentPipProcessor(PackedExecutionExporter exporter)
 {
     m_exporter        = exporter;
     m_processingBlock = ActionBlockSlim.Create <ProcessFingerprintComputationEventData>(
         degreeOfParallelism: -1, // default
         processItemAction: ProcessFingerprintComputedCore);
 }
Ejemplo n.º 3
0
        public async Task IncreaseConcurrency(bool useChannelBasedImpl)
        {
            int count = 0;

            var waitForFirstTwoItems = new TaskCompletionSource <object>();
            // Event that will hold first two workers.
            var mre         = new ManualResetEventSlim(false);
            var actionBlock = ActionBlockSlim.Create <int>(
                2,
                n =>
            {
                var currentCount = Interlocked.Increment(ref count);
                if (currentCount == 2)
                {
                    // Notify the test that 2 items are processed.
                    waitForFirstTwoItems.SetResult(null);
                }

                if (currentCount <= 2)
                {
                    // This is the first or the second thread that should be blocked before we increase the number of threads.
                    mre.Wait(TimeSpan.FromSeconds(100));
                }

                Thread.Sleep(1);
            },
                useChannelBasedImpl: useChannelBasedImpl);

            // Schedule work
            actionBlock.Post(1);
            actionBlock.Post(2);

            await waitForFirstTwoItems.Task;

            // The first 2 threads should be blocked in the callback in the action block,
            // but the count should be incremented
            Assert.Equal(2, count);

            var task = actionBlock.CompletionAsync();

            // The task should not be completed yet!
            Assert.NotEqual(TaskStatus.RanToCompletion, task.Status);

            // This will cause another thread to spawn
            actionBlock.IncreaseConcurrencyTo(3);

            // Add more work
            actionBlock.Post(3);

            actionBlock.Complete();

            // Release the first 2 threads
            mre.Set();

            // Waiting for completion
            await task;

            // The new thread should run and increment the count
            Assert.Equal(3, count);
        }
Ejemplo n.º 4
0
        public async Task CompletionTaskIsDoneWhenCompletedIsCalled(bool useChannelBasedImpl)
        {
            var actionBlock = ActionBlockSlim.Create <int>(42, n => { }, useChannelBasedImpl: useChannelBasedImpl);
            var task        = actionBlock.CompletionAsync();

            Assert.NotEqual(TaskStatus.RanToCompletion, task.Status);

            actionBlock.Complete();
            await task;

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
        }
Ejemplo n.º 5
0
            public FingerprintStoreEventProcessor(int degreeOfParallelism)
            {
                Contract.Requires(degreeOfParallelism > 0);

                // To ensure that events coming from the same pips are processed according to the order when they came,
                // we use N action blocks, all with degree of parallelism 1. Thus, we potentially get parallelism across
                // different pips, but maintain sequential processing within a single pip.
                // This is necessary in particular when the runtime cache miss analyzer is enabled because
                // we use cachelookup fingerprints to perform cache miss analyzer for strongfingerprint misses.
                m_actionBlocks = new ActionBlockSlim <Action> [degreeOfParallelism];
                for (int i = 0; i < degreeOfParallelism; ++i)
                {
                    // Initially we start each action block with a parallelism of 0 so they are in a paused state.
                    // Processing is started once the RuntimeCacheMissAnalyzer is available via its initialization task completing.
                    m_actionBlocks[i] = ActionBlockSlim.Create <Action>(0, a => a());
                }
            }
Ejemplo n.º 6
0
        public async Task AllTheElementsAreFinished(bool useChannelBasedImpl)
        {
            int count       = 0;
            var actionBlock = ActionBlockSlim.Create <int>(
                42,
                n => { Interlocked.Increment(ref count); Thread.Sleep(1); },
                useChannelBasedImpl: useChannelBasedImpl);

            var task = actionBlock.CompletionAsync();

            actionBlock.Post(1);
            actionBlock.Post(2);

            actionBlock.Complete();
            await task;

            Assert.Equal(2, count);
        }
Ejemplo n.º 7
0
        public async Task AllTheElementsAreProcessedBy2Thread(bool useChannelBasedImpl)
        {
            const int maxCount    = 420;
            int       count       = 0;
            var       actionBlock = ActionBlockSlim.Create <int>(
                2,
                n => { Interlocked.Increment(ref count); },
                useChannelBasedImpl: useChannelBasedImpl);

            for (int i = 0; i < maxCount; i++)
            {
                actionBlock.Post(i);
            }

            actionBlock.Complete();
            await actionBlock.CompletionAsync();

            Assert.Equal(maxCount, count);
        }
Ejemplo n.º 8
0
        /// <nodoc />
        public PipTableSerializationScheduler(int maxDegreeOfParallelism, bool debug, Action <Pip, MutablePipState> serializer)
        {
            Contract.Requires(maxDegreeOfParallelism >= -1);
            Contract.Requires(maxDegreeOfParallelism > 0 || debug);

            m_serializer = serializer;

            maxDegreeOfParallelism = maxDegreeOfParallelism == -1 ? Environment.ProcessorCount : maxDegreeOfParallelism;
            m_nonSerializedDebug   = maxDegreeOfParallelism == 0 && debug;

            m_serializationQueue = ActionBlockSlim.Create <QueueItem>(
                m_nonSerializedDebug ? 1 : maxDegreeOfParallelism,
                item => ProcessQueueItem(item));

            if (m_nonSerializedDebug)
            {
                m_serializationQueue.Complete();    // Don't allow more changes
                m_nonSerializablePips = new List <Pip>();
            }
        }