예제 #1
0
  @Override
  public void run() {
    Logger.debug((lock ? "Lock" : "Unlock") + " request received from " + from + " for " + key);
    boolean success = false;
    try {
      int localClock = LocalClock.get();
      if (senderClock > localClock) LocalClock.advance(senderClock);
      AbstractDistinguishable object =
          ((DTL2Directory) DirectoryManager.getManager()).getLocalObject(key);
      if (object != null) {
        if (!lock) {
          LockTable.unLock(object, null);
          Logger.debug("Remote Unlocked " + key);
          return;
        }

        LockTable.remoteLockResponse(object);
        Logger.debug("Remote Locked granted " + key);
        success = true;
      }
    } catch (TransactionException e) {
      Logger.debug("Remote Locked refused " + key);
    }
    try {
      CommunicationManager.getManager().send(from, new LockResponse(key, success, hashCode));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
예제 #2
0
  public static boolean lock(Object lockIndex, Set<Object> contextLocks)
      throws TransactionException {
    AtomicInteger lock = getLock(lockIndex);
    int val = lock.get();
    if ((val & REMOTE) != 0) { // is remote lock
      Logger.debug("Remotly Locked object");
      return false;
    }

    //		final int selfLockIndex = lockIndex>>>DIVIDE_8;
    //		final byte selfLockByte = contextLocks[selfLockIndex];
    //		final byte selfLockBit = (byte)(1 << (lockIndex & MODULE_8));

    if ((val & LOCK) != 0) { // is already locked?

      if (contextLocks.contains(lockIndex)) // check for self locking
      return true;
      Logger.debug("Locally Locked object");
      throw FAILURE_EXCEPTION;
    }

    boolean isLocked = lock.compareAndSet(val, val | LOCK);

    if (!isLocked) {
      Logger.debug("Being Locked object");
      throw FAILURE_EXCEPTION;
    }

    contextLocks.add(lockIndex); // mark in self locks

    Logger.debug("Successful Locked object");
    return true;
  }
예제 #3
0
  public static void remoteLockResponse(Object lockIndex) throws TransactionException {
    AtomicInteger lock = getLock(lockIndex);
    int val = lock.get();
    if ((val & LOCK) != 0) { // is already locked?
      Logger.debug("Remote Lock Failed: Locked object");
      throw FAILURE_EXCEPTION;
    }

    boolean isLocked = lock.compareAndSet(val, val | LOCK);

    if (!isLocked) {
      Logger.debug("Remote Lock Failed: Concurrent Locked object");
      throw FAILURE_EXCEPTION;
    }

    Logger.debug("Remote Successful Locked object executed");
  }
예제 #4
0
 public static boolean isAvailable(Object lockIndex) {
   AtomicInteger lock = getLock(lockIndex);
   int val = lock.get();
   boolean remote = (val & REMOTE) != 0;
   boolean locked = (val & LOCK) != 0;
   Logger.debug("Available " + lockIndex + " " + val + " <" + remote + "," + locked + ">");
   return remote || locked;
 }
예제 #5
0
 public static void remoteLockRequest(AbstractContext context, GlobalObject key) {
   int hashCode = context.hashCode();
   pendingLocks.put(hashCode, context);
   synchronized (context) {
     try {
       Logger.debug(
           "Request remote lock for " + key + " hashcode is " + hashCode + " for " + context);
       CommunicationManager.getManager().send(key.getHome(), new LockRequest(key, true, hashCode));
       context.wait();
       Logger.debug("Request remote lock come back...");
     } catch (InterruptedException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   pendingLocks.remove(hashCode);
 }
예제 #6
0
 public static void remoteUnlockRequest(GlobalObject key) {
   if (key == null) return;
   try {
     Logger.debug("Remote Lock release for " + key + " sent to " + key.getHome());
     CommunicationManager.getManager().send(key.getHome(), new LockRequest(key, false, 0));
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
예제 #7
0
 @Override
 public void run() {
   Logger.debug(key + " Retured Lock " + (success ? "granted" : "refused"));
   if (senderClock > LocalClock.get()) LocalClock.advance(senderClock);
   if (hashCode == 0) {
     Logger.debug("No call backs");
     return;
   }
   Logger.debug("Hashcode is " + hashCode);
   AbstractContext context = LockTable.pendingLocks.get(hashCode);
   if (context == null) {
     System.out.println("Null context: " + hashCode);
     return;
   }
   Logger.debug(context + " will notified");
   synchronized (context) {
     ((Context) context).pendingLock = success;
     context.notifyAll();
     Logger.debug(context + " notified");
   }
 }
예제 #8
0
  public static boolean unLock(Object lockIndex, Set<Object> contextLocks) {
    AtomicInteger lock = getLock(lockIndex);
    int val = lock.get();

    if ((val & REMOTE) != 0) { // is remote lock
      Logger.debug("Can't unlock remote object");
      return false;
    }

    int unlockedValue = val & UNLOCK;
    if (!lock.compareAndSet(val, unlockedValue)) {
      Logger.debug("Fail due to concurrent unlock!!");
      return false;
    }

    if (contextLocks != null) clearSelfLock(lockIndex, contextLocks);

    Logger.debug("Unlock local object successfully.");

    return true;
  }
예제 #9
0
  public static boolean setAndReleaseLock(
      Object lockIndex, int newClock, Set<Object> contextLocks) {
    AtomicInteger lock = getLock(lockIndex);
    int val = lock.get();

    boolean remote = ((val & REMOTE) != 0); // is remote lock
    Logger.debug("set & release " + lockIndex + " from " + val + "/" + remote + " to " + newClock);

    lock.set(newClock); // now it will be local
    clearSelfLock(lockIndex, contextLocks);

    return remote;
  }
예제 #10
0
  public static int checkLock(Object lockIndex, int clock, boolean checkLocalLock) {
    //		System.out.println("Check lock for " + lockIndex);
    AtomicInteger lock = getLock(lockIndex);
    int val = lock.get();

    if ((val & REMOTE) != 0) {
      Logger.debug("Remotly Locked object");
      return -1;
    }

    if (clock
        < (val & UNLOCK)) { // check the clock without lock, TODO check if this is the best way
      Logger.debug("Outdated version: " + clock + " current is " + (val & UNLOCK));
      throw FAILURE_EXCEPTION;
    }

    if (checkLocalLock && (val & LOCK) != 0) { // is already locked?
      Logger.debug("on read: Locally Locked object");
      throw FAILURE_EXCEPTION;
    }

    Logger.debug("Valid Local object");
    return val;
  }
예제 #11
0
 @Override
 public void setRemote(Object id, String ownerIP, int ownerPort) {
   $HY$_id = id;
   try {
     $HY$_proxy =
         (($HY$_IReservation)
             LocateRegistry.getRegistry(ownerIP, ownerPort).lookup(getClass().getName()));
   } catch (AccessException e) {
     e.printStackTrace();
   } catch (RemoteException e) {
     e.printStackTrace();
   } catch (NotBoundException e) {
     e.printStackTrace();
   } catch (Exception e) {
     try {
       Logger.debug(Arrays.toString(LocateRegistry.getRegistry(ownerIP, ownerPort).list()));
     } catch (AccessException e1) {
       e1.printStackTrace();
     } catch (RemoteException e1) {
       e1.printStackTrace();
     }
   }
 }
예제 #12
0
 public static void setRemote(Object lockIndex) {
   Logger.debug("changing version of " + lockIndex + " to remote.");
   getLock(lockIndex).set(REMOTE);
 }
 @Override
 public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
   if (ITypeInternalName.REMOTE.equals(desc)) remote = true;
   Logger.debug(method + " " + desc);
   return super.visitAnnotation(desc, visible);
 }