Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. public static void ensureSufficientLockHeld(LockContext lockContext, LockType lockType) {
  2. // TODO(hw4_part2): implement
  3.  
  4. TransactionContext transaction = TransactionContext.getTransaction(); // current transaction
  5. if(transaction == null){
  6. return;
  7. }
  8. //ensuring that we have the appropriate locks on ancestors,
  9. LockType currLockType = lockContext.getEffectiveLockType(transaction);
  10.  
  11. if(hasSufficient(currLockType, lockType)){
  12. return;
  13. }
  14.  
  15. //handle parent stuff
  16. if(lockContext.parent != null){
  17.  
  18.  
  19. //need to acquire proper parent locks from top >> bottom
  20. getCorrectParentLocks(lockContext.parent, LockType.parentLock(lockType));
  21.  
  22. }
  23.  
  24. //
  25. currLockType = lockContext.getExplicitLockType(transaction);
  26. if(currLockType.equals(LockType.IX) && lockType.equals(LockType.S)){
  27. lockContext.promote(transaction, LockType.SIX);
  28.  
  29. }
  30. else if(currLockType.equals(LockType.NL)){
  31. lockContext.acquire(transaction, lockType);
  32. }
  33. else{
  34. lockContext.escalate(transaction);
  35. currLockType = lockContext.getExplicitLockType(transaction);
  36. if(currLockType.equals(LockType.S) && lockType.equals(LockType.X)){
  37. lockContext.promote(transaction, lockType);
  38. }
  39. }
  40.  
  41.  
  42.  
  43.  
  44. //and acquiring the lock on the resource
  45. return;
  46. }
  47.  
  48. public static void getCorrectParentLocks(LockContext lockContext, LockType lockType){
  49. if(lockContext.parent != null){
  50. // LockType parentLock = lockContext.parent.getEffectiveLockType(transaction);
  51. //need to acquire proper parent locks from top >> bottom
  52. LockUtil.getCorrectParentLocks(lockContext.parent, LockType.parentLock(lockType));
  53. //acquire promote escalate
  54.  
  55.  
  56. }
  57. TransactionContext transaction = TransactionContext.getTransaction();
  58. LockType currLockType = lockContext.getExplicitLockType(transaction);
  59. if(currLockType.equals(LockType.X) || LockType.substitutable(currLockType ,lockType)){
  60. return;
  61. }
  62. if(currLockType.equals(LockType.S) && lockType.equals(LockType.IX)){
  63. lockContext.promote(transaction, LockType.SIX);
  64. }
  65. else if(lockContext.getEffectiveLockType(transaction).equals(LockType.NL)){
  66. lockContext.acquire(transaction, lockType);
  67. }
  68. else{
  69. lockContext.promote(transaction, lockType);
  70. }
  71.  
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement