blob: 8145edc4d7db7e31e5cf4587525d124dba91ec1c [file] [log] [blame]
Jeff Sharkeyf63b7772013-10-01 17:57:41 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070019import android.os.AsyncTask;
20
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070021import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070022
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070023import java.lang.ref.WeakReference;
24import java.util.ArrayList;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070025import java.util.HashMap;
26import java.util.concurrent.Executor;
27import java.util.concurrent.LinkedBlockingQueue;
28
29public class ProviderExecutor extends Thread implements Executor {
30
31 @GuardedBy("sExecutors")
Steve McKayfefcd702015-08-20 16:19:38 +000032 private static HashMap<String, ProviderExecutor> sExecutors = new HashMap<>();
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070033
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070034 public static ProviderExecutor forAuthority(String authority) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070035 synchronized (sExecutors) {
36 ProviderExecutor executor = sExecutors.get(authority);
37 if (executor == null) {
38 executor = new ProviderExecutor();
39 executor.setName("ProviderExecutor: " + authority);
40 executor.start();
41 sExecutors.put(authority, executor);
42 }
43 return executor;
44 }
45 }
46
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070047 public interface Preemptable {
48 void preempt();
49 }
50
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070051 private final LinkedBlockingQueue<Runnable> mQueue = new LinkedBlockingQueue<Runnable>();
52
Steve McKayfefcd702015-08-20 16:19:38 +000053 private final ArrayList<WeakReference<Preemptable>> mPreemptable = new ArrayList<>();
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070054
55 private void preempt() {
56 synchronized (mPreemptable) {
57 int count = 0;
58 for (WeakReference<Preemptable> ref : mPreemptable) {
59 final Preemptable p = ref.get();
60 if (p != null) {
61 count++;
62 p.preempt();
63 }
64 }
65 mPreemptable.clear();
66 }
67 }
68
69 /**
70 * Execute the given task. If given task is not {@link Preemptable}, it will
71 * preempt all outstanding preemptable tasks.
72 */
73 public <P> void execute(AsyncTask<P, ?, ?> task, P... params) {
74 if (task instanceof Preemptable) {
75 synchronized (mPreemptable) {
76 mPreemptable.add(new WeakReference<Preemptable>((Preemptable) task));
77 }
78 task.executeOnExecutor(mNonPreemptingExecutor, params);
79 } else {
80 task.executeOnExecutor(this, params);
81 }
82 }
83
84 private Executor mNonPreemptingExecutor = new Executor() {
85 @Override
86 public void execute(Runnable command) {
Steve McKay0af8afd2016-02-25 13:34:03 -080087 assert(command != null);
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070088 mQueue.add(command);
89 }
90 };
91
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070092 @Override
93 public void execute(Runnable command) {
Jeff Sharkey753a3ae2013-10-22 17:09:44 -070094 preempt();
Steve McKay0af8afd2016-02-25 13:34:03 -080095 assert(command != null);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070096 mQueue.add(command);
97 }
98
99 @Override
100 public void run() {
101 while (true) {
102 try {
103 final Runnable command = mQueue.take();
104 command.run();
105 } catch (InterruptedException e) {
106 // That was weird; let's go look for more tasks.
107 }
108 }
109 }
110}