|
|
Imports System.Collections.Concurrent |
|
|
Imports System.Collections.Generic |
|
|
Imports System.Linq |
|
|
Imports System.Threading |
|
|
Imports System.Threading.Tasks |
|
|
|
|
|
Namespace TaskSchedulers |
|
|
|
|
|
|
|
|
|
|
|
Public Class LimitedConcurrencyLevelTaskScheduler : Inherits TaskScheduler |
|
|
|
|
|
<ThreadStatic()> Private Shared _currentThreadIsProcessingItems As Boolean |
|
|
|
|
|
|
|
|
Private ReadOnly _tasks As LinkedList(Of Task) = New LinkedList(Of Task)() |
|
|
|
|
|
|
|
|
Private ReadOnly _maxDegreeOfParallelism As Integer |
|
|
|
|
|
|
|
|
Private _delegatesQueuedOrRunning As Integer = 0 |
|
|
|
|
|
|
|
|
Public Sub New(ByVal maxDegreeOfParallelism As Integer) |
|
|
If (maxDegreeOfParallelism < 1) Then |
|
|
Throw New ArgumentOutOfRangeException("maxDegreeOfParallelism") |
|
|
End If |
|
|
_maxDegreeOfParallelism = maxDegreeOfParallelism |
|
|
End Sub |
|
|
|
|
|
|
|
|
Protected Overrides Sub QueueTask(ByVal t As Task) |
|
|
|
|
|
|
|
|
SyncLock (_tasks) |
|
|
_tasks.AddLast(t) |
|
|
If (_delegatesQueuedOrRunning < _maxDegreeOfParallelism) Then |
|
|
_delegatesQueuedOrRunning = _delegatesQueuedOrRunning + 1 |
|
|
NotifyThreadPoolOfPendingWork() |
|
|
End If |
|
|
End SyncLock |
|
|
End Sub |
|
|
|
|
|
|
|
|
Private Sub NotifyThreadPoolOfPendingWork() |
|
|
|
|
|
ThreadPool.UnsafeQueueUserWorkItem(Sub() |
|
|
|
|
|
|
|
|
_currentThreadIsProcessingItems = True |
|
|
Try |
|
|
|
|
|
While (True) |
|
|
Dim item As Task |
|
|
SyncLock (_tasks) |
|
|
|
|
|
|
|
|
If (_tasks.Count = 0) Then |
|
|
_delegatesQueuedOrRunning = _delegatesQueuedOrRunning - 1 |
|
|
Exit While |
|
|
End If |
|
|
|
|
|
|
|
|
item = _tasks.First.Value |
|
|
_tasks.RemoveFirst() |
|
|
End SyncLock |
|
|
|
|
|
|
|
|
MyBase.TryExecuteTask(item) |
|
|
End While |
|
|
|
|
|
Finally |
|
|
_currentThreadIsProcessingItems = False |
|
|
End Try |
|
|
End Sub, |
|
|
Nothing) |
|
|
End Sub |
|
|
|
|
|
|
|
|
Protected Overrides Function TryExecuteTaskInline(ByVal t As Task, |
|
|
ByVal taskWasPreviouslyQueued As Boolean) As Boolean |
|
|
|
|
|
If (Not _currentThreadIsProcessingItems) Then |
|
|
Return False |
|
|
End If |
|
|
|
|
|
|
|
|
If (taskWasPreviouslyQueued) Then |
|
|
|
|
|
If TryDequeue(t) Then |
|
|
Return MyBase.TryExecuteTask(t) |
|
|
Else |
|
|
Return False |
|
|
End If |
|
|
Else |
|
|
Return MyBase.TryExecuteTask(t) |
|
|
End If |
|
|
End Function |
|
|
|
|
|
|
|
|
Protected Overrides Function TryDequeue(ByVal t As Task) As Boolean |
|
|
SyncLock (_tasks) |
|
|
Return _tasks.Remove(t) |
|
|
End SyncLock |
|
|
End Function |
|
|
|
|
|
|
|
|
Public Overrides ReadOnly Property MaximumConcurrencyLevel As Integer |
|
|
Get |
|
|
Return _maxDegreeOfParallelism |
|
|
End Get |
|
|
End Property |
|
|
|
|
|
|
|
|
Protected Overrides Function GetScheduledTasks() As IEnumerable(Of Task) |
|
|
Dim lockTaken As Boolean = False |
|
|
Try |
|
|
Monitor.TryEnter(_tasks, lockTaken) |
|
|
If (lockTaken) Then |
|
|
Return _tasks.ToArray() |
|
|
Else |
|
|
Throw New NotSupportedException() |
|
|
End If |
|
|
Finally |
|
|
If (lockTaken) Then |
|
|
Monitor.Exit(_tasks) |
|
|
End If |
|
|
End Try |
|
|
End Function |
|
|
End Class |
|
|
|
|
|
End Namespace |