public void Enter(ref bool lockTaken)
{
if (lockTaken)
throw new ArgumentException ("lockTaken", "lockTaken must be initialized to false");
if (isThreadOwnerTrackingEnabled && IsHeldByCurrentThread)
throw new LockRecursionException ();
int slot = -1;
RuntimeHelpers.PrepareConstrainedRegions ();
try {
slot = ClientInterlocked.Increment (ref ticket.Users) - 1;
ClientSpinWait wait = new ClientSpinWait ();
while (slot != ticket.Value) {
wait.SpinOnce ();
while (stallTickets != null && ((ClientConcurrentOrderedList<int>)stallTickets).TryRemove(ticket.Value))
++ticket.Value;
}
} finally {
if (slot == ticket.Value) {
lockTaken = true;
threadWhoTookLock = Thread.CurrentThread.ManagedThreadId;
} else if (slot != -1) {
// We have been interrupted, initialize stallTickets
if (stallTickets == null)
ClientInterlocked.CompareExchange(ref stallTickets, new ClientConcurrentOrderedList<int>(), null);
((ClientConcurrentOrderedList<int>)stallTickets).TryAdd (slot);
}
}
}