Example #1
0
 /** ロック開放後同一カテゴリとキーでロック取得可能. */
 @Test
 public void ロック開放後同一カテゴリとキーでロック取得可能() {
   Lock lock = LockManager.getLock(Lock.CATEGORY_ODATA, "aaa", null, null);
   lock.release();
   Lock lock2 = LockManager.getLock(Lock.CATEGORY_ODATA, "aaa", null, null);
   assertNotNull(lock2);
   lock2.release();
 }
Example #2
0
 /** 違うカテゴリであれば同キー名での連続取得が可能. */
 @Test
 public void 違うカテゴリであれば同キー名での連続取得が可能() {
   Lock lockOData = LockManager.getLock(Lock.CATEGORY_ODATA, "aaa", null, null);
   Lock lockDav = LockManager.getLock(Lock.CATEGORY_DAV, "aaa", null, null);
   assertNotNull(lockOData);
   assertNotNull(lockDav);
   lockDav.release();
   lockOData.release();
 }
Example #3
0
  /** Test if this module is working. */
  public static void selfTest() {

    System.out.print("Enter Condition2.selfTest\n");

    Lock lock = new Lock();
    Condition2 condition = new Condition2(lock);

    KThread t[] = new KThread[10];
    for (int i = 0; i < 10; i++) {
      t[i] = new KThread(new Condition2Test(lock, condition));
      t[i].setName("Thread" + i).fork();
    }

    KThread.yield();

    lock.acquire();

    System.out.print("condition.wake();\n");
    condition.wake();

    System.out.print("condition.wakeAll();\n");
    condition.wakeAll();

    lock.release();

    System.out.print("Leave Condition2.selfTest\n");

    t[9].join();
  }
Example #4
0
  static void AdultItinerary() {
    System.out.println("--- called adult itinerary");

    /* This is where you should put your solutions. Make calls
     to the BoatGrader to show that it is synchronized. For
     example:
         bg.AdultRowToMolokai();
     indicates that an adult has rowed the boat across to Molokai
    */

    pilotLock.acquire();

    // While the boat is not at Oahu or there are more than 2 children on Oahu,
    // put the adult on Oahu thread to sleep (because we wouldn't send and adult in
    // these scenarios).
    while (boatLocation != 0 || numChildrenOnOahu >= 2) {
      System.out.println("--- can't send an adult to Molokai right now");
      adultReadyAtOahu.sleep();
    }

    System.out.println("--- sending an adult to Molokai");
    numAdultsOnOahu--; // an adult is rowing Oahu --> Molokai, so decrement
    bg.AdultRowToMolokai();
    numAdultsOnMolokai++; // adult has rowed over, so increment
    printState();
    boatLocation = 1; // boat is now at Molokai
    childReadyAtMolokai.wake(); // wake up child at Molokai since it's now their turn			

    pilotLock.release();
  }
Example #5
0
  /** Do the real work of releaseAndFindNotifyTargets */
  Set<Locker> releaseAndFindNotifyTargetsInternal(long nodeId, Locker locker, int lockTableIndex) {
    Map<Long, Lock> lockTable = lockTables[lockTableIndex];
    Lock useLock = lockTable.get(nodeId);
    if (useLock == null) {
      useLock = lockTable.get(Long.valueOf(nodeId));
    }

    if (useLock == null) {
      /* Lock doesn't exist. */
      return null;
    }

    Set<Locker> lockersToNotify = useLock.release(locker, memoryBudget, lockTableIndex);
    if (lockersToNotify == null) {
      /* Not owner. */
      return null;
    }

    /* If it's not in use at all, remove it from the lock table. */
    if ((useLock.nWaiters() == 0) && (useLock.nOwners() == 0)) {
      lockTables[lockTableIndex].remove(nodeId);
      if (useLock.isThin()) {
        memoryBudget.updateLockMemoryUsage(REMOVE_TOTAL_THINLOCKIMPL_OVERHEAD, lockTableIndex);
      } else {
        memoryBudget.updateLockMemoryUsage(REMOVE_TOTAL_LOCKIMPL_OVERHEAD, lockTableIndex);
      }
    }

    return lockersToNotify;
  }
Example #6
0
 /** 同じキー名での取得は2回目以降のものはブロックされる. */
 @Test
 public void 同じキー名での取得は2回目以降のものはブロックされる() {
   Lock lock = null;
   Lock lock2 = null;
   String lockName = "lk" + new Date().getTime();
   try {
     lock = LockManager.getLock(Lock.CATEGORY_ODATA, lockName, null, null);
     lock2 = LockManager.getLock(Lock.CATEGORY_ODATA, lockName, null, null);
   } catch (DcCoreException e) {
     assertEquals(DcCoreException.class, e.getClass());
   } finally {
     lock.release();
     if (lock2 != null) {
       lock2.release();
     }
   }
 }
  @Test
  public void testSimpleLocking() throws Exception {
    ByteBufferCache cache = new ByteBufferCache(40, 80, true);
    ByteBufferDirectory dir = new ByteBufferDirectory(cache);

    Lock lock = dir.makeLock("testlock");

    assertThat(lock.isLocked(), equalTo(false));
    assertThat(lock.obtain(200), equalTo(true));
    assertThat(lock.isLocked(), equalTo(true));
    try {
      assertThat(lock.obtain(200), equalTo(false));
      assertThat("lock should be thrown", false, equalTo(true));
    } catch (LockObtainFailedException e) {
      // all is well
    }
    lock.release();
    assertThat(lock.isLocked(), equalTo(false));
    dir.close();
    cache.close();
  }
Example #8
0
  static void ChildItinerary() {
    System.out.println("--- called child itinerary");

    pilotLock.acquire();

    Boolean readyToTerminate = false;
    int currentLocation = 0; // current location set to Oahu		

    while (!isDone) {
      if (currentLocation == 0) {
        // If we are currently on Oahu...
        while (boatLocation != 0 || numChildrenOnOahu == 1) {
          System.out.println(
              "--- child currently on Oahu, but the boat is not on Oahu ("
                  + boatLocation
                  + ") or we only have 1 child on Oahu ("
                  + numChildrenOnOahu
                  + ")");
          // ...but the boat is not in Oahu or we only have 1 child on Oahu, then sleep.
          childReadyAtOahu.sleep();
        }

        System.out.println("--- child currently on Oahu, ready to go");

        if (numAdultsOnOahu == 0 && numChildrenOnOahu == 2) {
          // ... we are ready to end the game!
          System.out.println("--- ready to end the game");
          readyToTerminate = true;
        }

        if (!hasChildPilot) {
          hasChildPilot = true;
          bg.ChildRowToMolokai();
          printState();
          currentLocation = 1; // this child are now now on Molokai		
        } else {
          numChildrenOnOahu -= 2; // 2 kids will go Oahu --> Molokai, so decrement
          bg.ChildRideToMolokai();
          numChildrenOnMolokai += 2; // 2 kids have arrived on Molokai, so increment	
          printState();
          currentLocation = 1; // this child are now now on Molokai
          boatLocation = 1; // boat is now at Molokai
          hasChildPilot = false;

          if (readyToTerminate) {
            gameEnd.acquire();
            end.wake();
            gameEnd.release();
            childReadyAtMolokai.sleep();
          } else {
            System.out.println("--- child ready on Molokai");
            childReadyAtMolokai.wake();
          }
        }
      } else {
        // Else, if we are currently on Molokai...
        while (boatLocation != 1 || hasChildPilot) {
          System.out.println(
              "--- child currently on Molokai, but either the boat is not on Molokai or there's already a child pilot");
          // ...but the boat is not at Molokai, then sleep.
          childReadyAtMolokai.sleep();
        }

        // ...then send a child back to Oahu.
        hasChildPilot = true;
        System.out.println("--- child currently on Molokai, send one back to Oahu");
        numChildrenOnMolokai--; // child is going from Molokai --> Oahu, so decrement
        bg.ChildRowToOahu();
        numChildrenOnOahu++; // child arrived at Oahu, so incrememt
        printState();
        boatLocation = 0; // boat is now at Oahu
        currentLocation = 0; // child is now at Oahu
        hasChildPilot = false;

        if (numChildrenOnOahu >= 2) {
          System.out.println("--- we now have 2 children on Oahu");
          childReadyAtOahu.wake(); // let Oahu now we're ready to go		
        } else {
          System.out.println("--- we don't enough children on Oahu so send an adult");
          adultReadyAtOahu.wake();
          childReadyAtOahu.sleep();
        }
      }
    }
  }
  public static void main(String[] args) throws Exception {

    if (args.length != 6) {
      System.out.println(
          "\nUsage: java org.apache.lucene.store.LockStressTest myID verifierHostOrIP verifierPort lockFactoryClassName lockDirName sleepTime\n"
              + "\n"
              + "  myID = int from 0 .. 255 (should be unique for test process)\n"
              + "  verifierHostOrIP = host name or IP address where LockVerifyServer is running\n"
              + "  verifierPort = port that LockVerifyServer is listening on\n"
              + "  lockFactoryClassName = primary LockFactory class that we will use\n"
              + "  lockDirName = path to the lock directory (only set for Simple/NativeFSLockFactory\n"
              + "  sleepTimeMS = milliseconds to pause betweeen each lock obtain/release\n"
              + "\n"
              + "You should run multiple instances of this process, each with its own\n"
              + "unique ID, and each pointing to the same lock directory, to verify\n"
              + "that locking is working correctly.\n"
              + "\n"
              + "Make sure you are first running LockVerifyServer.\n"
              + "\n");
      System.exit(1);
    }

    final int myID = Integer.parseInt(args[0]);

    if (myID < 0 || myID > 255) {
      System.out.println("myID must be a unique int 0..255");
      System.exit(1);
    }

    final String verifierHost = args[1];
    final int verifierPort = Integer.parseInt(args[2]);
    final String lockFactoryClassName = args[3];
    final String lockDirName = args[4];
    final int sleepTimeMS = Integer.parseInt(args[5]);

    Class c;
    try {
      c = Class.forName(lockFactoryClassName);
    } catch (ClassNotFoundException e) {
      throw new IOException("unable to find LockClass " + lockFactoryClassName);
    }

    LockFactory lockFactory;
    try {
      lockFactory = (LockFactory) c.newInstance();
    } catch (IllegalAccessException e) {
      throw new IOException(
          "IllegalAccessException when instantiating LockClass " + lockFactoryClassName);
    } catch (InstantiationException e) {
      throw new IOException(
          "InstantiationException when instantiating LockClass " + lockFactoryClassName);
    } catch (ClassCastException e) {
      throw new IOException(
          "unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory");
    }

    File lockDir = new File(lockDirName);

    if (lockFactory instanceof NativeFSLockFactory) {
      ((NativeFSLockFactory) lockFactory).setLockDir(lockDir);
    } else if (lockFactory instanceof SimpleFSLockFactory) {
      ((SimpleFSLockFactory) lockFactory).setLockDir(lockDir);
    }

    lockFactory.setLockPrefix("test");

    LockFactory verifyLF =
        new VerifyingLockFactory((byte) myID, lockFactory, verifierHost, verifierPort);

    Lock l = verifyLF.makeLock("test.lock");

    while (true) {

      boolean obtained = false;

      try {
        obtained = l.obtain(10);
      } catch (LockObtainFailedException e) {
        System.out.print("x");
      }

      if (obtained) {
        System.out.print("l");
        l.release();
      }
      Thread.sleep(sleepTimeMS);
    }
  }
Example #10
0
 /** ロックが取得可能. */
 @Test
 public void ロックが取得可能() {
   Lock lock = LockManager.getLock(Lock.CATEGORY_ODATA, "aaa", null, null);
   assertNotNull(lock);
   lock.release();
 }