Advertisement
Guest User

Untitled

a guest
Apr 29th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.92 KB | None | 0 0
  1. module system.lock;
  2.  
  3. // Itinerations of a spinlock necessary to call it a deadlock
  4. immutable ulong deadlockItinerations = 35000000;
  5.  
  6. // The lock information, 1 if locked, 0 if not
  7. alias Lock = ubyte;
  8.  
  9. // Test a lock, if its locked, wait for it to unlock, if not lock it.
  10. void adquireLock(Lock* lock) {
  11.     import core.bitop;
  12.     import util.term;
  13.  
  14.     if (volatileLoad(lock)) {
  15.         // Spinlock
  16.         ulong itinerations = 0;
  17.  
  18.         if (volatileLoad(lock)) {
  19.             if (++itinerations == deadlockItinerations) {
  20.                 warning("Potential spinlock in core #%u");
  21.                 itinerations = 0;
  22.             }
  23.         }
  24.     } else volatileStore(lock, 1);
  25. }
  26.  
  27. // Release an used lock
  28. void lockRelease(Lock* lock) {
  29.     import core.bitop;
  30.     import util.term;
  31.  
  32.     if (!volatileLoad(lock)) {
  33.         panic("Trying to release an already released lock");
  34.     }
  35.  
  36.     volatileStore(lock, 0);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement