1 9 package org.ozoneDB.core; 10 11 import org.ozoneDB.DxLib.*; 12 import org.ozoneDB.util.LogWriter; 13 14 15 23 public final class SharedLock extends AbstractLock { 24 25 protected final static long serialVersionUID = 1; 26 protected final static byte subSerialVersionUID = 1; 27 28 private int level = LEVEL_NONE; 29 30 private transient int prevLevel = LEVEL_NONE; 31 32 35 public DxSet lockers; 36 37 38 public SharedLock() { 39 reset(); 40 } 41 42 43 public synchronized void reset() { 44 level = prevLevel = LEVEL_NONE; 45 lockers = new DxHashSet( 8 ); 46 } 47 48 49 public int tryAcquire( Transaction ta, int newLevel ) { 50 if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) { 51 ta.env.logWriter.newEntry( this, "tryAcquire(): current:" + level + " new:" + newLevel, LogWriter.DEBUG3 ); 52 } 53 synchronized (this) { 54 int prevLevel = level( ta ); 55 lockers.add( ta.taID() ); 56 57 if (!lockers.contains( ta.taID() )) { 58 throw new RuntimeException ( "tryAcquire(): unable to add ta to lockers." ); 59 } 61 level = newLevel > level ? newLevel : level; 62 return prevLevel; 63 } 64 } 65 66 67 public void release( Transaction ta ) { 68 if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) { 69 ta.env.logWriter.newEntry( this, "release()", LogWriter.DEBUG3 ); 70 } 71 synchronized (this) { 72 if (!lockers.remove( ta.taID )) { 73 throw new RuntimeException ( "release(): transaction does not hold the lock." ); 74 } 75 if (lockers.isEmpty()) { 76 level = LEVEL_NONE; 77 } 78 if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) { 79 ta.env.logWriter.newEntry( this, "release(): count=" + lockers.count() + ", level=" + level, LogWriter.DEBUG3 ); 80 } 81 } 82 } 83 84 85 public boolean isAcquiredBy( Transaction ta ) { 86 return lockers != null ? lockers.contains( ta.taID() ) : false; 87 } 88 89 90 public DxCollection lockerIDs() { 91 return lockers != null ? lockers : new DxHashSet(); 92 } 93 94 95 public int level( Transaction ta ) { 96 if (ta != null) { 97 return isAcquiredBy( ta ) ? level : LEVEL_NONE; 98 } else { 99 return level; 100 } 101 } 102 103 104 public int previousLevel() { 105 return prevLevel; 106 } 107 108 public boolean areMultipleLockersHoldingLocks() { 109 return lockers.count()>=2; 110 } 111 112 113 public boolean hasChanged() { 114 return level > prevLevel; 115 } 116 117 public String toString() { 118 return "SharedLock[lockers="+lockers+"]"; 119 } 120 } 121 | Popular Tags |