Пример #1
0
  /**
   * Sets the block id, data and auxData for the block at (x, y, z), if the current data matches the
   * expected data.<br>
   *
   * @param x the x coordinate
   * @param y the y coordinate
   * @param z the z coordinate
   * @param expectId the expected block id
   * @param expectData the expected block data
   * @param expectAuxData the expected block auxiliary data
   * @param newId the new block id
   * @param newData the new block data
   * @param newAuxData the new block auxiliary data
   * @return true if the block was set
   */
  @Override
  public boolean compareAndSetBlock(
      int x, int y, int z, short expectId, short expectData, short newId, short newData) {
    int index = getIndex(x, y, z);
    int spins = 0;
    boolean interrupted = false;
    try {
      while (true) {
        if (spins++ > SPINS) {
          interrupted |= atomicWait(index);
        }
        checkCompressing();

        short oldBlockId = blockIds.get(index);
        boolean oldReserved = auxStore.isReserved(oldBlockId);

        if (!oldReserved) {
          if (blockIds.get(index) != expectId || expectData != 0) {
            return false;
          }
        } else {
          int seq = auxStore.getSequence(oldBlockId);
          short oldId = auxStore.getId(oldBlockId);
          short oldData = auxStore.getData(oldBlockId);
          if (!testSequence(x, y, z, seq)) {
            continue;
          }
          if (oldId != expectId || oldData != expectData) {
            return false;
          }
        }

        if (newData == 0 && !auxStore.isReserved(newId)) {
          if (!blockIds.compareAndSet(index, oldBlockId, newId)) {
            continue;
          }
        } else {
          int newIndex = auxStore.add(newId, newData);
          if (!blockIds.compareAndSet(index, oldBlockId, (short) newIndex)) {
            auxStore.remove(newIndex);
            continue;
          }
        }

        if (oldReserved) {
          auxStore.remove(oldBlockId);
        }

        markDirty(x, y, z);
        return true;
      }
    } finally {
      atomicNotify(index);
      if (interrupted) {
        Thread.currentThread().interrupt();
      }
    }
  }
Пример #2
0
  /**
   * Sets the block id, data and auxData for the block at (x, y, z).<br>
   * <br>
   * If the data is 0 and the auxData is null, then the block will be stored as a single short.<br>
   *
   * @param x the x coordinate
   * @param y the y coordinate
   * @param z the z coordinate
   * @param id the block id
   * @param data the block data
   * @param auxData the block auxiliary data
   */
  public final void setBlock(int x, int y, int z, short id, short data, T auxData) {
    int index = getIndex(x, y, z);
    int spins = 0;
    boolean interrupted = false;
    try {
      while (true) {
        if (spins++ > SPINS) {
          interrupted |= atomicWait();
        }
        checkCompressing();

        short oldBlockId = blockIds.get(index);
        boolean oldReserved = auxStore.isReserved(oldBlockId);
        if (data == 0 && auxData == null && !auxStore.isReserved(id)) {
          if (!blockIds.compareAndSet(index, oldBlockId, id)) {
            continue;
          }
          if (oldReserved) {
            if (!auxStore.remove(oldBlockId)) {
              throw new IllegalStateException(
                  "setBlock() tried to remove old record, but it had already been removed");
            }
          }
          return;
        } else {
          int newIndex = auxStore.add(id, data, auxData);
          if (!blockIds.compareAndSet(index, oldBlockId, (short) newIndex)) {
            if (auxStore.remove(newIndex)) {
              throw new IllegalStateException(
                  "setBlock() tried to remove old record, but it had already been removed");
            }
            continue;
          }
          if (oldReserved) {
            if (!auxStore.remove(oldBlockId)) {
              throw new IllegalStateException(
                  "setBlock() tried to remove old record, but it had already been removed");
            }
          }
          return;
        }
      }
    } finally {
      markDirty(x, y, z);
      atomicNotify();
      if (interrupted) {
        Thread.currentThread().interrupt();
      }
    }
  }
