/** {@inheritDoc} */ @Override protected final boolean tryReleaseShared(int releases) { // Fail-fast path. if(broken) return true; // Check if some other node updated the state. // This method is called with release==0 only when trying to wake through update. if (releases == 0) return true; for (; ; ) { // If broken, return immediately, exception will be thrown anyway. if (broken) return true; int cur = getState(); int next = cur + releases; if (next < cur) // overflow throw new Error("Maximum permit count exceeded"); if (compareAndSetGlobalState(cur, next, false)) return true; } }
/** {@inheritDoc} */ @Override protected final boolean tryReleaseShared(int releases) { // Fail-fast path. if(broken) return true; // Check if some other node updated the state. // This method is called with release==0 only when trying to wake through update. if (releases == 0) return true; for (; ; ) { // If broken, return immediately, exception will be thrown anyway. if (broken) return true; int cur = getState(); int next = cur + releases; if (next < cur) // overflow throw new Error("Maximum permit count exceeded"); if (compareAndSetGlobalState(cur, next, false)) return true; } }
/** * This method is used by the AQS to test if the current thread should block or not. * * @param acquires Number of permits to acquire. * @return Negative number if thread should block, positive if thread successfully acquires permits. */ final int nonfairTryAcquireShared(int acquires) { for (; ; ) { // If broken, return immediately, exception will be thrown anyway. if (broken) return 1; int available = getState(); int remaining = available - acquires; if (remaining < 0 || compareAndSetGlobalState(available, remaining, false)) return remaining; } }
/** * This method is used internally to implement {@linkplain GridCacheSemaphoreImpl#drainPermits()}. * * @return Number of permits to drain. */ final int drainPermits() { for (; ; ) { // If broken, return immediately, exception will be thrown anyway. if (broken) return 1; int curr = getState(); if (curr == 0 || compareAndSetGlobalState(curr, 0, true)) return curr; } }
/** * Gets the number of permissions currently available. * * @return Number of permits available at this semaphore. */ final int getPermits() { return getState(); }
/** * This method is used by the AQS to test if the current thread should block or not. * * @param acquires Number of permits to acquire. * @return Negative number if thread should block, positive if thread successfully acquires permits. */ final int nonfairTryAcquireShared(int acquires) { for (; ; ) { // If broken, return immediately, exception will be thrown anyway. if (broken) return 1; int available = getState(); int remaining = available - acquires; if (remaining < 0 || compareAndSetGlobalState(available, remaining, false)) return remaining; } }
/** * This method is used internally to implement {@linkplain GridCacheSemaphoreImpl#drainPermits()}. * * @return Number of permits to drain. */ final int drainPermits() { for (; ; ) { // If broken, return immediately, exception will be thrown anyway. if (broken) return 1; int curr = getState(); if (curr == 0 || compareAndSetGlobalState(curr, 0, true)) return curr; } }
/** * Gets the number of permissions currently available. * * @return Number of permits available at this semaphore. */ final int getPermits() { return getState(); }