-
Notifications
You must be signed in to change notification settings - Fork 5k
/
ReprioritizableTaskScheduler.cs
110 lines (101 loc) · 4.07 KB
/
ReprioritizableTaskScheduler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
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 ReprioritizableTaskScheduler : 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;
}
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) =>
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);
}
}
}
}