Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Introducing .NET 4.0 Parallel Programming

, 7 Apr 2010 CPOL
Introduces the Parallel Programming features of .NET 4.0.
sln.zip
sln
DataRace
bin
Debug
DataRace.exe
DataRace.vshost.exe
DataRace.vshost.exe.manifest
obj
x86
Debug
DataRace.exe
TempPE
Properties
Debugging1
bin
Debug
Debugging.exe
Debugging.vshost.exe
Debugging1.exe
Debugging1.vshost.exe
Debugging1.vshost.exe.manifest
Release
obj
x86
Debug
Debugging.exe
Debugging1.exe
TempPE
Properties
Debugging2
bin
Debug
Deadlocks.exe
Deadlocks.vshost.exe
Deadlocks.vshost.exe.manifest
Debugging2.exe
Debugging2.vshost.exe
Debugging2.vshost.exe.manifest
Release
obj
x86
Debug
Deadlocks.exe
Debugging2.exe
TempPE
Properties
Parallel
bin
Debug
Parallel.exe
Parallel.vshost.exe
Parallel.vshost.exe.manifest
obj
x86
Debug
Parallel.exe
TempPE
Properties
ParallelExtensionsExtras
bin
Debug
ParallelExtensionsExtras.dll
CoordinationDataStructures
AsyncCoordination
Drawing
Extensions
APM
EAP
TaskFactoryExtensions
obj
Debug
ParallelExtensionsExtras.dll
TempPE
ParallelAlgorithms
Partitioners
Properties
TaskSchedulers
Utils
PLINQ
bin
Debug
PLINQ.exe
PLINQ.vshost.exe
PLINQ.vshost.exe.manifest
obj
x86
Debug
PLINQ.exe
TempPE
Properties
PLINQExtensions
bin
Debug
PLINQExtensions.exe
PLINQExtensions.vshost.exe
PLINQExtensions.vshost.exe.manifest
obj
x86
Debug
PLINQExtensions.exe
TempPE
Properties
Raytracer_CSharp
bin
Debug
ParallelExtensionsExtras.dll
Raytracer.exe
Raytracer.vshost.exe
Raytracer.vshost.exe.manifest
obj
Debug
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
TempPE
Properties
Settings.settings
Tasks
bin
Debug
Tasks.exe
Tasks.vshost.exe
Tasks.vshost.exe.manifest
obj
x86
Debug
TempPE
Properties
Temp
bin
Debug
Temp.exe
obj
x86
Debug
TempPE
Properties
//--------------------------------------------------------------------------
// 
//  Copyright (c) Microsoft Corporation.  All rights reserved. 
// 
//  File: PrioritizingTaskScheduler.cs
//
//--------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;

namespace System.Threading.Tasks.Schedulers
{
    /// <summary>Provides a task scheduler that supports reprioritizing previously queued tasks.</summary>
    public sealed class PrioritizingTaskScheduler : TaskScheduler
    {
        private readonly LinkedList<Task> _tasks = new LinkedList<Task>(); // protected by lock(_tasks)

        /// <summary>Queues a task to the scheduler.</summary>
        /// <param name="task">The task to be queued.</param>
        protected override void QueueTask(Task task)
        {
            // Store the task, and notify the ThreadPool of work to be processed
            lock (_tasks) _tasks.AddLast(task);
            ThreadPool.UnsafeQueueUserWorkItem(ProcessNextQueuedItem, null);
        }

        /// <summary>Reprioritizes a previously queued task to the front of the queue.</summary>
        /// <param name="task">The task to be reprioritized.</param>
        /// <returns>Whether the task could be found and moved to the front of the queue.</returns>
        public bool Prioritize(Task task)
        {
            lock (_tasks)
            {
                var node = _tasks.Find(task);
                if (node != null)
                {
                    _tasks.Remove(node);
                    _tasks.AddFirst(node);
                    return true;
                }
            }
            return false;
        }

        /// <summary>Reprioritizes a previously queued task to the back of the queue.</summary>
        /// <param name="task">The task to be reprioritized.</param>
        /// <returns>Whether the task could be found and moved to the back of the queue.</returns>
        public bool Deprioritize(Task task)
        {
            lock (_tasks)
            {
                var node = _tasks.Find(task);
                if (node != null)
                {
                    _tasks.Remove(node);
                    _tasks.AddLast(node);
                    return true;
                }
            }
            return false;
        }

        /// <summary>Removes a previously queued item from the scheduler.</summary>
        /// <param name="task">The task to be removed.</param>
        /// <returns>Whether the task could be removed from the scheduler.</returns>
        protected override bool TryDequeue(Task task)
        {
            lock (_tasks) return _tasks.Remove(task);
        }

        /// <summary>Picks up and executes the next item in the queue.</summary>
        /// <param name="ignored">Ignored.</param>
        private void ProcessNextQueuedItem(object ignored)
        {
            Task t;
            lock (_tasks)
            {
                if (_tasks.Count > 0)
                {
                    t = _tasks.First.Value;
                    _tasks.RemoveFirst();
                }
                else return;
            }
            base.TryExecuteTask(t);
        }

        /// <summary>Executes the specified task inline.</summary>
        /// <param name="task">The task to be executed.</param>
        /// <param name="taskWasPreviouslyQueued">Whether the task was previously queued.</param>
        /// <returns>Whether the task could be executed inline.</returns>
        protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
        {
            return TryExecuteTask(task);
        }

        /// <summary>Gets all of the tasks currently queued to the scheduler.</summary>
        /// <returns>An enumerable of the tasks currently queued to the scheduler.</returns>
        protected override IEnumerable<Task> GetScheduledTasks()
        {
            bool lockTaken = false;
            try
            {
                Monitor.TryEnter(_tasks, ref lockTaken);
                if (lockTaken) return _tasks.ToArray();
                else throw new NotSupportedException();
            }
            finally
            {
                if (lockTaken) Monitor.Exit(_tasks);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 7 Apr 2010
Article Copyright 2010 by mohamad halabi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid