Java Code Examples for java.util.concurrent.atomic.AtomicLong.compareAndSet()

The following are Jave code examples for showing how to use compareAndSet() of the java.util.concurrent.atomic.AtomicLong class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: RxJava3-preview   File: BackpressureHelper.java   View Source Code Vote up 7 votes
/**
 * Atomically adds the positive value n to the requested value in the AtomicLong and
 * caps the result at Long.MAX_VALUE and returns the previous value and
 * considers Long.MIN_VALUE as a cancel indication (no addition then).
 * @param requested the AtomicLong holding the current requested value
 * @param n the value to add, must be positive (not verified)
 * @return the original value before the add
 */
public static long addCancel(AtomicLong requested, long n) {
    for (;;) {
        long r = requested.get();
        if (r == Long.MIN_VALUE) {
            return Long.MIN_VALUE;
        }
        if (r == Long.MAX_VALUE) {
            return Long.MAX_VALUE;
        }
        long u = addCap(r, n);
        if (requested.compareAndSet(r, u)) {
            return r;
        }
    }
}
 
Example 2
Project: RxJava3-preview   File: BackpressureHelper.java   View Source Code Vote up 7 votes
/**
 * Atomically subtract the given number (positive, not validated) from the target field unless it contains Long.MAX_VALUE.
 * @param requested the target field holding the current requested amount
 * @param n the produced element count, positive (not validated)
 * @return the new amount
 */
public static long produced(AtomicLong requested, long n) {
    for (;;) {
        long current = requested.get();
        if (current == Long.MAX_VALUE) {
            return Long.MAX_VALUE;
        }
        long update = current - n;
        if (update < 0L) {
            RxJavaCommonPlugins.onError(new IllegalStateException("More produced than requested: " + update));
            update = 0L;
        }
        if (requested.compareAndSet(current, update)) {
            return update;
        }
    }
}
 
Example 3
Project: RxJava3-preview   File: BackpressureHelper.java   View Source Code Vote up 6 votes
/**
 * Atomically subtract the given number (positive, not validated) from the target field if
 * it doesn't contain Long.MIN_VALUE (indicating some cancelled state) or Long.MAX_VALUE (unbounded mode).
 * @param requested the target field holding the current requested amount
 * @param n the produced element count, positive (not validated)
 * @return the new amount
 */
public static long producedCancel(AtomicLong requested, long n) {
    for (;;) {
        long current = requested.get();
        if (current == Long.MIN_VALUE) {
            return Long.MIN_VALUE;
        }
        if (current == Long.MAX_VALUE) {
            return Long.MAX_VALUE;
        }
        long update = current - n;
        if (update < 0L) {
            RxJavaCommonPlugins.onError(new IllegalStateException("More produced than requested: " + update));
            update = 0L;
        }
        if (requested.compareAndSet(current, update)) {
            return update;
        }
    }
}
 
Example 4
Project: openjdk-jdk10   File: Test7009231.java   View Source Code Vote up 5 votes
protected void setBit(AtomicLong atomic, long lMask) {
    long lWord;
    do {
        lWord = atomic.get();
    } while (!atomic.compareAndSet(lWord, lWord | lMask));

    if ((atomic.get() & lMask) == 0L) {
        throw new InternalError();
    }
}
 
Example 5
Project: incubator-netbeans   File: TerminalPinSupport.java   View Source Code Vote up 5 votes
private boolean setIfGreater(AtomicLong al, long setTo) {
while (true) {
    long current = al.get();
    if (setTo > current) {
	if (al.compareAndSet(current, setTo)) {
	    return true;
	}
    } else {
	return false;
    }
}
   }
 
Example 6
Project: Hitalk   File: ExampleUnitTest.java   View Source Code Vote up 5 votes
public static long getAndAddRequest(AtomicLong requested, long n) {
    // add n to field but check for overflow
    while (true) {
        long current = requested.get();
        long next = addCap(current, n);
        if (requested.compareAndSet(current, next)) {
            return current;
        }
    }
}
 
Example 7
Project: rocketmq-rocketmq-all-4.1.0-incubating   File: Consumer.java   View Source Code Vote up 5 votes
public static void compareAndSetMax(final AtomicLong target, final long value) {
    long prev = target.get();
    while (value > prev) {
        boolean updated = target.compareAndSet(prev, value);
        if (updated)
            break;

        prev = target.get();
    }
}
 
