Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Dispatcher / ConcurrencyBehavior.cs / 1 / ConcurrencyBehavior.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.ServiceModel.Dispatcher { using System; using System.ServiceModel; using System.ServiceModel.Channels; using System.Threading; using System.Collections.Generic; class ConcurrencyBehavior { ConcurrencyMode mode; bool supportsTransactedBatch; internal ConcurrencyBehavior(DispatchRuntime runtime) { this.mode = runtime.ConcurrencyMode; this.supportsTransactedBatch = ConcurrencyBehavior.SupportsTransactedBatch(runtime.ChannelDispatcher); } static bool SupportsTransactedBatch(ChannelDispatcher channelDispatcher) { return channelDispatcher.IsTransactedReceive && (channelDispatcher.MaxTransactedBatchSize > 0); } internal bool IsConcurrent(ref MessageRpc rpc) { return IsConcurrent(this.mode, rpc.Channel.HasSession, this.supportsTransactedBatch); } internal static bool IsConcurrent(ConcurrencyMode mode, bool hasSession, bool supportsTransactedBatch) { return !supportsTransactedBatch && ((mode != ConcurrencyMode.Single) || !hasSession); } internal static bool IsConcurrent(ChannelDispatcher runtime, bool hasSession) { if (ConcurrencyBehavior.SupportsTransactedBatch(runtime)) { return false; } if (!hasSession) { return true; } foreach (EndpointDispatcher endpointDispatcher in runtime.Endpoints) { if (endpointDispatcher.DispatchRuntime.ConcurrencyMode != ConcurrencyMode.Single) { return true; } } return false; } internal void LockInstance(ref MessageRpc rpc) { if (this.mode != ConcurrencyMode.Multiple) { ConcurrencyInstanceContextFacet resource = rpc.InstanceContext.Concurrency; lock (rpc.InstanceContext.ThisLock) { if (!resource.Locked) { resource.Locked = true; } else { MessageRpcWaiter waiter = new MessageRpcWaiter(rpc.Pause()); resource.EnqueueNewMessage(waiter); } } if (this.mode == ConcurrencyMode.Reentrant) { rpc.OperationContext.IsServiceReentrant = true; } } } internal void UnlockInstance(ref MessageRpc rpc) { if (this.mode != ConcurrencyMode.Multiple) { ConcurrencyBehavior.UnlockInstance(rpc.InstanceContext); } } internal static void UnlockInstanceBeforeCallout(OperationContext operationContext) { if (operationContext != null && operationContext.IsServiceReentrant) { ConcurrencyBehavior.UnlockInstance(operationContext.InstanceContext); } } static void UnlockInstance(InstanceContext instanceContext) { ConcurrencyInstanceContextFacet resource = instanceContext.Concurrency; lock (instanceContext.ThisLock) { if (resource.HasWaiters) { IWaiter nextWaiter = resource.DequeueWaiter(); nextWaiter.Signal(); } else { //We have no pending Callouts and no new Messages to process resource.Locked = false; } } } internal static void LockInstanceAfterCallout(OperationContext operationContext) { if (operationContext != null) { InstanceContext instanceContext = operationContext.InstanceContext; if (operationContext.IsServiceReentrant) { ConcurrencyInstanceContextFacet resource = instanceContext.Concurrency; ThreadWaiter waiter = null; lock (instanceContext.ThisLock) { if (!resource.Locked) { resource.Locked = true; } else { waiter = new ThreadWaiter(); resource.EnqueueCalloutMessage(waiter); } } if (waiter != null) { waiter.Wait(); } } } } internal interface IWaiter { void Signal(); } class MessageRpcWaiter : IWaiter { IResumeMessageRpc resume; internal MessageRpcWaiter(IResumeMessageRpc resume) { this.resume = resume; } void IWaiter.Signal() { try { bool alreadyResumedNoLock; this.resume.Resume(out alreadyResumedNoLock); if (alreadyResumedNoLock) { DiagnosticUtility.DebugAssert("ConcurrencyBehavior resumed more than once for same call."); } } catch (Exception e) { if (DiagnosticUtility.IsFatal(e)) { throw; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); } } } class ThreadWaiter : IWaiter { ManualResetEvent wait = new ManualResetEvent(false); void IWaiter.Signal() { this.wait.Set(); } internal void Wait() { this.wait.WaitOne(); this.wait.Close(); } } } internal class ConcurrencyInstanceContextFacet { internal bool Locked; QueuecalloutMessageQueue; Queue newMessageQueue; internal bool HasWaiters { get { return (((this.calloutMessageQueue != null) && (this.calloutMessageQueue.Count > 0)) || ((this.newMessageQueue != null) && (this.newMessageQueue.Count > 0))); } } ConcurrencyBehavior.IWaiter DequeueFrom(Queue queue) { ConcurrencyBehavior.IWaiter waiter = queue.Dequeue(); if (queue.Count == 0) { queue.TrimExcess(); } return waiter; } internal ConcurrencyBehavior.IWaiter DequeueWaiter() { // Finishing old work takes precedence over new work. if ((this.calloutMessageQueue != null) && (this.calloutMessageQueue.Count > 0)) { return this.DequeueFrom(this.calloutMessageQueue); } else { return this.DequeueFrom(this.newMessageQueue); } } internal void EnqueueNewMessage(ConcurrencyBehavior.IWaiter waiter) { if (this.newMessageQueue == null) this.newMessageQueue = new Queue (); this.newMessageQueue.Enqueue(waiter); } internal void EnqueueCalloutMessage(ConcurrencyBehavior.IWaiter waiter) { if (this.calloutMessageQueue == null) this.calloutMessageQueue = new Queue (); this.calloutMessageQueue.Enqueue(waiter); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SettingsPropertyNotFoundException.cs
- WebContext.cs
- XmlElementCollection.cs
- AffineTransform3D.cs
- RegionInfo.cs
- DurableEnlistmentState.cs
- Hyperlink.cs
- ScrollableControlDesigner.cs
- SqlCommandBuilder.cs
- BypassElementCollection.cs
- MinMaxParagraphWidth.cs
- RequestTimeoutManager.cs
- MarginsConverter.cs
- SinglePageViewer.cs
- XmlSchemaComplexContentExtension.cs
- ConfigUtil.cs
- Error.cs
- PkcsMisc.cs
- OracleRowUpdatingEventArgs.cs
- CfgParser.cs
- Trace.cs
- LinkConverter.cs
- TypedTableBaseExtensions.cs
- QueryRewriter.cs
- ConsoleCancelEventArgs.cs
- MailWriter.cs
- HostDesigntimeLicenseContext.cs
- GeometryHitTestResult.cs
- ServicesSection.cs
- ValueChangedEventManager.cs
- NameSpaceExtractor.cs
- PropertyGeneratedEventArgs.cs
- ContainerAction.cs
- EnumType.cs
- GenerateDerivedKeyRequest.cs
- WinFormsSpinner.cs
- StoreItemCollection.Loader.cs
- CompilerScopeManager.cs
- ColorConvertedBitmap.cs
- QueuePathEditor.cs
- LocatorGroup.cs
- SimpleType.cs
- Processor.cs
- TrustLevelCollection.cs
- ModelTreeEnumerator.cs
- _HelperAsyncResults.cs
- AnimationException.cs
- DataGridViewDataErrorEventArgs.cs
- CatchBlock.cs
- COM2PropertyDescriptor.cs
- ApplicationFileCodeDomTreeGenerator.cs
- QueryOutputWriter.cs
- MemberDescriptor.cs
- TextBoxAutoCompleteSourceConverter.cs
- OdbcDataAdapter.cs
- PackWebResponse.cs
- Listen.cs
- XmlAnyElementAttributes.cs
- TimeoutException.cs
- TableLayoutPanelCodeDomSerializer.cs
- DataRowChangeEvent.cs
- SequenceRange.cs
- CodeSnippetCompileUnit.cs
- AdvancedBindingEditor.cs
- Int32KeyFrameCollection.cs
- TypeLibConverter.cs
- RtfControlWordInfo.cs
- BinaryObjectInfo.cs
- BamlResourceSerializer.cs
- ByteConverter.cs
- TaiwanLunisolarCalendar.cs
- SettingsPropertyIsReadOnlyException.cs
- ExpressionHelper.cs
- GeneralTransform3D.cs
- XMLSchema.cs
- FrameworkContentElement.cs
- SqlCacheDependencyDatabase.cs
- TcpServerChannel.cs
- XPathNavigatorException.cs
- UnionCodeGroup.cs
- DPAPIProtectedConfigurationProvider.cs
- ParameterBuilder.cs
- DesignTimeValidationFeature.cs
- ListSurrogate.cs
- Border.cs
- SrgsGrammarCompiler.cs
- DateTimeFormatInfo.cs
- ModifierKeysConverter.cs
- VirtualizingPanel.cs
- AffineTransform3D.cs
- SplitterCancelEvent.cs
- LineServices.cs
- TextSelection.cs
- BigInt.cs
- AuthenticationModuleElementCollection.cs
- StylusSystemGestureEventArgs.cs
- PreservationFileWriter.cs
- XmlLanguage.cs
- CellPartitioner.cs
- EdmItemCollection.cs