/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.work.impl.utils;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import java.util.ArrayDeque;
import java.util.concurrent.Executor;

/**
 * A {@link Executor} which delegates to another {@link Executor} but ensures that tasks are
 * executed serially, like a single threaded executor.
 */
public class SerialExecutor implements Executor {
    private final ArrayDeque<Task> mTasks;
    private final Executor mExecutor;
    private final Object mLock;
    private volatile Runnable mActive;

    public SerialExecutor(@NonNull Executor executor) {
        mExecutor = executor;
        mTasks = new ArrayDeque<>();
        mLock = new Object();
    }

    @Override
    public void execute(@NonNull Runnable command) {
        synchronized (mLock) {
            mTasks.add(new Task(this, command));
            if (mActive == null) {
                scheduleNext();
            }
        }
    }

    // Synthetic access
    void scheduleNext() {
        synchronized (mLock) {
            if ((mActive = mTasks.poll()) != null) {
                mExecutor.execute(mActive);
            }
        }
    }

    /**
     * @return {@code true} if there are tasks to execute in the queue.
     */
    public boolean hasPendingTasks() {
        synchronized (mLock) {
            return !mTasks.isEmpty();
        }
    }

    @NonNull
    @VisibleForTesting
    public Executor getDelegatedExecutor() {
        return mExecutor;
    }

    /**
     * A {@link Runnable} which tells the {@link SerialExecutor} to schedule the next command
     * after completion.
     */
    static class Task implements Runnable {
        final SerialExecutor mSerialExecutor;
        final Runnable mRunnable;

        Task(@NonNull SerialExecutor serialExecutor, @NonNull Runnable runnable) {
            mSerialExecutor = serialExecutor;
            mRunnable = runnable;
        }

        @Override
        public void run() {
            try {
                mRunnable.run();
            } finally {
                mSerialExecutor.scheduleNext();
            }
        }
    }
}

@@ -645,16 +645,54 @@
 utils;%0A%0A
+import androidx.annotation.GuardedBy;%0A
 import a
@@ -719,16 +719,16 @@
 onNull;%0A
-
 import a
@@ -1125,39 +1125,32 @@
 or;%0A
+%0A
     
-private final Object 
+@GuardedBy(%22
 mLock
-;
+%22)
 %0A   
@@ -1162,17 +1162,8 @@
 ate 
-volatile 
 Runn
@@ -1173,24 +1173,49 @@
 e mActive;%0A%0A
+    final Object mLock;%0A%0A
     public S
@@ -1631,16 +1631,40 @@
  access%0A
+    @GuardedBy(%22mLock%22)%0A
     void
@@ -1685,43 +1685,8 @@
 ) %7B%0A
-        synchronized (mLock) %7B%0A    
     
@@ -1738,28 +1738,24 @@
             
-    
 mExecutor.ex
@@ -1770,30 +1770,16 @@
 ctive);%0A
-            %7D%0A
         
@@ -2665,24 +2665,83 @@
 %7D finally %7B%0A
+                synchronized (mSerialExecutor.mLock) %7B%0A    
             
@@ -2768,32 +2768,50 @@
 scheduleNext();%0A
+                %7D%0A
             %7D%0A  

Remove unnecessary volatile and double locking, add @GuardedBy