/**
   * Constructor.
   *
   * @param name Latch name.
   * @param cnt Current count.
   * @param initCnt Initial count.
   * @param autoDel Auto delete flag.
   * @param key Latch key.
   * @param latchView Latch projection.
   * @param ctx Cache context.
   */
  public GridCacheCountDownLatchImpl(
      String name,
      int cnt,
      int initCnt,
      boolean autoDel,
      GridCacheInternalKey key,
      GridCacheProjection<GridCacheInternalKey, GridCacheCountDownLatchValue> latchView,
      GridCacheContext ctx) {
    assert name != null;
    assert cnt >= 0;
    assert initCnt >= 0;
    assert key != null;
    assert latchView != null;
    assert ctx != null;

    this.name = name;
    this.cnt = cnt;
    this.initCnt = initCnt;
    this.autoDel = autoDel;
    this.key = key;
    this.latchView = latchView;
    this.ctx = ctx;

    log = ctx.gridConfig().getGridLogger().getLogger(getClass());
  }
 /** {@inheritDoc} */
 @Override
 public GridFuture<Boolean> awaitAsync(final long timeout, final TimeUnit unit) {
   return ctx.closures()
       .callLocalSafe(
           new Callable<Boolean>() {
             @Override
             public Boolean call() throws Exception {
               return await(timeout, unit);
             }
           },
           true);
 }
  /**
   * Default constructor.
   *
   * @param name Sequence name.
   * @param key Sequence key.
   * @param seqView Sequence projection.
   * @param ctx CacheContext.
   * @param locVal Local counter.
   * @param upBound Upper bound.
   */
  public GridCacheAtomicSequenceImpl(
      String name,
      GridCacheInternalStorableKey key,
      GridCacheProjection<GridCacheInternalStorableKey, GridCacheAtomicSequenceValue> seqView,
      GridCacheContext ctx,
      long locVal,
      long upBound) {
    assert key != null;
    assert seqView != null;
    assert ctx != null;
    assert locVal <= upBound;

    batchSize = ctx.config().getAtomicSequenceReserveSize();
    this.ctx = ctx;
    this.key = key;
    this.seqView = seqView;
    this.upBound = upBound;
    this.locVal = locVal;
    this.name = name;

    log = ctx.gridConfig().getGridLogger().getLogger(getClass());
  }
  /** {@inheritDoc} */
  @Override
  public GridFuture<?> awaitAsync() {
    return ctx.closures()
        .callLocalSafe(
            new Callable<Object>() {
              @Nullable
              @Override
              public Object call() throws Exception {
                await();

                return null;
              }
            },
            true);
  }
  /**
   * Default constructor.
   *
   * @param name Atomic reference name.
   * @param key Atomic reference key.
   * @param atomicView Atomic projection.
   * @param ctx Cache context.
   */
  public GridCacheAtomicReferenceImpl(
      String name,
      GridCacheInternalKey key,
      GridCacheProjection<GridCacheInternalKey, GridCacheAtomicReferenceValue<T>> atomicView,
      GridCacheContext ctx) {
    assert key != null;
    assert atomicView != null;
    assert ctx != null;
    assert name != null;

    this.ctx = ctx;
    this.key = key;
    this.atomicView = atomicView;
    this.name = name;

    log = ctx.gridConfig().getGridLogger().getLogger(getClass());
  }
 /** @param ctx Context. */
 public GridNearCache(GridCacheContext<K, V> ctx) {
   super(ctx, ctx.config().getNearStartSize());
 }
 /** @param ctx Cache registry. */
 public GridReplicatedCache(GridCacheContext<K, V> ctx) {
   super(ctx, ctx.config().getStartSize());
 }
  /**
   * Asynchronous sequence update operation. Will add given amount to the sequence value.
   *
   * @param l Increment amount.
   * @param updateCall Cache call that will update sequence reservation count in accordance with l.
   * @param updated If {@code true}, will return sequence value after update, otherwise will return
   *     sequence value prior to update.
   * @return Future indicating sequence value.
   * @throws GridException If update failed.
   */
  private GridFuture<Long> internalUpdateAsync(
      long l, @Nullable Callable<Long> updateCall, boolean updated) throws GridException {
    checkRemoved();

    A.ensure(l > 0, " Parameter mustn't be less then 1: " + l);

    lock.lock();

    try {
      // If reserved range isn't exhausted.
      if (locVal + l <= upBound) {
        long curVal = locVal;

        locVal += l;

        return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
      }
    } finally {
      lock.unlock();
    }

    if (updateCall == null) updateCall = internalUpdate(l, updated);

    while (true) {
      if (updateGuard.compareAndSet(false, true)) {
        try {
          // This call must be outside lock.
          return ctx.closures().callLocalSafe(updateCall, true);
        } finally {
          lock.lock();

          try {
            updateGuard.set(false);

            cond.signalAll();
          } finally {
            lock.unlock();
          }
        }
      } else {
        lock.lock();

        try {
          while (locVal >= upBound && updateGuard.get()) {
            try {
              cond.await(500, MILLISECONDS);
            } catch (InterruptedException e) {
              throw new GridInterruptedException(e);
            }
          }

          checkRemoved();

          // If reserved range isn't exhausted.
          if (locVal + l <= upBound) {
            long curVal = locVal;

            locVal += l;

            return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
          }
        } finally {
          lock.unlock();
        }
      }
    }
  }
 /** {@inheritDoc} */
 @Override
 public GridFuture<?> countDownAllAsync() {
   return ctx.closures().callLocalSafe(new CountDownCallable(0), true);
 }
 /** {@inheritDoc} */
 @Override
 public GridFuture<Integer> countDownAsync(int val) {
   return ctx.closures().callLocalSafe(new CountDownCallable(val), true);
 }