DownloadManager.Task: Remove top level thread interrupt

DownloadManager doesn't know enough about the Downloader implementations to
know that interrupting the thread is the right thing to do. In particular,
if a Downloader implementation wants to delegate work to additional worker
threads, then it will probably not want its main thread to be interrupted
on cancelation. Instead, it will want to cancel/interrupt its worker threads,
and keep its main thread blocked until work on those worker threads has
stopped (download() must not return whilst the Downloader is still touching
the cache).

This change moves control over what happens to the individual Downloader
implementations.

Issue: #5978
PiperOrigin-RevId: 309419177
This commit is contained in:
olly 2020-05-01 17:26:00 +01:00 committed by Oliver Woodman
parent 32516d85be
commit bb744e5f9b
3 changed files with 20 additions and 2 deletions

View File

@ -1284,8 +1284,6 @@ public final class DownloadManager {
if (!isCanceled) {
isCanceled = true;
downloader.cancel();
// TODO - This will need propagating deeper as soon as we start using additional threads.
interrupt();
}
}

View File

@ -35,6 +35,8 @@ public final class ProgressiveDownloader implements Downloader {
private final CacheDataSource dataSource;
private final AtomicBoolean isCanceled;
@Nullable private volatile Thread downloadThread;
/**
* @param uri Uri of the data to be downloaded.
* @param customCacheKey A custom key that uniquely identifies the original stream. Used for cache
@ -74,6 +76,10 @@ public final class ProgressiveDownloader implements Downloader {
@Override
public void download(@Nullable ProgressListener progressListener) throws IOException {
downloadThread = Thread.currentThread();
if (isCanceled.get()) {
return;
}
@Nullable PriorityTaskManager priorityTaskManager = dataSource.getUpstreamPriorityTaskManager();
if (priorityTaskManager != null) {
priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
@ -96,6 +102,10 @@ public final class ProgressiveDownloader implements Downloader {
@Override
public void cancel() {
isCanceled.set(true);
@Nullable Thread downloadThread = this.downloadThread;
if (downloadThread != null) {
downloadThread.interrupt();
}
}
@Override

View File

@ -76,6 +76,8 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
private final Executor executor;
private final AtomicBoolean isCanceled;
@Nullable private volatile Thread downloadThread;
/**
* @param manifestUri The {@link Uri} of the manifest to be downloaded.
* @param manifestParser A parser for the manifest.
@ -103,6 +105,10 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
@Override
public final void download(@Nullable ProgressListener progressListener) throws IOException {
downloadThread = Thread.currentThread();
if (isCanceled.get()) {
return;
}
@Nullable
PriorityTaskManager priorityTaskManager =
cacheDataSourceFactory.getUpstreamPriorityTaskManager();
@ -183,6 +189,10 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
@Override
public void cancel() {
isCanceled.set(true);
@Nullable Thread downloadThread = this.downloadThread;
if (downloadThread != null) {
downloadThread.interrupt();
}
}
@Override