private <T extends Comparable<T>> boolean isLockReady(Procedure<?> proc, Queue<T> rq) { LockStatus s = rq.getLockStatus(); // if we have the lock access, we are ready if (s.hasLockAccess(proc)) { return true; } boolean xlockReq = rq.requireExclusiveLock(proc); // if we need to hold the xlock, then we need to make sure that no one holds any lock, including // the shared lock, otherwise, we just need to make sure that no one holds the xlock return xlockReq ? !s.isLocked() : !s.hasExclusiveLock(); }
private <T extends Comparable<T>> void doAdd(FairQueue<T> fairq, Queue<T> queue, Procedure<?> proc, boolean addFront) { queue.add(proc, addFront); // For the following conditions, we will put the queue back into execution // 1. The procedure has already held the lock, or the lock has been restored when restarting, // which means it can be executed immediately. // 2. The exclusive lock for this queue has not been held. // 3. The given procedure has the exclusive lock permission for this queue. Supplier<String> reason = null; if (proc.hasLock()) { reason = () -> proc + " has lock"; } else if (proc.isLockedWhenLoading()) { reason = () -> proc + " restores lock when restarting"; } else if (!queue.getLockStatus().hasExclusiveLock()) { reason = () -> "the exclusive lock is not held by anyone when adding " + proc; } else if (queue.getLockStatus().hasLockAccess(proc)) { reason = () -> proc + " has the excusive lock access"; } if (reason != null) { addToRunQueue(fairq, queue, reason); } }