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

Commit b33955b0 authored by Tomasz Mikolajewski's avatar Tomasz Mikolajewski
Browse files

Allow more than one open pipe at once.

This CL fixes that by adding a thread pool of max 8 threads. Trying to
open more file descriptors will block in a queue.

Test: Unit tests.
Bug: 33132744
Change-Id: Ide148e71cbf4948a59efc006285d22b8b9b958da
parent 94d27a80
Loading
Loading
Loading
Loading
+18 −16
Original line number Diff line number Diff line
@@ -58,8 +58,10 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;

/**
@@ -83,7 +85,7 @@ public class Archive implements Closeable {
    private final Uri mArchiveUri;
    private final Uri mNotificationUri;
    private final StrictJarFile mZipFile;
    private final ExecutorService mExecutor;
    private final ThreadPoolExecutor mExecutor;
    private final Map<String, ZipEntry> mEntries;
    private final Map<String, List<ZipEntry>> mTree;

@@ -100,7 +102,12 @@ public class Archive implements Closeable {
        mZipFile = file != null ?
                new StrictJarFile(file.getPath(), false /* verify */, false /* signatures */) :
                new StrictJarFile(fd, false /* verify */, false /* signatures */);
        mExecutor = Executors.newSingleThreadExecutor();

        // At most 8 active threads. All threads idling for more than a minute will
        // be closed.
        mExecutor = new ThreadPoolExecutor(8, 8, 60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(0));
        mExecutor.allowCoreThreadTimeOut(true);

        // Build the tree structure in memory.
        mTree = new HashMap<>();
@@ -493,25 +500,20 @@ public class Archive implements Closeable {
    }

    /**
     * Schedules a gracefully close of the archive after any opened files are closed.
     * Closes an archive.
     *
     * <p>This method does not block until shutdown. Once called, other methods should not be
     * called.
     * called. Any active pipes will be terminated.
     */
    @Override
    public void close() {
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
        mExecutor.shutdownNow();
        try {
            mZipFile.close();
        } catch (IOException e) {
            // Silent close.
        }
    }
        });
        mExecutor.shutdown();
    }

    private void addCursorRow(MatrixCursor cursor, ZipEntry entry) {
        final MatrixCursor.RowBuilder row = cursor.newRow();