Пример #3
0
  private int getAndSetBlockRaw(int x, int y, int z, short id, short data) {
    int index = getIndex(x, y, z);
    int spins = 0;
    boolean interrupted = false;
    try {
      while (true) {
        if (spins++ > SPINS) {
          interrupted |= atomicWait(index);
        }
        checkCompressing();

        short oldBlockId = blockIds.get(index);
        boolean oldReserved = auxStore.isReserved(oldBlockId);
        if (data == 0 && !auxStore.isReserved(id)) {
          if (!blockIds.compareAndSet(index, oldBlockId, id)) {
            continue;
          }
        } else {
          int newIndex = auxStore.add(id, data);
          if (!blockIds.compareAndSet(index, oldBlockId, (short) newIndex)) {
            auxStore.remove(newIndex);
            continue;
          }
        }

        if (oldReserved) {
          return auxStore.remove(oldBlockId);
        }

        return BlockFullState.getPacked(oldBlockId, (short) 0);
      }
    } finally {
      markDirty(x, y, z);
      atomicNotify(index);
      if (interrupted) {
        Thread.currentThread().interrupt();
      }
    }
  }
Пример #4
0
 /**
  * Compresses the auxiliary store.<br>
  * <br>
  * This method should only be called when the store is guaranteed not to be accessed from any
  * other thread.<br>
  */
 @Override
 public void compress() {
   if (!compressing.compareAndSet(false, true)) {
     throw new IllegalStateException("Compression started while compression was in progress");
   }
   int length = side * side * side;
   AtomicIntArrayStore newAuxStore = new AtomicIntArrayStore(length);
   for (int i = 0; i < length; i++) {
     short blockId = blockIds.get(i);
     if (auxStore.isReserved(blockId)) {
       short storedId = auxStore.getId(blockId);
       short storedData = auxStore.getData(blockId);
       int newIndex = newAuxStore.add(storedId, storedData);
       if (!blockIds.compareAndSet(i, blockId, (short) newIndex)) {
         throw new IllegalStateException("Unstable block id data during compression step");
       }
     }
   }
   auxStore = newAuxStore;
   compressing.set(false);
 }
Пример #5
0
  /**
   * Sets the block id, data and auxData for the block at (x, y, z), if the current data matches the
   * expected data.<br>
   *
   * @param x the x coordinate
   * @param y the y coordinate
   * @param z the z coordinate
   * @param expectId the expected block id
   * @param expectData the expected block data
   * @param expectAuxData the expected block auxiliary data
   * @param newId the new block id
   * @param newData the new block data
   * @param newAuxData the new block auxiliary data
   * @return true if the block was set
   */
  public final boolean compareAndSetBlock(
      int x,
      int y,
      int z,
      short expectId,
      short expectData,
      T expectAuxData,
      short newId,
      short newData,
      T newAuxData) {
    int index = getIndex(x, y, z);
    int spins = 0;
    boolean interrupted = false;
    try {
      while (true) {
        if (spins++ > SPINS) {
          interrupted |= atomicWait();
        }
        checkCompressing();

        short oldBlockId = blockIds.get(index);
        boolean oldReserved = auxStore.isReserved(oldBlockId);

        if (!oldReserved) {
          if (blockIds.get(index) != expectId || expectData != 0 || expectAuxData != null) {
            return false;
          }
        } else {
          int seq = auxStore.getSequence(oldBlockId);
          short oldId = auxStore.getId(oldBlockId);
          short oldData = auxStore.getData(oldBlockId);
          T oldAuxData = auxStore.getAuxData(oldBlockId);
          if (!testSequence(x, y, z, seq)) {
            continue;
          }
          if (oldId != expectId || oldData != expectData || oldAuxData != expectAuxData) {
            return false;
          }
        }

        if (newData == 0 && newAuxData == null && !auxStore.isReserved(newId)) {
          if (!blockIds.compareAndSet(index, oldBlockId, newId)) {
            continue;
          }
          if (oldReserved) {
            if (!auxStore.remove(oldBlockId)) {
              throw new IllegalStateException(
                  "setBlock() tried to remove old record, but it had already been removed");
            }
          }
          markDirty(x, y, z);
          return true;
        } else {
          int newIndex = auxStore.add(newId, newData, newAuxData);
          if (!blockIds.compareAndSet(index, oldBlockId, (short) newIndex)) {
            if (!auxStore.remove(newIndex)) {
              throw new IllegalStateException(
                  "setBlock() tried to remove old record, but it had already been removed");
            }
            continue;
          }
          if (oldReserved) {
            if (!auxStore.remove(oldBlockId)) {
              throw new IllegalStateException(
                  "setBlock() tried to remove old record, but it had already been removed");
            }
          }
          markDirty(x, y, z);
          return true;
        }
      }
    } finally {
      atomicNotify();
      if (interrupted) {
        Thread.currentThread().interrupt();
      }
    }
  }