none
TaskScheduler task inlining – errors due to re-entrancy RRS feed

  • Question

  • I could not find out how TaskScheduler task inlining prevent errors due to re-entrancy – “To prevent errors due to re-entrancy, task inlining only occurs when the wait target is found in the relevant thread's local queue.” (https://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.aspx).

    How can processing task only from local queue can prevent re-entrancy?

    Friday, April 7, 2017 8:37 AM

Answers

  • Hi User919,

    I think waiting target is a WaitHandle instance on which a Thread is blocked. So if a WaitHandle instance is found in a lcoal queue of a waiting Thread - it would be automatically considered as inlined Task's local queue item.

    >>How can processing task only from local queue can prevent re-entrancy?

    To do that, Wait asks for assistance from the target scheduler, by making a call to the TaskScheduler’s TryExecuteTaskInline method.  TryExecuteTaskInline can do whatever logic the scheduler needs in order to validate that the current context is acceptable for inlining the Task.  If it is, the scheduler tries to execute the task then and there as part of the call to TryExecuteTaskInline (using the base TaskScheduler’s TryExecuteTask method) and returns whether the Task could be executed.  If it couldn’t be executed in TryExecuteTaskInline, the scheduler returns false, and Wait will need to block until the Task completes.

    For more information, please refer to:

    https://blogs.msdn.microsoft.com/pfxteam/2009/10/15/task-wait-and-inlining/

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 10, 2017 5:23 AM
    Moderator

All replies

  • Hi User919,

    I think waiting target is a WaitHandle instance on which a Thread is blocked. So if a WaitHandle instance is found in a lcoal queue of a waiting Thread - it would be automatically considered as inlined Task's local queue item.

    >>How can processing task only from local queue can prevent re-entrancy?

    To do that, Wait asks for assistance from the target scheduler, by making a call to the TaskScheduler’s TryExecuteTaskInline method.  TryExecuteTaskInline can do whatever logic the scheduler needs in order to validate that the current context is acceptable for inlining the Task.  If it is, the scheduler tries to execute the task then and there as part of the call to TryExecuteTaskInline (using the base TaskScheduler’s TryExecuteTask method) and returns whether the Task could be executed.  If it couldn’t be executed in TryExecuteTaskInline, the scheduler returns false, and Wait will need to block until the Task completes.

    For more information, please refer to:

    https://blogs.msdn.microsoft.com/pfxteam/2009/10/15/task-wait-and-inlining/

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 10, 2017 5:23 AM
    Moderator
  • Hi User919,

    I think waiting target is a WaitHandle instance on which a Thread is blocked. So if a WaitHandle instance is found in a lcoal queue of a waiting Thread - it would be automatically considered as inlined Task's local queue item.

    >>How can processing task only from local queue can prevent re-entrancy?

    To do that, Wait asks for assistance from the target scheduler, by making a call to the TaskScheduler’s TryExecuteTaskInline method.  TryExecuteTaskInline can do whatever logic the scheduler needs in order to validate that the current context is acceptable for inlining the Task.  If it is, the scheduler tries to execute the task then and there as part of the call to TryExecuteTaskInline (using the base TaskScheduler’s TryExecuteTask method) and returns whether the Task could be executed.  If it couldn’t be executed in TryExecuteTaskInline, the scheduler returns false, and Wait will need to block until the Task completes.

    For more information, please refer to:

    https://blogs.msdn.microsoft.com/pfxteam/2009/10/15/task-wait-and-inlining/

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Hi,

    based on you reply I’ve found an article at https://blogs.msdn.microsoft.com/pfxteam/2009/10/15/task-wait-and-inlining/ explaining all details.

    Thanks.

    U
    Saturday, December 16, 2017 5:00 PM