Skip to content

Commit

Permalink
Fix locks and add lock annotations in RequestManager.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=226105246
  • Loading branch information
sjudd committed Dec 19, 2018
1 parent d2b2742 commit eaf720e
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions library/src/main/java/com/bumptech/glide/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.os.Looper;
import android.support.annotation.CheckResult;
import android.support.annotation.DrawableRes;
import android.support.annotation.GuardedBy;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RawRes;
Expand Down Expand Up @@ -63,9 +64,13 @@ public class RequestManager implements LifecycleListener,

protected final Glide glide;
protected final Context context;
@SuppressWarnings("WeakerAccess")
@Synthetic final Lifecycle lifecycle;
@GuardedBy("this")
private final RequestTracker requestTracker;
@GuardedBy("this")
private final RequestManagerTreeNode treeNode;
@GuardedBy("this")
private final TargetTracker targetTracker = new TargetTracker();
private final Runnable addSelfToLifecycle = new Runnable() {
@Override
Expand All @@ -80,6 +85,7 @@ public void run() {
// the list each time a request is started.
private final CopyOnWriteArrayList<RequestListener<Object>> defaultRequestListeners;

@GuardedBy("this")
private RequestOptions requestOptions;

public RequestManager(
Expand Down Expand Up @@ -132,11 +138,11 @@ public RequestManager(
glide.registerRequestManager(this);
}

protected void setRequestOptions(@NonNull RequestOptions toSet) {
protected synchronized void setRequestOptions(@NonNull RequestOptions toSet) {
requestOptions = toSet.clone().autoClone();
}

private void updateRequestOptions(@NonNull RequestOptions toUpdate) {
private synchronized void updateRequestOptions(@NonNull RequestOptions toUpdate) {
requestOptions = requestOptions.apply(toUpdate);
}

Expand Down Expand Up @@ -204,8 +210,7 @@ public synchronized RequestManager setDefaultRequestOptions(
* the behavior of the listener on the resource type, you will need to use {@code instanceof} to
* do so. It's not safe to cast resource types without first checking with {@code instanceof}.
*/
public synchronized RequestManager addDefaultRequestListener(
RequestListener<Object> requestListener) {
public RequestManager addDefaultRequestListener(RequestListener<Object> requestListener) {
defaultRequestListeners.add(requestListener);
return this;
}
Expand Down Expand Up @@ -294,7 +299,7 @@ public synchronized void resumeRequests() {
*/
// Public API.
@SuppressWarnings("unused")
public void resumeRequestsRecursive() {
public synchronized void resumeRequestsRecursive() {
Util.assertMainThread();
resumeRequests();
for (RequestManager requestManager : treeNode.getDescendants()) {
Expand All @@ -308,7 +313,7 @@ public void resumeRequestsRecursive() {
* requests.
*/
@Override
public void onStart() {
public synchronized void onStart() {
resumeRequests();
targetTracker.onStart();
}
Expand Down Expand Up @@ -650,12 +655,13 @@ <T> TransitionOptions<?, T> getDefaultTransitionOptions(Class<T> transcodeClass)
}

@Override
public String toString() {
public synchronized String toString() {
return super.toString() + "{tracker=" + requestTracker + ", treeNode=" + treeNode + "}";
}

private static class RequestManagerConnectivityListener implements ConnectivityMonitor
.ConnectivityListener {
private class RequestManagerConnectivityListener
implements ConnectivityMonitor.ConnectivityListener {
@GuardedBy("RequestManager.this")
private final RequestTracker requestTracker;

RequestManagerConnectivityListener(@NonNull RequestTracker requestTracker) {
Expand All @@ -665,7 +671,9 @@ private static class RequestManagerConnectivityListener implements ConnectivityM
@Override
public void onConnectivityChanged(boolean isConnected) {
if (isConnected) {
requestTracker.restartRequests();
synchronized (RequestManager.this) {
requestTracker.restartRequests();
}
}
}
}
Expand All @@ -679,7 +687,7 @@ private static class ClearTarget extends ViewTarget<View, Object> {
@Override
public void onResourceReady(@NonNull Object resource,
@Nullable Transition<? super Object> transition) {
// Do nothing.

}
}
}

0 comments on commit eaf720e

Please sign in to comment.