Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
Jeff Sharkey | b3d4263 | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 19 | import android.os.AsyncTask; |
| 20 | |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 21 | import com.android.internal.annotations.GuardedBy; |
| 22 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 23 | |
Jeff Sharkey | b3d4263 | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 24 | import java.lang.ref.WeakReference; |
| 25 | import java.util.ArrayList; |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 26 | import java.util.HashMap; |
| 27 | import java.util.concurrent.Executor; |
| 28 | import java.util.concurrent.LinkedBlockingQueue; |
| 29 | |
| 30 | public class ProviderExecutor extends Thread implements Executor { |
| 31 | |
| 32 | @GuardedBy("sExecutors") |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 33 | private static HashMap<String, ProviderExecutor> sExecutors = new HashMap<>(); |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 34 | |
Jeff Sharkey | b3d4263 | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 35 | public static ProviderExecutor forAuthority(String authority) { |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 36 | synchronized (sExecutors) { |
| 37 | ProviderExecutor executor = sExecutors.get(authority); |
| 38 | if (executor == null) { |
| 39 | executor = new ProviderExecutor(); |
| 40 | executor.setName("ProviderExecutor: " + authority); |
| 41 | executor.start(); |
| 42 | sExecutors.put(authority, executor); |
| 43 | } |
| 44 | return executor; |
| 45 | } |
| 46 | } |
| 47 | |
Jeff Sharkey | b3d4263 | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 48 | public interface Preemptable { |
| 49 | void preempt(); |
| 50 | } |
| 51 | |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 52 | private final LinkedBlockingQueue<Runnable> mQueue = new LinkedBlockingQueue<Runnable>(); |
| 53 | |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 54 | private final ArrayList<WeakReference<Preemptable>> mPreemptable = new ArrayList<>(); |
Jeff Sharkey | b3d4263 | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 55 | |
| 56 | private void preempt() { |
| 57 | synchronized (mPreemptable) { |
| 58 | int count = 0; |
| 59 | for (WeakReference<Preemptable> ref : mPreemptable) { |
| 60 | final Preemptable p = ref.get(); |
| 61 | if (p != null) { |
| 62 | count++; |
| 63 | p.preempt(); |
| 64 | } |
| 65 | } |
| 66 | mPreemptable.clear(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Execute the given task. If given task is not {@link Preemptable}, it will |
| 72 | * preempt all outstanding preemptable tasks. |
| 73 | */ |
| 74 | public <P> void execute(AsyncTask<P, ?, ?> task, P... params) { |
| 75 | if (task instanceof Preemptable) { |
| 76 | synchronized (mPreemptable) { |
| 77 | mPreemptable.add(new WeakReference<Preemptable>((Preemptable) task)); |
| 78 | } |
| 79 | task.executeOnExecutor(mNonPreemptingExecutor, params); |
| 80 | } else { |
| 81 | task.executeOnExecutor(this, params); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private Executor mNonPreemptingExecutor = new Executor() { |
| 86 | @Override |
| 87 | public void execute(Runnable command) { |
| 88 | Preconditions.checkNotNull(command); |
| 89 | mQueue.add(command); |
| 90 | } |
| 91 | }; |
| 92 | |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public void execute(Runnable command) { |
Jeff Sharkey | b3d4263 | 2013-10-22 17:09:44 -0700 | [diff] [blame] | 95 | preempt(); |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 96 | Preconditions.checkNotNull(command); |
| 97 | mQueue.add(command); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void run() { |
| 102 | while (true) { |
| 103 | try { |
| 104 | final Runnable command = mQueue.take(); |
| 105 | command.run(); |
| 106 | } catch (InterruptedException e) { |
| 107 | // That was weird; let's go look for more tasks. |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |