/** * @param ptr Pointer. * @return Long value. */ public long readLongVolatile(long ptr) { return GridUnsafe.getLongVolatile(null, ptr); }
/** * @param lock Lock to check. * @return {@code True} if at least one read lock is held by any thread for the given offheap RW lock. */ public boolean isReadLocked(long lock) { return lockCount(GridUnsafe.getLongVolatile(null, lock)) > 0; }
/** * @param lock Lock to check. * @return {@code True} if write lock is held by any thread for the given offheap RW lock. */ public boolean isWriteLocked(long lock) { return lockCount(GridUnsafe.getLongVolatile(null, lock)) == -1; }
/** * Updates readers wait count. * * @param lock Lock to update. * @param delta Delta to update. */ private void updateReadersWaitCount(long lock, ReentrantLock lockObj, int delta) { assert lockObj.isHeldByCurrentThread(); while (true) { // Safe to do non-volatile read because of CAS below. long state = GridUnsafe.getLongVolatile(null, lock); long updated = updateState(state, 0, delta, 0); if (GridUnsafe.compareAndSwapLong(null, lock, state, updated)) return; } }
/** * Updates writers wait count. * * @param lock Lock to update. * @param delta Delta to update. */ private void updateWritersWaitCount(long lock, ReentrantLock lockObj, int delta) { assert lockObj.isHeldByCurrentThread(); while (true) { long state = GridUnsafe.getLongVolatile(null, lock); long updated = updateState(state, 0, 0, delta); if (GridUnsafe.compareAndSwapLong(null, lock, state, updated)) return; } } }
/** * @param lock Lock address. */ public boolean tryWriteLock(long lock, int tag) { long state = GridUnsafe.getLongVolatile(null, lock); return checkTag(state, tag) && canWriteLock(state) && GridUnsafe.compareAndSwapLong(null, lock, state, updateState(state, -1, 0, 0)); }
long state = GridUnsafe.getLongVolatile(null, lock); state = GridUnsafe.getLongVolatile(null, lock);
/** * @param lock Lock address. */ public void readUnlock(long lock) { while (true) { long state = GridUnsafe.getLongVolatile(null, lock); if (lockCount(state) <= 0) throw new IllegalMonitorStateException("Attempted to release a read lock while not holding it " + "[lock=" + U.hexLong(lock) + ", state=" + U.hexLong(state) + ']'); long updated = updateState(state, -1, 0, 0); assert updated != 0; if (GridUnsafe.compareAndSwapLong(null, lock, state, updated)) { // Notify monitor if we were CASed to zero and there is a write waiter. if (lockCount(updated) == 0 && writersWaitCount(updated) > 0) { int idx = lockIndex(lock); ReentrantLock lockObj = locks[idx]; lockObj.lock(); try { // Note that we signal all waiters for this stripe. Since not all waiters in this // stripe/index belong to this particular lock, we can't wake up just one of them. writeConditions[idx].signalAll(); } finally { lockObj.unlock(); } } return; } } }
/** * @param lock Lock address. */ public boolean writeLock(long lock, int tag) { assert tag != 0; for (int i = 0; i < SPIN_CNT; i++) { long state = GridUnsafe.getLongVolatile(null, lock); assert state != 0; if (!checkTag(state, tag)) return false; if (canWriteLock(state)) { if (GridUnsafe.compareAndSwapLong(null, lock, state, updateState(state, -1, 0, 0))) return true; else // Retry CAS, do not count as spin cycle. i--; } } int idx = lockIndex(lock); ReentrantLock lockObj = locks[idx]; lockObj.lock(); try { updateWritersWaitCount(lock, lockObj, 1); return waitAcquireWriteLock(lock, idx, tag); } finally { lockObj.unlock(); } }
/** * @param lock Lock address. */ public void writeUnlock(long lock, int tag) { long updated; assert tag != 0; while (true) { long state = GridUnsafe.getLongVolatile(null, lock); if (lockCount(state) != -1) throw new IllegalMonitorStateException("Attempted to release write lock while not holding it " + "[lock=" + U.hexLong(lock) + ", state=" + U.hexLong(state) + ']'); updated = releaseWithTag(state, tag); assert updated != 0; if (GridUnsafe.compareAndSwapLong(null, lock, state, updated)) break; } int writeWaitCnt = writersWaitCount(updated); int readWaitCnt = readersWaitCount(updated); if (writeWaitCnt > 0 || readWaitCnt > 0) { int idx = lockIndex(lock); ReentrantLock lockObj = locks[idx]; lockObj.lock(); try { signalNextWaiter(writeWaitCnt, readWaitCnt, idx); } finally { lockObj.unlock(); } } }
/** * @param tag Tag to initialize RW lock. * @return Relative pointer of the allocated page. * @throws GridOffHeapOutOfMemoryException If failed to allocate. */ private long allocateFreePage(int tag) throws GridOffHeapOutOfMemoryException { long limit = region.address() + region.size(); while (true) { long lastIdx = GridUnsafe.getLongVolatile(null, lastAllocatedIdxPtr); // Check if we have enough space to allocate a page. if (pagesBase + (lastIdx + 1) * sysPageSize > limit) return INVALID_REL_PTR; if (GridUnsafe.compareAndSwapLong(null, lastAllocatedIdxPtr, lastIdx, lastIdx + 1)) { long absPtr = pagesBase + lastIdx * sysPageSize; assert lastIdx <= PageIdUtils.MAX_PAGE_NUM : lastIdx; long pageIdx = fromSegmentIndex(idx, lastIdx); assert pageIdx != INVALID_REL_PTR; writePageId(absPtr, pageIdx); GridUnsafe.putLong(absPtr, PAGE_MARKER); rwLock.init(absPtr + LOCK_OFFSET, tag); allocatedPages.incrementAndGet(); memMetrics.updateTotalAllocatedPages(1L); return pageIdx; } } }
while (true) { try { long state = GridUnsafe.getLongVolatile(null, lock);
while (true) { try { long state = GridUnsafe.getLongVolatile(null, lock);
long state = GridUnsafe.getLongVolatile(null, lock); long state = GridUnsafe.getLongVolatile(null, lock);
/** * @param ptr Pointer. * @return Long value. */ public long readLongVolatile(long ptr) { return GridUnsafe.getLongVolatile(null, ptr); }
/** * @param lock Lock to check. * @return {@code True} if at least one read lock is held by any thread for the given offheap RW lock. */ public boolean isReadLocked(long lock) { return lockCount(GridUnsafe.getLongVolatile(null, lock)) > 0; }
/** * @param lock Lock to check. * @return {@code True} if write lock is held by any thread for the given offheap RW lock. */ public boolean isWriteLocked(long lock) { return lockCount(GridUnsafe.getLongVolatile(null, lock)) == -1; }
/** * Updates readers wait count. * * @param lock Lock to update. * @param delta Delta to update. */ private void updateReadersWaitCount(long lock, ReentrantLock lockObj, int delta) { assert lockObj.isHeldByCurrentThread(); while (true) { // Safe to do non-volatile read because of CAS below. long state = GridUnsafe.getLongVolatile(null, lock); long updated = updateState(state, 0, delta, 0); if (GridUnsafe.compareAndSwapLong(null, lock, state, updated)) return; } }
/** * Updates writers wait count. * * @param lock Lock to update. * @param delta Delta to update. */ private void updateWritersWaitCount(long lock, ReentrantLock lockObj, int delta) { assert lockObj.isHeldByCurrentThread(); while (true) { long state = GridUnsafe.getLongVolatile(null, lock); long updated = updateState(state, 0, 0, delta); if (GridUnsafe.compareAndSwapLong(null, lock, state, updated)) return; } } }
/** * @param lock Lock address. */ public boolean tryWriteLock(long lock, int tag) { long state = GridUnsafe.getLongVolatile(null, lock); return checkTag(state, tag) && canWriteLock(state) && GridUnsafe.compareAndSwapLong(null, lock, state, updateState(state, -1, 0, 0)); }