use of java.util.concurrent.atomic.AtomicMarkableReference in project hpcourse by cscenter.
the class LockFreeSetImpl method add.
public boolean add(T item) {
while (true) {
Pair pair_lc = search(item);
Node left = pair_lc.left;
Node curr = pair_lc.curr;
if (curr.item != null && curr.item.compareTo(item) == 0)
return false;
else {
Node node = new Node(item);
node.next = new AtomicMarkableReference(curr, false);
if (left.next.compareAndSet(curr, node, false, false))
return true;
}
}
}
use of java.util.concurrent.atomic.AtomicMarkableReference in project j2objc by google.
the class AtomicMarkableReferenceTest method testCompareAndSetInMultipleThreads.
/**
* compareAndSet in one thread enables another waiting for reference value
* to succeed
*/
public void testCompareAndSetInMultipleThreads() throws Exception {
final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!ai.compareAndSet(two, three, false, false)) Thread.yield();
}
});
t.start();
assertTrue(ai.compareAndSet(one, two, false, false));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertSame(three, ai.getReference());
assertFalse(ai.isMarked());
}
use of java.util.concurrent.atomic.AtomicMarkableReference in project j2objc by google.
the class AtomicMarkableReferenceTest method testConstructor.
// android-note: Removed because the CTS runner does a bad job of
// retrying tests that have suite() declarations.
//
// public static void main(String[] args) {
// main(suite(), args);
// }
// public static Test suite() {
// return new TestSuite(AtomicMarkableReferenceTest.class);
// }
/**
* constructor initializes to given reference and mark
*/
public void testConstructor() {
AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
assertSame(one, ai.getReference());
assertFalse(ai.isMarked());
AtomicMarkableReference a2 = new AtomicMarkableReference(null, true);
assertNull(a2.getReference());
assertTrue(a2.isMarked());
}
use of java.util.concurrent.atomic.AtomicMarkableReference in project j2objc by google.
the class AtomicMarkableReferenceTest method testWeakCompareAndSet.
/**
* repeated weakCompareAndSet succeeds in changing values when equal
* to expected
*/
public void testWeakCompareAndSet() {
boolean[] mark = new boolean[1];
AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
assertSame(one, ai.get(mark));
assertFalse(ai.isMarked());
assertFalse(mark[0]);
do {
} while (!ai.weakCompareAndSet(one, two, false, false));
assertSame(two, ai.get(mark));
assertFalse(mark[0]);
do {
} while (!ai.weakCompareAndSet(two, m3, false, true));
assertSame(m3, ai.get(mark));
assertTrue(mark[0]);
}
use of java.util.concurrent.atomic.AtomicMarkableReference in project j2objc by google.
the class AtomicMarkableReferenceTest method testAttemptMark.
/**
* attemptMark succeeds in single thread
*/
public void testAttemptMark() {
boolean[] mark = new boolean[1];
AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
assertFalse(ai.isMarked());
assertTrue(ai.attemptMark(one, true));
assertTrue(ai.isMarked());
assertSame(one, ai.get(mark));
assertTrue(mark[0]);
}
Aggregations