package com.tencent.map.lib.thread;

import com.tencent.matrix.trace.core.AppMethodBeat;
import java.io.Serializable;
import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

class HighThreadExecutor$a<E> extends AbstractQueue<E> implements Serializable, BlockingQueue<E> {
    transient b<E> a;
    final /* synthetic */ HighThreadExecutor b;
    private int c;
    private final AtomicInteger d = new AtomicInteger();
    private final ReentrantLock e = new ReentrantLock();
    private final Condition f = this.e.newCondition();
    private final ReentrantLock g = new ReentrantLock();
    private final Condition h = this.g.newCondition();

    class b<E> {
        E a;
        b<E> b;

        b(E e) {
            this.a = e;
        }
    }

    public HighThreadExecutor$a(HighThreadExecutor highThreadExecutor, int i) {
        this.b = highThreadExecutor;
        AppMethodBeat.i(98182);
        this.c = i;
        this.a = new b(null);
        AppMethodBeat.o(98182);
    }

    public Iterator<E> iterator() {
        AppMethodBeat.i(98183);
        a aVar = new a(this);
        AppMethodBeat.o(98183);
        return aVar;
    }

    public int size() {
        AppMethodBeat.i(98184);
        int i = this.d.get();
        AppMethodBeat.o(98184);
        return i;
    }

    public int drainTo(Collection<? super E> collection) {
        AppMethodBeat.i(98185);
        int drainTo = drainTo(collection, Integer.MAX_VALUE);
        AppMethodBeat.o(98185);
        return drainTo;
    }

    public int drainTo(Collection<? super E> collection, int i) {
        int i2 = 1;
        AppMethodBeat.i(98186);
        if (collection == null) {
            NullPointerException nullPointerException = new NullPointerException();
            AppMethodBeat.o(98186);
            throw nullPointerException;
        } else if (collection == this) {
            IllegalArgumentException illegalArgumentException = new IllegalArgumentException();
            AppMethodBeat.o(98186);
            throw illegalArgumentException;
        } else if (i <= 0) {
            AppMethodBeat.o(98186);
            return 0;
        } else {
            ReentrantLock reentrantLock = this.e;
            reentrantLock.lock();
            b bVar;
            int i3;
            try {
                int min = Math.min(i, this.d.get());
                bVar = this.a;
                i3 = 0;
                while (i3 < min) {
                    b bVar2 = bVar.b;
                    collection.add(bVar2.a);
                    bVar2.a = null;
                    bVar.b = bVar;
                    i3++;
                    bVar = bVar2;
                }
                if (i3 > 0) {
                    this.a = bVar;
                    if (this.d.getAndAdd(-i3) != this.c) {
                        i2 = 0;
                    }
                } else {
                    i2 = 0;
                }
                reentrantLock.unlock();
                if (i2 != 0) {
                    d();
                }
                AppMethodBeat.o(98186);
                return min;
            } catch (Throwable th) {
                reentrantLock.unlock();
                if (null != null) {
                    d();
                }
                AppMethodBeat.o(98186);
            }
        }
    }

    public boolean offer(E e) {
        AppMethodBeat.i(98187);
        if (e == null) {
            NullPointerException nullPointerException = new NullPointerException();
            AppMethodBeat.o(98187);
            throw nullPointerException;
        }
        AtomicInteger atomicInteger = this.d;
        if (atomicInteger.get() == this.c) {
            AppMethodBeat.o(98187);
            return false;
        }
        int i = -1;
        b bVar = new b(e);
        ReentrantLock reentrantLock = this.g;
        reentrantLock.lock();
        try {
            if (atomicInteger.get() < this.c) {
                a(bVar);
                i = atomicInteger.getAndIncrement();
                if (i + 1 < this.c) {
                    this.h.signal();
                }
            }
            reentrantLock.unlock();
            if (i == 0) {
                e();
            }
            if (i >= 0) {
                AppMethodBeat.o(98187);
                return true;
            }
            AppMethodBeat.o(98187);
            return false;
        } catch (Throwable th) {
            reentrantLock.unlock();
            AppMethodBeat.o(98187);
        }
    }

    public boolean offer(E e, long j, TimeUnit timeUnit) {
        AppMethodBeat.i(98188);
        if (e == null) {
            NullPointerException nullPointerException = new NullPointerException();
            AppMethodBeat.o(98188);
            throw nullPointerException;
        }
        long toNanos = timeUnit.toNanos(j);
        ReentrantLock reentrantLock = this.g;
        AtomicInteger atomicInteger = this.d;
        reentrantLock.lockInterruptibly();
        while (atomicInteger.get() == this.c) {
            try {
                if (toNanos <= 0) {
                    reentrantLock.unlock();
                    AppMethodBeat.o(98188);
                    return false;
                }
                toNanos = this.h.awaitNanos(toNanos);
            } catch (Throwable th) {
                reentrantLock.unlock();
                AppMethodBeat.o(98188);
            }
        }
        a(new b(e));
        int andIncrement = atomicInteger.getAndIncrement();
        if (andIncrement + 1 < this.c) {
            this.h.signal();
        }
        reentrantLock.unlock();
        if (andIncrement == 0) {
            e();
        }
        AppMethodBeat.o(98188);
        return true;
    }

