/** * Sets value for this sync. * * @param x Value. * @param e Exception. */ public void set(@Nullable X x, @Nullable RuntimeException e) { this.x = x; this.e = e; boolean res = compareAndSetState(-1, 0); // Set finish state. assert res; }
/** * Sets value for this sync. * * @param x Value. * @param e Exception. */ public void set(@Nullable X x, @Nullable RuntimeException e) { this.x = x; this.e = e; boolean res = compareAndSetState(-1, 0); // Set finish state. assert res; }
/** * Releases this sync after update. * * @return {@code true} If this sync was flipped and current thread was the last updater. */ public boolean releaseAfterUpdate() { for (;;) { int curr = getState(); assert curr != 0; int next = curr < 0 ? curr + 1 : curr - 1; assert next != 0; if (compareAndSetState(curr, next)) return next == -1; // -1 means that sync was flipped and we are the last updater. } }
/** * Flips this sync so that no subsequent updates can happen. * * @return {@code true} If there are no updaters currently in progress to wait. */ public boolean flip() { for (;;) { int curr = getState(); if (curr <= 0) return false; if (compareAndSetState(curr, -curr)) return curr == 1; // 1 means no active updaters, we should take snapshot ourself. } }
/** * Tries to acquire this sync for update. Multiple threads allowed to do update at the same time. * * @return {@code true} If operation succeeded. */ public boolean tryAcquireForUpdate() { for (;;) { int curr = getState(); if (curr <= 0) // We are flipped or finished. return false; if (compareAndSetState(curr, curr + 1)) return true; } }
/** * Tries to acquire this sync for update. Multiple threads allowed to do update at the same time. * * @return {@code true} If operation succeeded. */ public boolean tryAcquireForUpdate() { for (;;) { int curr = getState(); if (curr <= 0) // We are flipped or finished. return false; if (compareAndSetState(curr, curr + 1)) return true; } }
/** * Releases this sync after update. * * @return {@code true} If this sync was flipped and current thread was the last updater. */ public boolean releaseAfterUpdate() { for (;;) { int curr = getState(); assert curr != 0; int next = curr < 0 ? curr + 1 : curr - 1; assert next != 0; if (compareAndSetState(curr, next)) return next == -1; // -1 means that sync was flipped and we are the last updater. } }
/** * Flips this sync so that no subsequent updates can happen. * * @return {@code true} If there are no updaters currently in progress to wait. */ public boolean flip() { for (;;) { int curr = getState(); if (curr <= 0) return false; if (compareAndSetState(curr, -curr)) return curr == 1; // 1 means no active updaters, we should take snapshot ourself. } }