Example 8
Project: rocketmq-rocketmq-all-4.1.0-incubating   File: MixAll.java   View Source Code Vote up 5 votes
public static boolean compareAndIncreaseOnly(final AtomicLong target, final long value) {
    long prev = target.get();
    while (value > prev) {
        boolean updated = target.compareAndSet(prev, value);
        if (updated)
            return true;

        prev = target.get();
    }

    return false;
}
 
Example 9
Project: MaxSim   File: Test7009231.java   View Source Code Vote up 5 votes
protected void clearBit(AtomicLong atomic, long lMask) {
    long lWord;
    do {
        lWord = atomic.get();
    } while (!atomic.compareAndSet(lWord, lWord & ~lMask));

    if ((atomic.get() & lMask) != 0L) {
        throw new InternalError();
    }
}
 
Example 10
Project: reading-and-annotate-rocketmq-3.4.6   File: Consumer.java   View Source Code Vote up 5 votes
public static void compareAndSetMax(final AtomicLong target, final long value) {
    long prev = target.get();
    while (value > prev) {
        boolean updated = target.compareAndSet(prev, value);
        if (updated)
            break;

        prev = target.get();
    }
}
 
Example 11
Project: RxJava3-preview   File: BackpressureHelper.java   View Source Code Vote up 5 votes
/**
 * Atomically adds the positive value n to the requested value in the AtomicLong and
 * caps the result at Long.MAX_VALUE and returns the previous value.
 * @param requested the AtomicLong holding the current requested value
 * @param n the value to add, must be positive (not verified)
 * @return the original value before the add
 */
public static long add(AtomicLong requested, long n) {
    for (;;) {
        long r = requested.get();
        if (r == Long.MAX_VALUE) {
            return Long.MAX_VALUE;
        }
        long u = addCap(r, n);
        if (requested.compareAndSet(r, u)) {
            return r;
        }
    }
}
 
Example 12
Project: RxJava3-preview   File: QueueDrainHelper.java   View Source Code Vote up 5 votes
/**
 * Accumulates requests (not validated) and handles the completed mode draining of the queue based on the requests.
 *
 * <p>
 * Post-completion backpressure handles the case when a source produces values based on
 * requests when it is active but more values are available even after its completion.
 * In this case, the onComplete() can't just emit the contents of the queue but has to
 * coordinate with the requested amounts. This requires two distinct modes: active and
 * completed. In active mode, requests flow through and the queue is not accessed but
 * in completed mode, requests no-longer reach the upstream but help in draining the queue.
 *
 * @param <T> the value type emitted
 * @param n the request amount, positive (not validated)
 * @param actual the target Subscriber to send events to
 * @param queue the queue to drain if in the post-complete state
 * @param state holds the request amount and the post-completed flag
 * @param isCancelled a supplier that returns true if the drain has been cancelled
 * @return true if the state indicates a completion state.
 */
public static <T> boolean postCompleteRequest(long n,
                                              Subscriber<? super T> actual,
                                              Queue<T> queue,
                                              AtomicLong state,
                                              BooleanSupplier isCancelled) {
    for (; ; ) {
        long r = state.get();

        // extract the current request amount
        long r0 = r & REQUESTED_MASK;

        // preserve COMPLETED_MASK and calculate new requested amount
        long u = (r & COMPLETED_MASK) | BackpressureHelper.addCap(r0, n);

        if (state.compareAndSet(r, u)) {
            // (complete, 0) -> (complete, n) transition then replay
            if (r == COMPLETED_MASK) {

                postCompleteDrain(n | COMPLETED_MASK, actual, queue, state, isCancelled);

                return true;
            }
            // (active, r) -> (active, r + n) transition then continue with requesting from upstream
            return false;
        }
    }

}
 
Example 13
Project: boohee_v5.6   File: OperatorMaterialize.java   View Source Code Vote up 5 votes
private void decrementRequested() {
    AtomicLong localRequested = this.requested;
    long r;
    do {
        r = localRequested.get();
        if (r == Long.MAX_VALUE) {
            return;
        }
    } while (!localRequested.compareAndSet(r, r - 1));
}
 
Example 14
Project: master-basic   File: IRandom.java   View Source Code Vote up 5 votes
public IRandom() {
	AtomicLong seedUniquifier = new AtomicLong(8682522807148012L);
	long next = 0;
	for (;;) {
		long current = seedUniquifier.get();
		next = current * 181783497276652981L;
		if (seedUniquifier.compareAndSet(current, next)){
			this.seed = new AtomicLong(next ^ System.nanoTime());
			return;
		}
	}
}
 
