Thread pool task manager process work item

For an overview of the thread pool task manager please see: Thread pool task manager: overview.

Thread worker process work item sequence

The thread worker loops continuously, processing work items as depicted in the following sequence diagram:

mtf-runtime-threadworker-procwork.png

When the thread worker is processing, there are three possible outcomes:
  1. There is work in its own queue - take an item and process it
  2. No work in own queue but work in another - steal an item from another thread's queue and process it
  3. No work in any queue - block on the work semaphore until an item becomes available

Work available in own queue

When there is work available in the thread's own queue, the following steps are executed:
  1. Lock() the work lock to gain exclusive access to the work queue
  2. Decrement the work item count
  3. Dequeue() the next work item
  4. Unlock() the work lock to allow other threads to access the work queue
  5. Execute the work item's callback method

No work in own queue, work available in another queue

When there is no work in the thread's own queue but work is available in another queue, the following steps are executed:
  1. Lock() the work lock to gain exclusive access to the work queue
  2. StealWorkItem() from another queue (this method is detailed separately)
  3. Unlock() the work lock to allow other threads to access the work queue
  4. Execute the work item's callback method

No work available in any queue

When there is no work available in any queue, the following steps are executed:
  1. Lock() the work lock to gain exclusive access to the work queue
  2. Increment number of threads blocked waiting for work
  3. Unlock() the work lock to allow other threads to access the work queue
  4. Block on the work semaphore until an item becomes available

Thread pool task manager steal work item sequence

The following sequence diagram shows how the task manager selects the best queue to steal work from. The basic principle is that work should be stolen from the busiest queue (i.e. the one with most items) that is in the same pool as the thread that is looking to steal work. Note that the detail of stealing a work item from within a specific queue is covered separately.

mtf-runtime-taskman-stealwork.png

Given that the task manager is initialized, stealing a work item consists of the following steps:
  1. GetBusiestQueueIndex() specifying whether to use the GUI pool or the processing pool
  2. Select the pool of workers based on the isGui flag
  3. If the queue within the pool is not empty, call its StealWorkItem() method to steal from it
  4. Return the stolen work item or null if no item could be stolen

Thread worker steal work item sequence

A work item is stolen from a particular thread worker as shown in the following sequence diagram:

mtf-runtime-threadworker-stealwork.png

Stealing a work item from a particular thread worker consists of the following steps:
  1. Lock() the work lock to gain exclusive access to the work queue
  2. Check that there is at least one work item in the queue
  3. Decrement the number of work items
  4. Dequeue() a work item from the front of the queue
  5. Unlock() the work lock to allow other threads to access the work queue
  6. Return the stolen work item or null if no item was available

Last edited Aug 30, 2012 at 10:09 AM by jaorme, version 14

Comments

No comments yet.