1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System;
using System.Runtime;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Threading;
class ThreadBehavior
{
SendOrPostCallback threadAffinityStartCallback;
SendOrPostCallback threadAffinityEndCallback;
static Action<object> cleanThreadCallback;
readonly SynchronizationContext context;
internal ThreadBehavior(DispatchRuntime dispatch)
{
this.context = dispatch.SynchronizationContext;
}
SendOrPostCallback ThreadAffinityStartCallbackDelegate
{
get
{
if (this.threadAffinityStartCallback == null)
{
this.threadAffinityStartCallback = new SendOrPostCallback(this.SynchronizationContextStartCallback);
}
return this.threadAffinityStartCallback;
}
}
SendOrPostCallback ThreadAffinityEndCallbackDelegate
{
get
{
if (this.threadAffinityEndCallback == null)
{
this.threadAffinityEndCallback = new SendOrPostCallback(this.SynchronizationContextEndCallback);
}
return this.threadAffinityEndCallback;
}
}
static Action<object> CleanThreadCallbackDelegate
{
get
{
if (ThreadBehavior.cleanThreadCallback == null)
{
ThreadBehavior.cleanThreadCallback = new Action<object>(ThreadBehavior.CleanThreadCallback);
}
return ThreadBehavior.cleanThreadCallback;
}
}
internal void BindThread(ref MessageRpc rpc)
{
this.BindCore(ref rpc, true);
}
internal void BindEndThread(ref MessageRpc rpc)
{
this.BindCore(ref rpc, false);
}
void BindCore(ref MessageRpc rpc, bool startOperation)
{
SynchronizationContext syncContext = GetSyncContext(rpc.InstanceContext);
if (syncContext != null)
{
IResumeMessageRpc resume = rpc.Pause();
if (startOperation)
{
syncContext.OperationStarted();
syncContext.Post(this.ThreadAffinityStartCallbackDelegate, resume);
}
else
{
syncContext.Post(this.ThreadAffinityEndCallbackDelegate, resume);
}
}
else if (rpc.SwitchedThreads)
{
IResumeMessageRpc resume = rpc.Pause();
ActionItem.Schedule(ThreadBehavior.CleanThreadCallbackDelegate, resume);
}
}
SynchronizationContext GetSyncContext(InstanceContext instanceContext)
{
Fx.Assert(instanceContext != null, "instanceContext is null !");
SynchronizationContext syncContext = instanceContext.SynchronizationContext ?? this.context;
return syncContext;
}
void SynchronizationContextStartCallback(object state)
{
ResumeProcessing((IResumeMessageRpc)state);
}
void SynchronizationContextEndCallback(object state)
{
IResumeMessageRpc resume = (IResumeMessageRpc)state;
ResumeProcessing(resume);
SynchronizationContext syncContext = GetSyncContext(resume.GetMessageInstanceContext());
Fx.Assert(syncContext != null, "syncContext is null !?");
syncContext.OperationCompleted();
}
void ResumeProcessing(IResumeMessageRpc resume)
{
bool alreadyResumedNoLock;
resume.Resume(out alreadyResumedNoLock);
if (alreadyResumedNoLock)
{
string text = SR.GetString(SR.SFxMultipleCallbackFromSynchronizationContext, context.GetType().ToString());
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(text));
}
}
static void CleanThreadCallback(object state)
{
bool alreadyResumedNoLock;
((IResumeMessageRpc)state).Resume(out alreadyResumedNoLock);
if (alreadyResumedNoLock)
{
Fx.Assert("IOThreadScheduler called back twice");
}
}
internal static SynchronizationContext GetCurrentSynchronizationContext()
{
if (AspNetEnvironment.IsApplicationDomainHosted())
{
return null;
}
return SynchronizationContext.Current;
}
}
}
|