Example 15
Project: master-basic   File: IRandom.java   View Source Code Vote up 5 votes
private int next() {
	long oldseed, nextseed;
	AtomicLong seed = this.seed;
	do {
		oldseed = seed.get();
		nextseed = (oldseed * multiplier + addend) & mask;
	} while (!seed.compareAndSet(oldseed, nextseed));
	return (int) (nextseed >>> (48 - bits));
}
 
Example 16
Project: openjdk-jdk10   File: Test7009231.java   View Source Code Vote up 5 votes
protected void clearBit(AtomicLong atomic, long lMask) {
    long lWord;
    do {
        lWord = atomic.get();
    } while (!atomic.compareAndSet(lWord, lWord & ~lMask));

    if ((atomic.get() & lMask) != 0L) {
        throw new InternalError();
    }
}
 
Example 17
Project: rmq4note   File: MixAll.java   View Source Code Vote up 5 votes
public static boolean compareAndIncreaseOnly(final AtomicLong target, final long value) {
    long prev = target.get();
    while (value > prev) {
        boolean updated = target.compareAndSet(prev, value);
        if (updated)
            return true;

        prev = target.get();
    }

    return false;
}
 
Example 18
Project: monarch   File: IndexManager.java   View Source Code Vote up 5 votes
/**
 * 
 * @param value
 * @param newValue
 */
private static boolean setNewLargestValue(AtomicLong value, long newValue) {
  boolean done = false;
  while (!done) {
    long oldValue = value.get();
    if (oldValue < newValue) {
      return value.compareAndSet(oldValue, newValue);
    } else {
      done = true;
    }
  }
  return false;
}
 
Example 19
Project: openjdk-jdk10   File: Random.java   View Source Code Vote up 3 votes
/**
 * Generates the next pseudorandom number. Subclasses should
 * override this, as this is used by all other methods.
 *
 * <p>The general contract of {@code next} is that it returns an
 * {@code int} value and if the argument {@code bits} is between
 * {@code 1} and {@code 32} (inclusive), then that many low-order
 * bits of the returned value will be (approximately) independently
 * chosen bit values, each of which is (approximately) equally
 * likely to be {@code 0} or {@code 1}. The method {@code next} is
 * implemented by class {@code Random} by atomically updating the seed to
 *  <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
 * and returning
 *  <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
 *
 * This is a linear congruential pseudorandom number generator, as
 * defined by D. H. Lehmer and described by Donald E. Knuth in
 * <i>The Art of Computer Programming,</i> Volume 2:
 * <i>Seminumerical Algorithms</i>, section 3.2.1.
 *
 * @param  bits random bits
 * @return the next pseudorandom value from this random number
 *         generator's sequence
 * @since  1.1
 */
protected int next(int bits) {
    long oldseed, nextseed;
    AtomicLong seed = this.seed;
    do {
        oldseed = seed.get();
        nextseed = (oldseed * multiplier + addend) & mask;
    } while (!seed.compareAndSet(oldseed, nextseed));
    return (int)(nextseed >>> (48 - bits));
}
 
Example 20
Project: OpenJSharp   File: Random.java   View Source Code Vote up 3 votes
/**
 * Generates the next pseudorandom number. Subclasses should
 * override this, as this is used by all other methods.
 *
 * <p>The general contract of {@code next} is that it returns an
 * {@code int} value and if the argument {@code bits} is between
 * {@code 1} and {@code 32} (inclusive), then that many low-order
 * bits of the returned value will be (approximately) independently
 * chosen bit values, each of which is (approximately) equally
 * likely to be {@code 0} or {@code 1}. The method {@code next} is
 * implemented by class {@code Random} by atomically updating the seed to
 *  <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre>
 * and returning
 *  <pre>{@code (int)(seed >>> (48 - bits))}.</pre>
 *
 * This is a linear congruential pseudorandom number generator, as
 * defined by D. H. Lehmer and described by Donald E. Knuth in
 * <i>The Art of Computer Programming,</i> Volume 3:
 * <i>Seminumerical Algorithms</i>, section 3.2.1.
 *
 * @param  bits random bits
 * @return the next pseudorandom value from this random number
 *         generator's sequence
 * @since  1.1
 */
protected int next(int bits) {
    long oldseed, nextseed;
    AtomicLong seed = this.seed;
    do {
        oldseed = seed.get();
        nextseed = (oldseed * multiplier + addend) & mask;
    } while (!seed.compareAndSet(oldseed, nextseed));
    return (int)(nextseed >>> (48 - bits));
}