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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
// ****************************************************************************
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// CONTENTS
// Value-add wrapper on top of standard CLR monitor
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Xml;
using System.Reflection;
using System.Threading;
using System.Diagnostics;
using System.Transactions;
using System.Workflow.Runtime.Hosting;
namespace System.Workflow.Runtime
{
internal static class LockFactory
{
internal static InstanceLock CreateWorkflowExecutorLock(Guid id)
{
return new InstanceLock(id, "Workflow Executor Lock: " + id.ToString(), 50, LockPriorityOperator.GreaterThanOrReentrant);
}
internal static InstanceLock CreateWorkflowSchedulerLock(Guid id)
{
return new InstanceLock(id, "Workflow Scheduler Lock: " + id.ToString(), 40, LockPriorityOperator.GreaterThan);
}
internal static InstanceLock CreateWorkflowMessageDeliveryLock(Guid id)
{
return new InstanceLock(id, "Workflow Message Delivery Lock: " + id.ToString(), 35, LockPriorityOperator.GreaterThanOrReentrant);
}
}
internal enum LockPriorityOperator
{
GreaterThan,
GreaterThanOrReentrant,
}
internal sealed class InstanceLock
{
#region Static Data/Methods
[ThreadStaticAttribute()]
private static List<InstanceLock> t_heldLocks = null;
[Conditional("DEBUG")]
internal static void AssertNoLocksHeld()
{
#if DEBUG
System.Diagnostics.Debug.Assert(HeldLocks.Count == 0, "No locks should be held.");
#endif
}
[Conditional("DEBUG")]
internal static void AssertIsLocked(InstanceLock theLock)
{
#if DEBUG
System.Diagnostics.Debug.Assert(HeldLocks.Contains(theLock), "Lock should be held.");
#endif
}
private static List<InstanceLock> HeldLocks
{
get
{
List<InstanceLock> tLocks = InstanceLock.t_heldLocks;
if (tLocks == null)
{
InstanceLock.t_heldLocks = new List<InstanceLock>();
tLocks = InstanceLock.t_heldLocks;
}
return tLocks;
}
}
#endregion Static Data/Methods
private Guid m_instanceId;
private String m_name;
private int m_priority;
private LockPriorityOperator m_operator;
internal int Priority
{
get
{
return this.m_priority;
}
}
internal LockPriorityOperator Operator
{
get
{
return this.m_operator;
}
}
internal InstanceLock(Guid id, String name, int priority, LockPriorityOperator lockOperator)
{
this.m_instanceId = id;
this.m_name = name;
this.m_priority = priority;
this.m_operator = lockOperator;
}
internal Guid InstanceId
{
get
{
return this.m_instanceId;
}
}
internal InstanceLockGuard Enter()
{
return new InstanceLockGuard(this);
}
internal bool TryEnter()
{
InstanceLockGuard.EnforceGuard(this);
bool lockHeld = false;
bool success = false;
try
{
Monitor.TryEnter(this, ref lockHeld);
if (lockHeld)
{
HeldLocks.Add(this);
success = true;
}
}
finally
{
if (lockHeld && !success)
{
Monitor.Exit(this);
}
}
return success;
}
internal void Exit()
{
try
{
HeldLocks.Remove(this);
}
finally
{
Monitor.Exit(this);
}
}
internal struct InstanceLockGuard : IDisposable
{
readonly InstanceLock m_lock;
internal static void EnforceGuard(InstanceLock theLock)
{
foreach (InstanceLock heldLock in HeldLocks)
{
switch (theLock.Operator)
{
case LockPriorityOperator.GreaterThan:
if (heldLock.InstanceId == theLock.InstanceId && heldLock.Priority <= theLock.Priority)
throw new InvalidOperationException(ExecutionStringManager.InstanceOperationNotValidinWorkflowThread);
break;
case LockPriorityOperator.GreaterThanOrReentrant:
// the checks here assume that locks have unique priorities
if (heldLock.InstanceId == theLock.InstanceId && heldLock.Priority < theLock.Priority)
throw new InvalidOperationException(ExecutionStringManager.InstanceOperationNotValidinWorkflowThread);
break;
default:
System.Diagnostics.Debug.Assert(false, "Unrecognized lock operator");
break;
}
}
}
internal InstanceLockGuard(InstanceLock theLock)
{
this.m_lock = theLock;
// Note: the following operations are logically atomic, but since the
// list we are using is thread local there is no need to take a lock.
EnforceGuard(theLock);
try
{
}
finally
{
bool success = false;
#pragma warning disable 0618
//@
Monitor.Enter(this.m_lock);
#pragma warning restore 0618
try
{
HeldLocks.Add(this.m_lock);
success = true;
}
finally
{
if (!success)
{
Monitor.Exit(this.m_lock);
}
}
}
}
internal void Pulse()
{
Monitor.Pulse(this.m_lock);
}
internal void Wait()
{
Monitor.Wait(this.m_lock);
}
public void Dispose()
{
// Note: the following operations are logically atomic, but since the
// list we are using is thread local there is no need to take a lock.
try
{
HeldLocks.Remove(this.m_lock);
}
finally
{
Monitor.Exit(this.m_lock);
}
}
}
}
internal sealed class SchedulerLockGuard : IDisposable
{
private InstanceLock.InstanceLockGuard lg;
private WorkflowExecutor workflowExec;
internal SchedulerLockGuard(InstanceLock il, WorkflowExecutor w)
{
lg = il.Enter();
workflowExec = w;
}
private static void FireEvents(List<SchedulerLockGuardInfo> eventList, WorkflowExecutor workflowExec)
{
if (!workflowExec.IsInstanceValid && (workflowExec.WorkflowStatus == WorkflowStatus.Completed || workflowExec.WorkflowStatus == WorkflowStatus.Terminated))
{
// The workflow is dead, let the instance have a hard ref to the corpse for support of the query apis.
workflowExec.WorkflowInstance.DeadWorkflow = workflowExec;
}
for (int i = 0; i < eventList.Count; i++)
{
SchedulerLockGuardInfo eseg = eventList[i];
// eseg.EventInfo is non-null only if the event type is Suspended or Terminated
// If the event type is Suspended, then call FireWorkflowSuspended after casting
// the event argument to a String.
// If the event type is Terminated, then call FireWorkflowTerminated after casting
// the event argument to either a String or an Exception.
switch (eseg.EventType)
{
case WorkflowEventInternal.Suspended:
workflowExec.FireWorkflowSuspended((String)eseg.EventInfo);
break;
case WorkflowEventInternal.Terminated:
if ((eseg.EventInfo as System.Exception) != null)
{
workflowExec.FireWorkflowTerminated((Exception)eseg.EventInfo);
}
else
{
workflowExec.FireWorkflowTerminated((String)eseg.EventInfo);
}
break;
default:
workflowExec.FireWorkflowExecutionEvent(eseg.Sender, eseg.EventType);
break;
}
}
}
internal static void Exit(InstanceLock il, WorkflowExecutor w)
{
List<SchedulerLockGuardInfo> eventList = new List<SchedulerLockGuardInfo>(w.EventsToFireList);
w.EventsToFireList.Clear();
il.Exit();
FireEvents(eventList, w);
}
public void Dispose()
{
List<SchedulerLockGuardInfo> eventList = new List<SchedulerLockGuardInfo>(workflowExec.EventsToFireList);
workflowExec.EventsToFireList.Clear();
lg.Dispose();
FireEvents(eventList, workflowExec);
}
}
internal sealed class SchedulerLockGuardInfo
{
private Object sender;
private WorkflowEventInternal eventType;
private object eventInfo;
internal SchedulerLockGuardInfo(Object _sender, WorkflowEventInternal _eventType)
{
sender = _sender;
eventType = _eventType;
eventInfo = null;
}
internal SchedulerLockGuardInfo(Object _sender, WorkflowEventInternal _eventType, object _eventInfo)
: this(_sender, _eventType)
{
eventInfo = _eventInfo;
}
internal Object Sender
{
get
{
return sender;
}
}
internal WorkflowEventInternal EventType
{
get
{
return eventType;
}
}
internal Object EventInfo
{
get
{
return eventInfo;
}
}
}
}
|