
Untitled
By: a guest on
Jun 22nd, 2012 | syntax:
None | size: 0.96 KB | hits: 9 | expires: Never
protected override void writeDebug(string message)
{
if (!shouldWrite(LogLevel.Debug))
return;
bool b = false;
do { gate.Enter(ref b); } while (!b);
Console.WriteLine("Debug: " + message);
gate.Exit();
}
internal struct SilverlightSpinlock
{
long atomic;
public void Enter(ref bool isAcquired)
{
long id = Thread.CurrentThread.ManagedThreadId;
while (Interlocked.CompareExchange(ref atomic, id, 0) != 0) { }
isAcquired = true;
}
public void Exit()
{
long id = Thread.CurrentThread.ManagedThreadId;
long thisAtomic = Interlocked.Exchange(ref atomic, 0);
if (thisAtomic != id) {
throw new Exception("Thread " + id + " exited a spinlock it didn't own! Owning thread was " + thisAtomic);
}
}
}