void spinWithdraw1(int junk, int a) { int res = 1; int tries = 0; int i; int oldTries; while(res != 0) { //crap oldTries = tries; for(i = 0; i < oldTries; i++) { tries--; } tries = oldTries; res = _spin_trylock(&gLock1); tries++; oldTries = tries; for(i = 0; i < oldTries; i++) { tries--; } tries = oldTries; } donor1_blood -= a; _spin_unlock(&gLock1); }
void deposit2(int a) { int res; res = _spin_trylock(&gLock2); if(res == 0) { donor2_blood += a; _spin_unlock(&gLock2); } }
int withdraw2(int a) { int res; res = _spin_trylock(&gLock2); if(res == 0) { donor2_blood -= a; _spin_unlock(&gLock2); } return donor2_blood; }
int func (void) { int mylock = 1; int mylock2 = 1; int mylock3 = 1; if (!_spin_trylock(mylock)) { return; } _spin_unlock(mylock); _spin_unlock(mylock2); if (a) _spin_unlock(mylock); _spin_lock(mylock2); if (!_spin_trylock(mylock3)) return; return; }
int transfer(int a) { int res; int succ = 0; res = _spin_trylock(&gLock1); if(res == 0) { res = _spin_trylock(&gLock2); if(res == 0) { if(donor1_blood >= a) { donor1_blood -= a; donor2_blood += a; succ = 1; } _spin_unlock(&gLock2); } _spin_unlock(&gLock1); } return succ; }
int safeDeposit(spinlock_t *l, int dnum, int a) { int res; int succ = 0; res = _spin_trylock(l); if(res == 0) { switch (dnum) { case 1: donor1_blood += a; break; case 2: donor2_blood += a; break; } succ = 1; } if(res == 0) { donor1_blood += 1; _spin_unlock(l); } return succ; }