Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module system.lock;
- // Itinerations of a spinlock necessary to call it a deadlock
- immutable ulong deadlockItinerations = 35000000;
- // The lock information, 1 if locked, 0 if not
- alias Lock = ubyte;
- // Test a lock, if its locked, wait for it to unlock, if not lock it.
- void adquireLock(Lock* lock) {
- import core.bitop;
- import util.term;
- if (volatileLoad(lock)) {
- // Spinlock
- ulong itinerations = 0;
- if (volatileLoad(lock)) {
- if (++itinerations == deadlockItinerations) {
- warning("Potential spinlock in core #%u");
- itinerations = 0;
- }
- }
- } else volatileStore(lock, 1);
- }
- // Release an used lock
- void lockRelease(Lock* lock) {
- import core.bitop;
- import util.term;
- if (!volatileLoad(lock)) {
- panic("Trying to release an already released lock");
- }
- volatileStore(lock, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement