package com.parse;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import p137.C2739;
import p137.C2742;
import p137.InterfaceC2741;

/* JADX INFO: loaded from: classes.dex */
public class TaskQueue {
    public final Lock lock = new ReentrantLock();
    public C2742<Void> tail;

    public <T> C2742<T> enqueue(InterfaceC2741<Void, C2742<T>> interfaceC2741) {
        this.lock.lock();
        try {
            C2742<Void> c2742M4805 = this.tail != null ? this.tail : C2742.m4805((Object) null);
            try {
                C2742<T> c2742Then = interfaceC2741.then(getTaskToAwait());
                this.tail = C2742.m4801((Collection<? extends C2742<?>>) Arrays.asList(c2742M4805, c2742Then));
                return c2742Then;
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e2) {
                throw new RuntimeException(e2);
            }
        } finally {
            this.lock.unlock();
        }
    }

    public final C2742<Void> getTaskToAwait() {
        this.lock.lock();
        try {
            return (this.tail != null ? this.tail : C2742.m4805((Object) null)).m4809((InterfaceC2741<Void, TContinuationResult>) new InterfaceC2741<Void, Void>(this) { // from class: com.parse.TaskQueue.2
                @Override // p137.InterfaceC2741
                public Void then(C2742<Void> c2742) {
                    return null;
                }
            }, C2742.f8455, (C2739) null);
        } finally {
            this.lock.unlock();
        }
    }
}
