private void AttemptWriteLockOnResource(object data) { LM_Rid_Bundle bundle = (LM_Rid_Bundle)data; Transaction t = new Transaction(); try { bundle.lm.LockForWrite(t, bundle.rid); bundle.lm.UnlockAll(t); bundle.status = 0; } catch (DeadLockDetected) { bundle.status = 1; } }
public void LM_UpgradeReadLockToWriteLock2() { MyLM lm = new MyLM(); RID res1 = new RID(); Transaction t1 = new Transaction(); LM_Rid_Bundle param = new LM_Rid_Bundle(lm, res1); Thread thread = new Thread(AttemptWriteLockOnResource); lm.setDeadlockTimeout(3000); // Shorten deadlock timeout to 3 seconds speed up unit test lm.LockForRead(t1, res1); thread.Start(param); Thread.Sleep(1000); lm.LockForWrite(t1, res1); thread.Join(); // Make sure that the second thread failed with deadlock exception. // This should happen because the first thread is still holding either the read lock or write lock // when the second thread trys to acquire a write lock. The second thread won't be able to steal the // lock from the first thread because two phase locking was used when the LM upgrades the read lock // to a write lock. Assert.AreEqual(param.status, 1); }
public void LM_UpgradeReadLockToWriteLock2() { MyLM lm = new MyLM(); RID res1 = new RID(); Transaction t1 = new Transaction(); LM_Rid_Bundle param = new LM_Rid_Bundle(lm, res1); Thread thread = new Thread(AttemptWriteLockOnResource); lm.setDeadlockTimeout(3000); // Shorten deadlock timeout to 3 seconds speed up unit test lm.LockForRead(t1, res1); thread.Start(param); Thread.Sleep(1000); lm.LockForWrite(t1, res1); thread.Join(); // Make sure that the second thread failed with deadlock exception. // This should happen because the first thread is still holding either the read lock or write lock // when the second thread trys to acquire a write lock. The second thread won't be able to steal the // lock from the first thread because two phase locking was used when the LM upgrades the read lock // to a write lock. Assert.AreEqual(param.status, 1); }