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 ExclusiveLock 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 33 public TransactionID locker; 34 35 36 public ExclusiveLock() { 37 reset(); 38 } 39 40 41 public synchronized void reset() { 42 level = LEVEL_NONE; 43 locker = null; 44 } 45 46 47 public int tryAcquire( Transaction ta, int newLevel ) { 48 if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) { 49 ta.env.logWriter.newEntry( this, "tryAcquire(): current:" + level + " new:" + newLevel, LogWriter.DEBUG3 ); 50 } 51 synchronized (this) { 52 int prevLevel = level( ta ); 53 if (locker == null || isAcquiredBy( ta )) { 54 locker = ta.taID(); 55 level = newLevel > level ? newLevel : level; 56 return prevLevel; 57 } else { 58 return NOT_ACQUIRED; 59 } 60 } 61 } 62 63 64 public void release( Transaction ta ) { 65 if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) { 66 ta.env.logWriter.newEntry( this, "release()", LogWriter.DEBUG3 ); 67 } 68 synchronized (this) { 69 if (!isAcquiredBy( ta )) { 70 ta.env.logWriter.newEntry( this, "release(): specified transaction has not aquired lock.", LogWriter.WARN ); 71 } 72 locker = null; 73 level = LEVEL_NONE; 74 } 75 } 76 77 78 public boolean isAcquiredBy( Transaction ta ) { 79 return locker != null ? locker.equals( ta.taID() ) : false; 80 } 81 82 83 public DxCollection lockerIDs() { 84 DxBag result = new DxArrayBag(); 85 if (locker != null) { 86 result.add( locker ); 87 } 88 return result; 89 } 90 91 92 public int level( Transaction ta ) { 93 if (ta != null) { 94 return isAcquiredBy( ta ) ? level : LEVEL_NONE; 95 } else { 96 return level; 97 } 98 } 99 100 public TransactionID getLocker() { 101 return locker; 102 } 103 } 104 | Popular Tags |