Drakim

Untitled

Jun 24th, 2016
521
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         public inline function borrow() {
  2.  
  3.             // If we can't set busyFlag then we have to wait for it to be freed
  4.             if(!Memory.safeCompareExchange(busyFlag, 0, 1)) {
  5.  
  6.                 // Increase the busyQueue by one and use this as our queue number
  7.                 var qnr = Memory.safeAdd(busyQueue, 1) + 1;
  8.  
  9.                 // If our queue number is over 100 we warp around
  10.                 if(qnr > 100) {
  11.                     // Attempt to set the queue number back by 100. This might fail due to a race condition if another thread
  12.                     // has advanced the busyQueue right after us. If that is the case, it's up to that thread to adjust it back
  13.                     Memory.safeCompareExchange(busyQueue, qnr, qnr-100);
  14.  
  15.                     // Either way, our queue number is warped around
  16.                     qnr -= 100;
  17.                 }
  18.  
  19.                 // Wait until we are woken with busyQueueAction set to our queue number
  20.                 while( !Memory.wait(busyQueueAction, qnr) ) { }
  21.  
  22.             }
  23.  
  24.         }
  25.  
  26.         public inline function release() {
  27.  
  28.             // Advance the busyQueueAction by one
  29.             var ticket = Memory.safeAdd(busyQueueAction, 1) + 1;
  30.  
  31.             // If the ticket is over 100 we warp around
  32.             if(ticket > 100) { Memory.safeSub(busyQueueAction, 100); }
  33.  
  34.             // Wake all the waiting threads
  35.             var woken = Memory.wake(busyQueueAction);
  36.  
  37.             // If nobody is listening, free up the busyflag
  38.             if(woken == 0) {Memory.safeWrite(busyFlag, 0);}
  39.  
  40.         }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×