Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a91643ea authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Simplify getting the first element in a queue if present" into main

parents 655b4991 c0926998
Loading
Loading
Loading
Loading
+15 −37
Original line number Original line Diff line number Diff line
@@ -296,23 +296,13 @@ public final class MessageQueue {
            return false;
            return false;
        }
        }


        MessageNode msgNode = null;
        final MessageNode msgNode = first(mPriorityQueue);
        MessageNode asyncMsgNode = null;
        if (msgNode != null && msgNode.getWhen() <= now) {

            return false;
        if (!mPriorityQueue.isEmpty()) {
            try {
                msgNode = mPriorityQueue.first();
            } catch (NoSuchElementException e) { }
        }

        if (!mAsyncPriorityQueue.isEmpty()) {
            try {
                asyncMsgNode = mAsyncPriorityQueue.first();
            } catch (NoSuchElementException e) { }
        }
        }


        if ((msgNode != null && msgNode.getWhen() <= now)
        final MessageNode asyncMsgNode = first(mAsyncPriorityQueue);
                || (asyncMsgNode != null && asyncMsgNode.getWhen() <= now)) {
        if (asyncMsgNode != null && asyncMsgNode.getWhen() <= now) {
            return false;
            return false;
        }
        }


@@ -730,10 +720,8 @@ public final class MessageQueue {
             */
             */


            /* Get the first node from each queue */
            /* Get the first node from each queue */
            Iterator<MessageNode> queueIter = mPriorityQueue.iterator();
            MessageNode msgNode = first(mPriorityQueue);
            MessageNode msgNode = iterateNext(queueIter);
            MessageNode asyncMsgNode = first(mAsyncPriorityQueue);
            Iterator<MessageNode> asyncQueueIter = mAsyncPriorityQueue.iterator();
            MessageNode asyncMsgNode = iterateNext(asyncQueueIter);


            if (DEBUG) {
            if (DEBUG) {
                if (msgNode != null) {
                if (msgNode != null) {
@@ -1260,16 +1248,10 @@ public final class MessageQueue {


    private void removeSyncBarrierConcurrent(int token) {
    private void removeSyncBarrierConcurrent(int token) {
        boolean removed;
        boolean removed;
        MessageNode first;
        final MatchBarrierToken matchBarrierToken = new MatchBarrierToken(token);
        final MatchBarrierToken matchBarrierToken = new MatchBarrierToken(token);


        try {
        // Retain the first element to see if we are currently stuck on a barrier.
            /* Retain the first element to see if we are currently stuck on a barrier. */
        final MessageNode first = first(mPriorityQueue);
            first = mPriorityQueue.first();
        } catch (NoSuchElementException e) {
            /* The queue is empty */
            first = null;
        }


        removed = findOrRemoveMessages(null, 0, null, null, 0, matchBarrierToken, true);
        removed = findOrRemoveMessages(null, 0, null, null, 0, matchBarrierToken, true);
        if (removed && first != null) {
        if (removed && first != null) {
@@ -1535,8 +1517,7 @@ public final class MessageQueue {
            // Call nextMessage to get the stack drained into our priority queues
            // Call nextMessage to get the stack drained into our priority queues
            nextMessage(true, false);
            nextMessage(true, false);


            Iterator<MessageNode> queueIter = mPriorityQueue.iterator();
            MessageNode queueNode = first(mPriorityQueue);
            MessageNode queueNode = iterateNext(queueIter);


            return (queueNode != null && queueNode.isBarrier());
            return (queueNode != null && queueNode.isBarrier());
        } else {
        } else {
@@ -2447,16 +2428,13 @@ public final class MessageQueue {
        return nodeA != null ? nodeA : nodeB;
        return nodeA != null ? nodeA : nodeB;
    }
    }


    private MessageNode iterateNext(Iterator<MessageNode> iter) {
    private static MessageNode first(ConcurrentSkipListSet<MessageNode> queue) {
        if (iter.hasNext()) {
        try {
        try {
                return iter.next();
            return queue.first();
        } catch (NoSuchElementException e) {
        } catch (NoSuchElementException e) {
                /* The queue is empty - this can happen if we race with remove */
            }
        }
            return null;
            return null;
        }
        }
    }


    /* Move any non-cancelled messages into the priority queue */
    /* Move any non-cancelled messages into the priority queue */
    private void drainStack(StackNode oldTop) {
    private void drainStack(StackNode oldTop) {