Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Concurrency Runtime (CRT): The Task Scheduler


Dispatching Tasks

The Scheduler tries to search for work to execute. "Work" can be:

  • Unblocked context
  • Lightweight tasks
  • Tasks in work-stealing queues

Again, all these tasks are stored in queues managed by Schedule groups, and each group is managed by a scheduling ring.

When a virtual processor is allocated to the scheduler, a ThreadProxy class is created and associated to this processor. After the creation the Dispatch method of the ThreadProxy is invoked.

As shown in Figure 18, the CRT uses abstract classes to enforces low coupling, and the real dispatch invoked depends of the implementation chosen by the runtime -- this selection is made according to the scheduler policy.

[Click image to view at full size]
Figure 18

The concrete implementation of Dispatch invokes the Dispatch method of the execution context.

Figure 19 shows the methods invoked by concrete implementation of Context::Dispatch method:

[Click image to view at full size]
Figure 19

So the algorithm for searching for the next task to execute is implemented by the WorkSearchContext class. Let's uncover all the classes used directly by WorkSearchContext to achieve its goal:

[Click image to view at full size]
Figure 20

The responsibility of WorkSearchContext is to give us a WorkItem to execute; it could be InternalContextBase, RealizedChore, or _UnrealizedChore. To understand the collaboration between these classes, let's search for methods directly used by WorkSearchContext:

[Click image to view at full size]
Figure 21

The WorkSearchContext iterates on the SchedulingRing and ScheduleGroup classes via SchedulerBase methods. And for each ScheduleBase, you search for RunnableContext, realizedChore, or UnrealizedChore.

The WorkSearchContext class is created by the VirtualProcessor class, and as shown in Figure 22, the algorithm used is specified when the VirtualProcessor is initialized, and it asks the Scheduler for the SchedulingProtocol which describes the scheduling algorithm that will be used for the Scheduler.

[Click image to view at full size]
Figure 22

WorkSearchContext will discover the algorithm to use by passing it a value from Algorithm enum.

[Click image to view at full size]
Figure 23

So two algorithms are implemented by this class to find a task:

Cache Local algorithm. This algorithm will look for a runnable context within the current schedule group, then realized chores and unrealized chores. If there's no more work in the current schedule group, it looks for the next group in the same schedule ring. And when it finishes all works in the current schedule ring, it looks in the next schedule ring.

So the scheduler prefers to continue to work on tasks within the current schedule group before moving to another schedule group.

This algorithm is implemented by the WorkSearchContext::SearchCacheLocal method, and as shown by this dependency graph, this method search invoke other methods to search for runnable contexts, RealizedChore or _UnrealizedChore.

[Click image to view at full size]
Figure 24

Another specificity of this algorithm is that the unblocked contexts are cached per virtual-processor and are typically scheduled in a last-in first-out (LIFO) fashion by the virtual processor that unblocked them. To verify this behavior, here's a dependency graph of methods invoked when searching for runnable context:

[Click image to view at full size]
Figure 25

This algorithm is the default algorithm used by the Scheduler if one is not specified..

Fair algorithm. In this case the scheduler prefers to round-robin through schedule groups after executing each task. Unblocked contexts are typically scheduled in a first-in-first-out (FIFO) fashion. Virtual processors do not cache unblocked contexts.

This algorithm is implemented by the WorkSearchContext::SearchFair method, and as shown by this dependency graph, this method search invokes other methods to search for runnable contexts, RealizedChore or _UnrealizedChore.

[Click image to view at full size]
Figure 26

From this analysis, it is fair to say that the CRT Task Scheduler appears to be well-designed, is characterized by a high cohesion and very low coupling, is very flexible, and provides by default an optimized algorithm to exploit better hardware resources.


Lahlali Issam is the developer of CppDepend code analysis tool for C/C++.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.