Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 0.96 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. protected override void writeDebug(string message)
  2.         {
  3.             if (!shouldWrite(LogLevel.Debug))
  4.                 return;
  5.  
  6.             bool b = false;
  7.             do { gate.Enter(ref b); } while (!b);
  8.             Console.WriteLine("Debug: " + message);
  9.             gate.Exit();
  10.         }
  11.  
  12.     internal struct SilverlightSpinlock
  13.     {
  14.         long atomic;
  15.  
  16.         public void Enter(ref bool isAcquired)
  17.         {
  18.             long id = Thread.CurrentThread.ManagedThreadId;
  19.             while (Interlocked.CompareExchange(ref atomic, id, 0) != 0) { }
  20.             isAcquired = true;
  21.         }
  22.  
  23.         public void Exit()
  24.         {
  25.             long id = Thread.CurrentThread.ManagedThreadId;
  26.             long thisAtomic = Interlocked.Exchange(ref atomic, 0);
  27.             if (thisAtomic != id) {
  28.                 throw new Exception("Thread " + id + " exited a spinlock it didn't own! Owning thread was " + thisAtomic);
  29.             }
  30.         }
  31.     }