public Runnable runIfNeeded()

in tcbot-persistence/src/main/java/org/apache/ignite/tcbot/persistence/scheduler/NamedTask.java [80:136]


    public Runnable runIfNeeded() throws Exception {
        long optReadStamp = lock.tryOptimisticRead();
        boolean canSkip = canSkipStartNow();
        boolean optRead = lock.validate(optReadStamp);

        if (optRead) {
            if (canSkip)
                return null;
        }
        else {
            long readStamp = lock.readLock();
            boolean canSkipStartNow;

            try {
                canSkipStartNow = canSkipStartNow();
            }
            finally {
                lock.unlockRead(readStamp);
            }

            if (canSkipStartNow)
                return null;
        }

        Runnable cmd;
        long writeLockStamp = lock.writeLock();
        try {
            cmd = this.cmd;
            this.cmd = null;

            // because here lock is not upgraded from read lock cmd may come here with null
            if (cmd != null)
                status = Status.RUNNING;
        }
        finally {
            lock.unlock(writeLockStamp);
        }

        if (cmd == null)
            return null;

        try {
            cmd.run();
        }
        finally {
            long writeLockStamp2 = lock.writeLock();
            try {
                lastFinishedTs = System.currentTimeMillis();
                status = Status.COMPLETED;
            }
            finally {
                lock.unlock(writeLockStamp2);
            }
        }

        return cmd;
    }