    public E poll(long j, TimeUnit timeUnit) {
        AppMethodBeat.i(98189);
        long toNanos = timeUnit.toNanos(j);
        AtomicInteger atomicInteger = this.d;
        ReentrantLock reentrantLock = this.e;
        reentrantLock.lockInterruptibly();
        while (atomicInteger.get() == 0) {
            try {
                if (toNanos <= 0) {
                    reentrantLock.unlock();
                    AppMethodBeat.o(98189);
                    return null;
                }
                toNanos = this.f.awaitNanos(toNanos);
            } catch (Throwable th) {
                reentrantLock.unlock();
                AppMethodBeat.o(98189);
            }
        }
        E b = b();
        int andDecrement = atomicInteger.getAndDecrement();
        if (andDecrement > 1) {
            this.f.signal();
        }
        reentrantLock.unlock();
        if (andDecrement == this.c) {
            d();
        }
        AppMethodBeat.o(98189);
        return b;
    }

    public void put(E e) {
        AppMethodBeat.i(98190);
        if (e == null) {
            NullPointerException nullPointerException = new NullPointerException();
            AppMethodBeat.o(98190);
            throw nullPointerException;
        }
        b bVar = new b(e);
        ReentrantLock reentrantLock = this.g;
        AtomicInteger atomicInteger = this.d;
        reentrantLock.lockInterruptibly();
        while (atomicInteger.get() == this.c) {
            try {
                this.h.await();
            } catch (Throwable th) {
                reentrantLock.unlock();
                AppMethodBeat.o(98190);
            }
        }
        a(bVar);
        int andIncrement = atomicInteger.getAndIncrement();
        if (andIncrement + 1 < this.c) {
            this.h.signal();
        }
        reentrantLock.unlock();
        if (andIncrement == 0) {
            e();
        }
        AppMethodBeat.o(98190);
    }

    public int remainingCapacity() {
        AppMethodBeat.i(98191);
        int i = this.c - this.d.get();
        AppMethodBeat.o(98191);
        return i;
    }

    public E take() {
        AppMethodBeat.i(98192);
        AtomicInteger atomicInteger = this.d;
        ReentrantLock reentrantLock = this.e;
        reentrantLock.lockInterruptibly();
        while (atomicInteger.get() == 0) {
            try {
                this.f.await();
            } catch (Throwable th) {
                reentrantLock.unlock();
                AppMethodBeat.o(98192);
            }
        }
        Object b = b();
        int andDecrement = atomicInteger.getAndDecrement();
        if (andDecrement > 1) {
            this.f.signal();
        }
        reentrantLock.unlock();
        if (andDecrement == this.c) {
            d();
        }
        AppMethodBeat.o(98192);
        return b;
    }

    public E peek() {
        E e = null;
        AppMethodBeat.i(98193);
        if (this.d.get() == 0) {
            AppMethodBeat.o(98193);
        } else {
            ReentrantLock reentrantLock = this.e;
            reentrantLock.lock();
            try {
                b bVar = this.a.b;
                if (bVar != null) {
                    e = bVar.a;
                    reentrantLock.unlock();
                    AppMethodBeat.o(98193);
                }
            } finally {
                reentrantLock.unlock();
                AppMethodBeat.o(98193);
            }
        }
        return e;
    }

    public E poll() {
        E e = null;
        AppMethodBeat.i(98194);
        AtomicInteger atomicInteger = this.d;
        if (atomicInteger.get() == 0) {
            AppMethodBeat.o(98194);
        } else {
            int i = -1;
            ReentrantLock reentrantLock = this.e;
            reentrantLock.lock();
            try {
                if (atomicInteger.get() > 0) {
                    e = b();
                    i = atomicInteger.getAndDecrement();
                    if (i > 1) {
                        this.f.signal();
                    }
                }
                reentrantLock.unlock();
                if (i == this.c) {
                    d();
                }
                AppMethodBeat.o(98194);
            } catch (Throwable th) {
                reentrantLock.unlock();
                AppMethodBeat.o(98194);
            }
        }
        return e;
    }

    private void a(b<E> bVar) {
        bVar.b = this.a.b;
        this.a.b = bVar;
    }

    private E b() {
        b bVar = this.a.b;
        if (bVar == null) {
            return null;
        }
        this.a.b = bVar.b;
        bVar.b = null;
        Object obj = bVar.a;
        bVar.a = null;
        return obj;
    }

    private void c() {
        AppMethodBeat.i(98195);
        this.g.lock();
        this.e.lock();
        AppMethodBeat.o(98195);
    }

    /* Access modifiers changed, original: 0000 */
    public void a() {
        AppMethodBeat.i(98196);
        this.e.unlock();
        this.g.unlock();
        AppMethodBeat.o(98196);
    }

    /* Access modifiers changed, original: 0000 */
    public void a(b<E> bVar, b<E> bVar2) {
        AppMethodBeat.i(98197);
        bVar.a = null;
        bVar2.b = bVar.b;
        if (this.d.getAndDecrement() == this.c) {
            this.h.signal();
        }
        AppMethodBeat.o(98197);
    }

    private void d() {
        AppMethodBeat.i(98198);
        ReentrantLock reentrantLock = this.g;
        reentrantLock.lock();
        try {
            this.h.signal();
        } finally {
            reentrantLock.unlock();
            AppMethodBeat.o(98198);
        }
    }

    private void e() {
        AppMethodBeat.i(98199);
        ReentrantLock reentrantLock = this.e;
        reentrantLock.lock();
        try {
            this.f.signal();
        } finally {
            reentrantLock.unlock();
            AppMethodBeat.o(98199);
        }
    }
}
