Last active
August 29, 2015 14:02
-
-
Save renestein/0d69c64270276231e1a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int runTasksCore(CancellationToken cancellationToken) | |
{ | |
bool searchForTask = true; | |
var usedCancellationToken = cancellationToken; | |
var serviceData = m_isServiceThreadFlags.Value; | |
while (searchForTask) | |
{ | |
searchForTask = false; | |
m_stopCancelTokenSource.Token.ThrowIfCancellationRequested(); | |
try | |
{ | |
Task task; | |
if (!tryGetTask(usedCancellationToken, out task)) | |
{ | |
continue; | |
} | |
m_stopCancelTokenSource.Token.ThrowIfCancellationRequested(); | |
searchForTask = TryExecuteTaskInline(task, true) && !tasksLimitReached(); | |
m_stopCancelTokenSource.Token.ThrowIfCancellationRequested(); | |
} | |
catch (OperationCanceledException e) | |
{ | |
Trace.WriteLine(e); | |
if (m_stopCancelTokenSource.IsCancellationRequested) | |
{ | |
break; | |
} | |
usedCancellationToken = CancellationToken.None; | |
searchForTask = !tasksLimitReached(); | |
} | |
} | |
return serviceData.ExecutedOperationsCount; | |
} | |
private bool tryGetTask(CancellationToken cancellationToken, out Task task) | |
{ | |
if (cancellationToken != CancellationToken.None) | |
{ | |
return m_tasks.TryTake(out task, Timeout.Infinite, cancellationToken); | |
} | |
return m_tasks.TryTake(out task); | |
} | |
private bool tasksLimitReached() | |
{ | |
var serviceData = m_isServiceThreadFlags.Value; | |
if ((serviceData.MaxOperationsAllowed == UNLIMITED_MAX_TASKS) || | |
(serviceData.ExecutedOperationsCount < serviceData.MaxOperationsAllowed)) | |
{ | |
return false; | |
} | |
return true; | |
} | |
public override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) | |
{ | |
checkIfDisposed(); | |
if (!isInServiceThread()) | |
{ | |
return false; | |
} | |
if (tasksLimitReached()) | |
{ | |
return false; | |
} | |
bool taskExecutedNow = false; | |
try | |
{ | |
m_isServiceThreadFlags.Value.ExecutedOperationsCount++; | |
taskExecutedNow = task.RunOnProxyScheduler(); | |
} | |
finally | |
{ | |
if (!taskExecutedNow) | |
{ | |
m_isServiceThreadFlags.Value.ExecutedOperationsCount--; | |
} | |
} | |
return taskExecutedNow; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment