Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Threading / Tasks / ThreadPoolTaskScheduler.cs / 1305376 / ThreadPoolTaskScheduler.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// TaskScheduler.cs
//
// [....]
//
// This file contains the primary interface and management of tasks and queues.
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System;
using System.Security;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.Eventing;
namespace System.Threading.Tasks
{
///
/// An implementation of TaskScheduler that uses the ThreadPool scheduler
///
internal sealed class ThreadPoolTaskScheduler: TaskScheduler
{
///
/// Constructs a new ThreadPool task scheduler object
///
internal ThreadPoolTaskScheduler()
{
}
// static delegate for threads allocated to handle LongRunning tasks.
private static ParameterizedThreadStart s_longRunningThreadWork = new ParameterizedThreadStart(LongRunningThreadWork);
private static void LongRunningThreadWork(object obj)
{
Task t = obj as Task;
Contract.Assert(t != null, "TaskScheduler.LongRunningThreadWork: t is null");
t.ExecuteEntry(false);
}
///
/// Schedules a task to the ThreadPool.
///
/// The task to schedule.
[SecurityCritical]
protected internal override void QueueTask(Task task)
{
#if !FEATURE_PAL // PAL doesn't support eventing
if (TplEtwProvider.Log.IsEnabled(EventLevel.Verbose, ((EventKeywords)(-1))))
{
Task currentTask = Task.InternalCurrent;
Task creatingTask = task.m_parent;
TplEtwProvider.Log.TaskScheduled(this.Id, currentTask == null ? 0 : currentTask.Id,
task.Id, creatingTask == null? 0 : creatingTask.Id,
(int) task.Options);
}
#endif
if ((task.Options & TaskCreationOptions.LongRunning) != 0)
{
// Run LongRunning tasks on their own dedicated thread.
Thread thread = new Thread(s_longRunningThreadWork);
thread.IsBackground = true; // Keep this thread from blocking process shutdown
thread.Start(task);
}
else
{
// Normal handling for non-LongRunning tasks.
bool forceToGlobalQueue = ((task.Options & TaskCreationOptions.PreferFairness) != 0);
ThreadPool.UnsafeQueueCustomWorkItem(task, forceToGlobalQueue);
}
}
///
/// This internal function will do this:
/// (1) If the task had previously been queued, attempt to pop it and return false if that fails.
/// (2) Propagate the return value from Task.ExecuteEntry() back to the caller.
///
/// IMPORTANT NOTE: TryExecuteTaskInline will NOT throw task exceptions itself. Any wait code path using this function needs
/// to account for exceptions that need to be propagated, and throw themselves accordingly.
///
[SecurityCritical]
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{
// If the task was previously scheduled, and we can't pop it, then return false.
if (taskWasPreviouslyQueued && !ThreadPool.TryPopCustomWorkItem(task))
return false;
// Propagate the return value of Task.ExecuteEntry()
bool rval = false;
try
{
rval = task.ExecuteEntry(false); // calling the TaskBase override here, because it handles switching Task.Current etc.
}
finally
{
// Only call NWIP() if task was previously queued
if(taskWasPreviouslyQueued) NotifyWorkItemProgress();
}
return rval;
}
[SecurityCritical]
protected internal override bool TryDequeue(Task task)
{
// just delegate to TP
return ThreadPool.TryPopCustomWorkItem(task);
}
[SecurityCritical]
protected override IEnumerable GetScheduledTasks()
{
return FilterTasksFromWorkItems(ThreadPool.GetQueuedWorkItems());
}
private IEnumerable FilterTasksFromWorkItems(IEnumerable tpwItems)
{
foreach (IThreadPoolWorkItem tpwi in tpwItems)
{
if (tpwi is Task)
{
yield return (Task)tpwi;
}
}
}
///
/// Notifies the scheduler that work is progressing (no-op).
///
internal override void NotifyWorkItemProgress()
{
ThreadPool.NotifyWorkItemProgress();
}
///
/// This is the only scheduler that returns false for this property, indicating that the task entry codepath is unsafe (CAS free)
/// since we know that the underlying scheduler already takes care of atomic transitions from queued to non-queued.
///
internal override bool RequiresAtomicStartTransition
{
get { return false; }
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// TaskScheduler.cs
//
// [....]
//
// This file contains the primary interface and management of tasks and queues.
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System;
using System.Security;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.Eventing;
namespace System.Threading.Tasks
{
///
/// An implementation of TaskScheduler that uses the ThreadPool scheduler
///
internal sealed class ThreadPoolTaskScheduler: TaskScheduler
{
///
/// Constructs a new ThreadPool task scheduler object
///
internal ThreadPoolTaskScheduler()
{
}
// static delegate for threads allocated to handle LongRunning tasks.
private static ParameterizedThreadStart s_longRunningThreadWork = new ParameterizedThreadStart(LongRunningThreadWork);
private static void LongRunningThreadWork(object obj)
{
Task t = obj as Task;
Contract.Assert(t != null, "TaskScheduler.LongRunningThreadWork: t is null");
t.ExecuteEntry(false);
}
///
/// Schedules a task to the ThreadPool.
///
/// The task to schedule.
[SecurityCritical]
protected internal override void QueueTask(Task task)
{
#if !FEATURE_PAL // PAL doesn't support eventing
if (TplEtwProvider.Log.IsEnabled(EventLevel.Verbose, ((EventKeywords)(-1))))
{
Task currentTask = Task.InternalCurrent;
Task creatingTask = task.m_parent;
TplEtwProvider.Log.TaskScheduled(this.Id, currentTask == null ? 0 : currentTask.Id,
task.Id, creatingTask == null? 0 : creatingTask.Id,
(int) task.Options);
}
#endif
if ((task.Options & TaskCreationOptions.LongRunning) != 0)
{
// Run LongRunning tasks on their own dedicated thread.
Thread thread = new Thread(s_longRunningThreadWork);
thread.IsBackground = true; // Keep this thread from blocking process shutdown
thread.Start(task);
}
else
{
// Normal handling for non-LongRunning tasks.
bool forceToGlobalQueue = ((task.Options & TaskCreationOptions.PreferFairness) != 0);
ThreadPool.UnsafeQueueCustomWorkItem(task, forceToGlobalQueue);
}
}
///
/// This internal function will do this:
/// (1) If the task had previously been queued, attempt to pop it and return false if that fails.
/// (2) Propagate the return value from Task.ExecuteEntry() back to the caller.
///
/// IMPORTANT NOTE: TryExecuteTaskInline will NOT throw task exceptions itself. Any wait code path using this function needs
/// to account for exceptions that need to be propagated, and throw themselves accordingly.
///
[SecurityCritical]
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{
// If the task was previously scheduled, and we can't pop it, then return false.
if (taskWasPreviouslyQueued && !ThreadPool.TryPopCustomWorkItem(task))
return false;
// Propagate the return value of Task.ExecuteEntry()
bool rval = false;
try
{
rval = task.ExecuteEntry(false); // calling the TaskBase override here, because it handles switching Task.Current etc.
}
finally
{
// Only call NWIP() if task was previously queued
if(taskWasPreviouslyQueued) NotifyWorkItemProgress();
}
return rval;
}
[SecurityCritical]
protected internal override bool TryDequeue(Task task)
{
// just delegate to TP
return ThreadPool.TryPopCustomWorkItem(task);
}
[SecurityCritical]
protected override IEnumerable GetScheduledTasks()
{
return FilterTasksFromWorkItems(ThreadPool.GetQueuedWorkItems());
}
private IEnumerable FilterTasksFromWorkItems(IEnumerable tpwItems)
{
foreach (IThreadPoolWorkItem tpwi in tpwItems)
{
if (tpwi is Task)
{
yield return (Task)tpwi;
}
}
}
///
/// Notifies the scheduler that work is progressing (no-op).
///
internal override void NotifyWorkItemProgress()
{
ThreadPool.NotifyWorkItemProgress();
}
///
/// This is the only scheduler that returns false for this property, indicating that the task entry codepath is unsafe (CAS free)
/// since we know that the underlying scheduler already takes care of atomic transitions from queued to non-queued.
///
internal override bool RequiresAtomicStartTransition
{
get { return false; }
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- OleDbRowUpdatedEvent.cs
- FormViewDeleteEventArgs.cs
- ComponentChangingEvent.cs
- DataGridViewColumnTypePicker.cs
- EFColumnProvider.cs
- SqlDataReaderSmi.cs
- StackBuilderSink.cs
- AddInEnvironment.cs
- UserMapPath.cs
- ComponentGuaranteesAttribute.cs
- Matrix.cs
- HttpConfigurationContext.cs
- ProxySimple.cs
- PropertySegmentSerializer.cs
- StreamWriter.cs
- BinaryOperationBinder.cs
- URLIdentityPermission.cs
- Processor.cs
- X509Utils.cs
- EventlogProvider.cs
- TreeNodeCollectionEditor.cs
- DecoderBestFitFallback.cs
- SafeSerializationManager.cs
- XmlDownloadManager.cs
- ToolTip.cs
- ListViewItemMouseHoverEvent.cs
- WindowsPrincipal.cs
- MaterialGroup.cs
- XNodeSchemaApplier.cs
- ViewKeyConstraint.cs
- BinaryObjectInfo.cs
- ReadOnlyHierarchicalDataSourceView.cs
- HttpInputStream.cs
- SimpleTypesSurrogate.cs
- DirectoryNotFoundException.cs
- ValidationResult.cs
- ExpressionVisitorHelpers.cs
- FormsAuthenticationTicket.cs
- ToolStripMenuItemDesigner.cs
- DataTableReaderListener.cs
- Page.cs
- SessionPageStatePersister.cs
- IdleTimeoutMonitor.cs
- AutomationElementIdentifiers.cs
- ShaderEffect.cs
- FunctionImportMapping.cs
- ActivityBindForm.Designer.cs
- UpdateRecord.cs
- RowType.cs
- Table.cs
- QuaternionAnimation.cs
- XmlChildEnumerator.cs
- XamlToRtfWriter.cs
- DataTableMapping.cs
- InternalDispatchObject.cs
- SqlNamer.cs
- Helpers.cs
- PagePropertiesChangingEventArgs.cs
- DatePickerAutomationPeer.cs
- DynamicResourceExtensionConverter.cs
- XmlElementAttributes.cs
- HttpCookie.cs
- EventLogPermissionEntry.cs
- OracleConnectionFactory.cs
- nulltextnavigator.cs
- IsolationInterop.cs
- RoleManagerModule.cs
- MembershipAdapter.cs
- Delegate.cs
- DefaultMergeHelper.cs
- ComponentEditorPage.cs
- URLString.cs
- DataListItemEventArgs.cs
- ServiceModelEnumValidatorAttribute.cs
- AdRotatorDesigner.cs
- FileDialog_Vista_Interop.cs
- RequestResizeEvent.cs
- AutoSizeComboBox.cs
- WebServiceClientProxyGenerator.cs
- DataGridViewCellFormattingEventArgs.cs
- Int32Rect.cs
- SelectedGridItemChangedEvent.cs
- EnumConverter.cs
- Fonts.cs
- DictionaryContent.cs
- ParameterRetriever.cs
- HttpCacheVary.cs
- EventSetter.cs
- Triplet.cs
- ListItemCollection.cs
- ListCollectionView.cs
- DataPagerFieldItem.cs
- AuthorizationRuleCollection.cs
- BaseCAMarshaler.cs
- WindowsListViewScroll.cs
- AttachedPropertyInfo.cs
- ReaderWriterLock.cs
- OrderingInfo.cs
- InvalidWMPVersionException.cs
- DataBindingHandlerAttribute.cs