/** * Atomically sets the element at position {@code i} to the given * updated value if the current value {@code ==} the expected value. * * <p><a href="package-summary.html#weakCompareAndSet">May fail * spuriously and does not provide ordering guarantees</a>, so is * only rarely an appropriate alternative to {@code compareAndSet}. * * @param i the index * @param expect the expected value * @param update the new value * @return true if successful */ public final boolean weakCompareAndSet(int i, int expect, int update) { return compareAndSet(i, expect, update); }
public int getAndPresetFirstEmptyIndex() { for (int i = 0; i < maxPendingUploadMetrics; i++) { if (metricStat.get(i) == UNSET) { if (metricStat.compareAndSet(i, UNSET, PRE_SET)) { return i; } } } return -1; }
private boolean clearIntDirty(final int idx) { if (!this.intDirty.weakCompareAndSet(idx, 1/* expected */, 0/* update */)) { return this.intDirty.compareAndSet(idx, 1/* expected */, 0/* update */); } return true; }
private boolean clearLongDirty(final int idx) { if (!this.longDirty.weakCompareAndSet(idx, 1/* expected */, 0/* update */)) { return this.longDirty.compareAndSet(idx, 1/* expected */, 0/* update */); } return true; }
private boolean incrementPartitionedSize(int value) { int h = (int) Thread.currentThread().getId(); h ^= (h >>> 18) ^ (h >>> 12); h = (h ^ (h >>> 10)) & SIZE_BUCKETS; if (h != 0) { h = (h - 1) << 4; while (true) { int localSize = this.partitionedSize.get(h); if (this.partitionedSize.compareAndSet(h, localSize, localSize + value)) { return true; } } } return false; }
private boolean incrementPartitionedSize(int value) { int h = (int) Thread.currentThread().getId(); h ^= (h >>> 18) ^ (h >>> 12); h = (h ^ (h >>> 10)) & SIZE_BUCKETS; if (h != 0) { h = (h - 1) << 4; while (true) { int localSize = this.partitionedSize.get(h); if (this.partitionedSize.compareAndSet(h, localSize, localSize + value)) { return true; } } } return false; }
private boolean incrementPartitionedSize(int value) { int h = (int) Thread.currentThread().getId(); h ^= (h >>> 18) ^ (h >>> 12); h = (h ^ (h >>> 10)) & SIZE_BUCKETS; if (h != 0) { h = (h - 1) << 4; while (true) { int localSize = this.partitionedSize.get(h); if (this.partitionedSize.compareAndSet(h, localSize, localSize + value)) { return true; } } } return false; }
@Override protected void forkedProcess( int id, int processors, int[] batch ) throws InterruptedException { int ticket = batch[0]; Thread.sleep( ThreadLocalRandom.current().nextInt( 10 ) ); for ( int i = 1; i < batch.length; i++ ) { if ( batch[i] % processors == id ) { boolean compareAndSet = reference.compareAndSet( batch[i], ticket, ticket + 1 ); assertTrue( "I am " + id + ". Was expecting " + ticket + " for " + batch[i] + " but was " + reference.get( batch[i] ), compareAndSet ); } } } };
@Override public Void call() throws Exception { while(!stop.get()) { final int nodeId = r.nextInt(TOTAL_CNT); if (!reservedIdx.compareAndSet(nodeId, 0, 1)) { yield(); continue; } stopGrid(nodeId); doSleep(500 + r.nextInt(1000)); startGrid(nodeId); reservedIdx.set(nodeId, 0); } return null; } }, 1, "tx-restart-thread");
public class AtomicBitSet { private final AtomicIntegerArray array; public AtomicBitSet(int length) { int intLength = (length + 31) / 32; array = new AtomicIntegerArray(intLength); } public void set(long n) { int bit = 1 << n; int idx = (int) (n >>> 5); while (true) { int num = array.get(idx); int num2 = num | bit; if (num == num2 || array.compareAndSet(idx, num, num2)) return; } } public boolean get(long n) { int bit = 1 << n; int idx = (int) (n >>> 5); int num = array.get(idx); return (num & bit) != 0; } }
return null; while (!locks.compareAndSet(g, 0, -1));
@SuppressWarnings({"BusyWait"}) @Override public Object call() throws Exception { GridRandom rnd = new GridRandom(); while (!restartsDone.get()) { int g; do { g = rnd.nextInt(locks.length()); } while (!locks.compareAndSet(g, 0, -1)); log.info("Stop node: " + g); stopGrid(g); Thread.sleep(rnd.nextInt(nodeLifeTime)); log.info("Start node: " + g); startGrid(g); Thread.sleep(rnd.nextInt(nodeLifeTime)); locks.set(g, 0); int c = restartCnt.incrementAndGet(); if (c % logFreq == 0) info("Node restarts: " + c); } return true; } }, restartThreadsNum, "restart-thread");
@Override public void run() { int key = idGen.getAndIncrement(); List<Integer> keys = new ArrayList<>(); for (int k = 0; k < keysCnt; k++) keys.add(k); int cntr = 0; for (int i = 0; i < ITERATIONS; i++) { cntr++; int nodeId; while(!reservedIdx.compareAndSet((nodeId = r.nextInt(TOTAL_CNT)), 0, 1)) doSleep(10); U.awaitQuiet(b); final IgniteEx grid = grid(nodeId); try (final Transaction tx = grid.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, 0, 0)) { reservedIdx.set(nodeId, 0); // Construct deadlock grid.cache(CACHE_NAME).get(keys.get(key)); // Should block. grid.cache(CACHE_NAME).get(keys.get((key + 1) % keysCnt)); fail("Deadlock expected"); } catch (Throwable t) { // Expected. } if (key == 0) log.info("Rolled back: " + cntr); } } }, keysCnt, "tx-lock-thread");
return; while (!locks.compareAndSet(g, 0, 1));
if (!idx.compareAndSet(nodeId, 0, 1)) { yield();
if(hashCode != -1) { int bucket = hashCode & (hashedToOrdinals.length() - 1); while(!hashedToOrdinals.compareAndSet(bucket, -1, t)) { bucket = (bucket + 1) & (hashedToOrdinals.length() - 1);
private final boolean clearIntDirty(final int idx) { if (!this.intDirty.weakCompareAndSet(idx, 1/*expected*/, 0/*update*/)) { return this.intDirty.compareAndSet(idx, 1/*expected*/, 0/*update*/); } return true; } private final boolean clearLongDirty(final int idx) {
private void setColor(int node, int color) { /* loop until either current is higher or equal to color or color was successfully saved */ int current; do { current = colors.get(node); } while (color >= current && !colors.compareAndSet(node, current, color)); }
private int incrementAndGet (int p, int q) { int val = count[p].get(q); while (! count[p].compareAndSet(q, val, val + 1)) val = count[p].get(q); return val + 1; }
private void addElapse(int slot, int elapse) { int idx = slot * Field.NFIELDS.ordinal() + Field.ELAPSE.ordinal(); for (;;) { int n = this.metrics.get(idx); if (this.metrics.compareAndSet(idx, n, n + elapse)) { break; } } }