001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static java.util.Objects.requireNonNull;
019
020import com.google.common.annotations.GwtIncompatible;
021import com.google.common.annotations.J2ktIncompatible;
022import com.google.common.annotations.VisibleForTesting;
023import com.google.common.base.MoreObjects;
024import com.google.common.base.Preconditions;
025import com.google.common.collect.ImmutableSet;
026import com.google.common.collect.Lists;
027import com.google.common.collect.MapMaker;
028import com.google.common.collect.Maps;
029import com.google.common.collect.Sets;
030import com.google.j2objc.annotations.Weak;
031import java.util.ArrayList;
032import java.util.Arrays;
033import java.util.Collections;
034import java.util.EnumMap;
035import java.util.List;
036import java.util.Map;
037import java.util.Map.Entry;
038import java.util.Set;
039import java.util.concurrent.ConcurrentMap;
040import java.util.concurrent.TimeUnit;
041import java.util.concurrent.locks.ReentrantLock;
042import java.util.concurrent.locks.ReentrantReadWriteLock;
043import java.util.logging.Level;
044import org.jspecify.annotations.Nullable;
045
046/**
047 * The {@code CycleDetectingLockFactory} creates {@link ReentrantLock} instances and {@link
048 * ReentrantReadWriteLock} instances that detect potential deadlock by checking for cycles in lock
049 * acquisition order.
050 *
051 * <p>Potential deadlocks detected when calling the {@code lock()}, {@code lockInterruptibly()}, or
052 * {@code tryLock()} methods will result in the execution of the {@link Policy} specified when
053 * creating the factory. The currently available policies are:
054 *
055 * <ul>
056 *   <li>DISABLED
057 *   <li>WARN
058 *   <li>THROW
059 * </ul>
060 *
061 * <p>The locks created by a factory instance will detect lock acquisition cycles with locks created
062 * by other {@code CycleDetectingLockFactory} instances (except those with {@code Policy.DISABLED}).
063 * A lock's behavior when a cycle is detected, however, is defined by the {@code Policy} of the
064 * factory that created it. This allows detection of cycles across components while delegating
065 * control over lock behavior to individual components.
066 *
067 * <p>Applications are encouraged to use a {@code CycleDetectingLockFactory} to create any locks for
068 * which external/unmanaged code is executed while the lock is held. (See caveats under
069 * <strong>Performance</strong>).
070 *
071 * <p><strong>Cycle Detection</strong>
072 *
073 * <p>Deadlocks can arise when locks are acquired in an order that forms a cycle. In a simple
074 * example involving two locks and two threads, deadlock occurs when one thread acquires Lock A, and
075 * then Lock B, while another thread acquires Lock B, and then Lock A:
076 *
077 * <pre>
078 * Thread1: acquire(LockA) --X acquire(LockB)
079 * Thread2: acquire(LockB) --X acquire(LockA)
080 * </pre>
081 *
082 * <p>Neither thread will progress because each is waiting for the other. In more complex
083 * applications, cycles can arise from interactions among more than 2 locks:
084 *
085 * <pre>
086 * Thread1: acquire(LockA) --X acquire(LockB)
087 * Thread2: acquire(LockB) --X acquire(LockC)
088 * ...
089 * ThreadN: acquire(LockN) --X acquire(LockA)
090 * </pre>
091 *
092 * <p>The implementation detects cycles by constructing a directed graph in which each lock
093 * represents a node and each edge represents an acquisition ordering between two locks.
094 *
095 * <ul>
096 *   <li>Each lock adds (and removes) itself to/from a ThreadLocal Set of acquired locks when the
097 *       Thread acquires its first hold (and releases its last remaining hold).
098 *   <li>Before the lock is acquired, the lock is checked against the current set of acquired
099 *       locks---to each of the acquired locks, an edge from the soon-to-be-acquired lock is either
100 *       verified or created.
101 *   <li>If a new edge needs to be created, the outgoing edges of the acquired locks are traversed
102 *       to check for a cycle that reaches the lock to be acquired. If no cycle is detected, a new
103 *       "safe" edge is created.
104 *   <li>If a cycle is detected, an "unsafe" (cyclic) edge is created to represent a potential
105 *       deadlock situation, and the appropriate Policy is executed.
106 * </ul>
107 *
108 * <p>Note that detection of potential deadlock does not necessarily indicate that deadlock will
109 * happen, as it is possible that higher level application logic prevents the cyclic lock
110 * acquisition from occurring. One example of a false positive is:
111 *
112 * <pre>
113 * LockA -&gt; LockB -&gt; LockC
114 * LockA -&gt; LockC -&gt; LockB
115 * </pre>
116 *
117 * <p><strong>ReadWriteLocks</strong>
118 *
119 * <p>While {@code ReadWriteLock} instances have different properties and can form cycles without
120 * potential deadlock, this class treats {@code ReadWriteLock} instances as equivalent to
121 * traditional exclusive locks. Although this increases the false positives that the locks detect
122 * (i.e. cycles that will not actually result in deadlock), it simplifies the algorithm and
123 * implementation considerably. The assumption is that a user of this factory wishes to eliminate
124 * any cyclic acquisition ordering.
125 *
126 * <p><strong>Explicit Lock Acquisition Ordering</strong>
127 *
128 * <p>The {@link CycleDetectingLockFactory.WithExplicitOrdering} class can be used to enforce an
129 * application-specific ordering in addition to performing general cycle detection.
130 *
131 * <p><strong>Garbage Collection</strong>
132 *
133 * <p>In order to allow proper garbage collection of unused locks, the edges of the lock graph are
134 * weak references.
135 *
136 * <p><strong>Performance</strong>
137 *
138 * <p>The extra bookkeeping done by cycle detecting locks comes at some cost to performance.
139 * Benchmarks (as of December 2011) show that:
140 *
141 * <ul>
142 *   <li>for an unnested {@code lock()} and {@code unlock()}, a cycle detecting lock takes 38ns as
143 *       opposed to the 24ns taken by a plain lock.
144 *   <li>for nested locking, the cost increases with the depth of the nesting:
145 *       <ul>
146 *         <li>2 levels: average of 64ns per lock()/unlock()
147 *         <li>3 levels: average of 77ns per lock()/unlock()
148 *         <li>4 levels: average of 99ns per lock()/unlock()
149 *         <li>5 levels: average of 103ns per lock()/unlock()
150 *         <li>10 levels: average of 184ns per lock()/unlock()
151 *         <li>20 levels: average of 393ns per lock()/unlock()
152 *       </ul>
153 * </ul>
154 *
155 * <p>As such, the CycleDetectingLockFactory may not be suitable for performance-critical
156 * applications which involve tightly-looped or deeply-nested locking algorithms.
157 *
158 * @author Darick Tong
159 * @since 13.0
160 */
161@J2ktIncompatible
162@GwtIncompatible
163public class CycleDetectingLockFactory {
164
165  /**
166   * Encapsulates the action to be taken when a potential deadlock is encountered. Clients can use
167   * one of the predefined {@link Policies} or specify a custom implementation. Implementations must
168   * be thread-safe.
169   *
170   * @since 13.0
171   */
172  public interface Policy {
173
174    /**
175     * Called when a potential deadlock is encountered. Implementations can throw the given {@code
176     * exception} and/or execute other desired logic.
177     *
178     * <p>Note that the method will be called even upon an invocation of {@code tryLock()}. Although
179     * {@code tryLock()} technically recovers from deadlock by eventually timing out, this behavior
180     * is chosen based on the assumption that it is the application's wish to prohibit any cyclical
181     * lock acquisitions.
182     */
183    void handlePotentialDeadlock(PotentialDeadlockException exception);
184  }
185
186  /**
187   * Pre-defined {@link Policy} implementations.
188   *
189   * @since 13.0
190   */
191  public enum Policies implements Policy {
192    /**
193     * When potential deadlock is detected, this policy results in the throwing of the {@code
194     * PotentialDeadlockException} indicating the potential deadlock, which includes stack traces
195     * illustrating the cycle in lock acquisition order.
196     */
197    THROW {
198      @Override
199      public void handlePotentialDeadlock(PotentialDeadlockException e) {
200        throw e;
201      }
202    },
203
204    /**
205     * When potential deadlock is detected, this policy results in the logging of a {@link
206     * Level#SEVERE} message indicating the potential deadlock, which includes stack traces
207     * illustrating the cycle in lock acquisition order.
208     */
209    WARN {
210      @Override
211      public void handlePotentialDeadlock(PotentialDeadlockException e) {
212        logger.get().log(Level.SEVERE, "Detected potential deadlock", e);
213      }
214    },
215
216    /**
217     * Disables cycle detection. This option causes the factory to return unmodified lock
218     * implementations provided by the JDK, and is provided to allow applications to easily
219     * parameterize when cycle detection is enabled.
220     *
221     * <p>Note that locks created by a factory with this policy will <em>not</em> participate the
222     * cycle detection performed by locks created by other factories.
223     */
224    DISABLED {
225      @Override
226      public void handlePotentialDeadlock(PotentialDeadlockException e) {}
227    };
228  }
229
230  /** Creates a new factory with the specified policy. */
231  public static CycleDetectingLockFactory newInstance(Policy policy) {
232    return new CycleDetectingLockFactory(policy);
233  }
234
235  /** Equivalent to {@code newReentrantLock(lockName, false)}. */
236  public ReentrantLock newReentrantLock(String lockName) {
237    return newReentrantLock(lockName, false);
238  }
239
240  /**
241   * Creates a {@link ReentrantLock} with the given fairness policy. The {@code lockName} is used in
242   * the warning or exception output to help identify the locks involved in the detected deadlock.
243   */
244  public ReentrantLock newReentrantLock(String lockName, boolean fair) {
245    return policy == Policies.DISABLED
246        ? new ReentrantLock(fair)
247        : new CycleDetectingReentrantLock(new LockGraphNode(lockName), fair);
248  }
249
250  /** Equivalent to {@code newReentrantReadWriteLock(lockName, false)}. */
251  public ReentrantReadWriteLock newReentrantReadWriteLock(String lockName) {
252    return newReentrantReadWriteLock(lockName, false);
253  }
254
255  /**
256   * Creates a {@link ReentrantReadWriteLock} with the given fairness policy. The {@code lockName}
257   * is used in the warning or exception output to help identify the locks involved in the detected
258   * deadlock.
259   */
260  public ReentrantReadWriteLock newReentrantReadWriteLock(String lockName, boolean fair) {
261    return policy == Policies.DISABLED
262        ? new ReentrantReadWriteLock(fair)
263        : new CycleDetectingReentrantReadWriteLock(new LockGraphNode(lockName), fair);
264  }
265
266  // A static mapping from an Enum type to its set of LockGraphNodes.
267  private static final ConcurrentMap<
268          Class<? extends Enum<?>>, Map<? extends Enum<?>, LockGraphNode>>
269      lockGraphNodesPerType = new MapMaker().weakKeys().makeMap();
270
271  /** Creates a {@code CycleDetectingLockFactory.WithExplicitOrdering<E>}. */
272  public static <E extends Enum<E>> WithExplicitOrdering<E> newInstanceWithExplicitOrdering(
273      Class<E> enumClass, Policy policy) {
274    // createNodes maps each enumClass to a Map with the corresponding enum key
275    // type.
276    checkNotNull(enumClass);
277    checkNotNull(policy);
278    @SuppressWarnings("unchecked")
279    Map<E, LockGraphNode> lockGraphNodes = (Map<E, LockGraphNode>) getOrCreateNodes(enumClass);
280    return new WithExplicitOrdering<>(policy, lockGraphNodes);
281  }
282
283  @SuppressWarnings("unchecked")
284  private static <E extends Enum<E>> Map<? extends E, LockGraphNode> getOrCreateNodes(
285      Class<E> clazz) {
286    Map<E, LockGraphNode> existing = (Map<E, LockGraphNode>) lockGraphNodesPerType.get(clazz);
287    if (existing != null) {
288      return existing;
289    }
290    Map<E, LockGraphNode> created = createNodes(clazz);
291    existing = (Map<E, LockGraphNode>) lockGraphNodesPerType.putIfAbsent(clazz, created);
292    return MoreObjects.firstNonNull(existing, created);
293  }
294
295  /**
296   * For a given Enum type, creates an immutable map from each of the Enum's values to a
297   * corresponding LockGraphNode, with the {@code allowedPriorLocks} and {@code
298   * disallowedPriorLocks} prepopulated with nodes according to the natural ordering of the
299   * associated Enum values.
300   */
301  @VisibleForTesting
302  static <E extends Enum<E>> Map<E, LockGraphNode> createNodes(Class<E> clazz) {
303    EnumMap<E, LockGraphNode> map = Maps.newEnumMap(clazz);
304    E[] keys = clazz.getEnumConstants();
305    int numKeys = keys.length;
306    ArrayList<LockGraphNode> nodes = Lists.newArrayListWithCapacity(numKeys);
307    // Create a LockGraphNode for each enum value.
308    for (E key : keys) {
309      LockGraphNode node = new LockGraphNode(getLockName(key));
310      nodes.add(node);
311      map.put(key, node);
312    }
313    // Pre-populate all allowedPriorLocks with nodes of smaller ordinal.
314    for (int i = 1; i < numKeys; i++) {
315      nodes.get(i).checkAcquiredLocks(Policies.THROW, nodes.subList(0, i));
316    }
317    // Pre-populate all disallowedPriorLocks with nodes of larger ordinal.
318    for (int i = 0; i < numKeys - 1; i++) {
319      nodes.get(i).checkAcquiredLocks(Policies.DISABLED, nodes.subList(i + 1, numKeys));
320    }
321    return Collections.unmodifiableMap(map);
322  }
323
324  /**
325   * For the given Enum value {@code rank}, returns the value's {@code "EnumClass.name"}, which is
326   * used in exception and warning output.
327   */
328  private static String getLockName(Enum<?> rank) {
329    return rank.getDeclaringClass().getSimpleName() + "." + rank.name();
330  }
331
332  /**
333   * A {@code CycleDetectingLockFactory.WithExplicitOrdering} provides the additional enforcement of
334   * an application-specified ordering of lock acquisitions. The application defines the allowed
335   * ordering with an {@code Enum} whose values each correspond to a lock type. The order in which
336   * the values are declared dictates the allowed order of lock acquisition. In other words, locks
337   * corresponding to smaller values of {@link Enum#ordinal()} should only be acquired before locks
338   * with larger ordinals. Example:
339   *
340   * <pre>{@code
341   * enum MyLockOrder {
342   *   FIRST, SECOND, THIRD;
343   * }
344   *
345   * CycleDetectingLockFactory.WithExplicitOrdering<MyLockOrder> factory =
346   *   CycleDetectingLockFactory.newInstanceWithExplicitOrdering(Policies.THROW);
347   *
348   * Lock lock1 = factory.newReentrantLock(MyLockOrder.FIRST);
349   * Lock lock2 = factory.newReentrantLock(MyLockOrder.SECOND);
350   * Lock lock3 = factory.newReentrantLock(MyLockOrder.THIRD);
351   *
352   * lock1.lock();
353   * lock3.lock();
354   * lock2.lock();  // will throw an IllegalStateException
355   * }</pre>
356   *
357   * <p>As with all locks created by instances of {@code CycleDetectingLockFactory} explicitly
358   * ordered locks participate in general cycle detection with all other cycle detecting locks, and
359   * a lock's behavior when detecting a cyclic lock acquisition is defined by the {@code Policy} of
360   * the factory that created it.
361   *
362   * <p>Note, however, that although multiple locks can be created for a given Enum value, whether
363   * it be through separate factory instances or through multiple calls to the same factory,
364   * attempting to acquire multiple locks with the same Enum value (within the same thread) will
365   * result in an IllegalStateException regardless of the factory's policy. For example:
366   *
367   * <pre>{@code
368   * CycleDetectingLockFactory.WithExplicitOrdering<MyLockOrder> factory1 =
369   *   CycleDetectingLockFactory.newInstanceWithExplicitOrdering(...);
370   * CycleDetectingLockFactory.WithExplicitOrdering<MyLockOrder> factory2 =
371   *   CycleDetectingLockFactory.newInstanceWithExplicitOrdering(...);
372   *
373   * Lock lockA = factory1.newReentrantLock(MyLockOrder.FIRST);
374   * Lock lockB = factory1.newReentrantLock(MyLockOrder.FIRST);
375   * Lock lockC = factory2.newReentrantLock(MyLockOrder.FIRST);
376   *
377   * lockA.lock();
378   *
379   * lockB.lock();  // will throw an IllegalStateException
380   * lockC.lock();  // will throw an IllegalStateException
381   *
382   * lockA.lock();  // reentrant acquisition is okay
383   * }</pre>
384   *
385   * <p>It is the responsibility of the application to ensure that multiple lock instances with the
386   * same rank are never acquired in the same thread.
387   *
388   * @param <E> The Enum type representing the explicit lock ordering.
389   * @since 13.0
390   */
391  public static final class WithExplicitOrdering<E extends Enum<E>>
392      extends CycleDetectingLockFactory {
393
394    private final Map<E, LockGraphNode> lockGraphNodes;
395
396    @VisibleForTesting
397    WithExplicitOrdering(Policy policy, Map<E, LockGraphNode> lockGraphNodes) {
398      super(policy);
399      this.lockGraphNodes = lockGraphNodes;
400    }
401
402    /** Equivalent to {@code newReentrantLock(rank, false)}. */
403    public ReentrantLock newReentrantLock(E rank) {
404      return newReentrantLock(rank, false);
405    }
406
407    /**
408     * Creates a {@link ReentrantLock} with the given fairness policy and rank. The values returned
409     * by {@link Enum#getDeclaringClass()} and {@link Enum#name()} are used to describe the lock in
410     * warning or exception output.
411     *
412     * @throws IllegalStateException If the factory has already created a {@code Lock} with the
413     *     specified rank.
414     */
415    public ReentrantLock newReentrantLock(E rank, boolean fair) {
416      return policy == Policies.DISABLED
417          ? new ReentrantLock(fair)
418          // requireNonNull is safe because createNodes inserts an entry for every E.
419          // (If the caller passes `null` for the `rank` parameter, this will throw, but that's OK.)
420          : new CycleDetectingReentrantLock(requireNonNull(lockGraphNodes.get(rank)), fair);
421    }
422
423    /** Equivalent to {@code newReentrantReadWriteLock(rank, false)}. */
424    public ReentrantReadWriteLock newReentrantReadWriteLock(E rank) {
425      return newReentrantReadWriteLock(rank, false);
426    }
427
428    /**
429     * Creates a {@link ReentrantReadWriteLock} with the given fairness policy and rank. The values
430     * returned by {@link Enum#getDeclaringClass()} and {@link Enum#name()} are used to describe the
431     * lock in warning or exception output.
432     *
433     * @throws IllegalStateException If the factory has already created a {@code Lock} with the
434     *     specified rank.
435     */
436    public ReentrantReadWriteLock newReentrantReadWriteLock(E rank, boolean fair) {
437      return policy == Policies.DISABLED
438          ? new ReentrantReadWriteLock(fair)
439          // requireNonNull is safe because createNodes inserts an entry for every E.
440          // (If the caller passes `null` for the `rank` parameter, this will throw, but that's OK.)
441          : new CycleDetectingReentrantReadWriteLock(
442              requireNonNull(lockGraphNodes.get(rank)), fair);
443    }
444  }
445
446  //////// Implementation /////////
447
448  private static final LazyLogger logger = new LazyLogger(CycleDetectingLockFactory.class);
449
450  final Policy policy;
451
452  private CycleDetectingLockFactory(Policy policy) {
453    this.policy = checkNotNull(policy);
454  }
455
456  /**
457   * Tracks the currently acquired locks for each Thread, kept up to date by calls to {@link
458   * #aboutToAcquire(CycleDetectingLock)} and {@link #lockStateChanged(CycleDetectingLock)}.
459   */
460  // This is logically a Set, but an ArrayList is used to minimize the amount
461  // of allocation done on lock()/unlock().
462  private static final ThreadLocal<ArrayList<LockGraphNode>> acquiredLocks =
463      new ThreadLocal<ArrayList<LockGraphNode>>() {
464        @Override
465        protected ArrayList<LockGraphNode> initialValue() {
466          return Lists.<LockGraphNode>newArrayListWithCapacity(3);
467        }
468      };
469
470  /**
471   * A Throwable used to record a stack trace that illustrates an example of a specific lock
472   * acquisition ordering. The top of the stack trace is truncated such that it starts with the
473   * acquisition of the lock in question, e.g.
474   *
475   * <pre>
476   * com...ExampleStackTrace: LockB -&gt; LockC
477   *   at com...CycleDetectingReentrantLock.lock(CycleDetectingLockFactory.java:443)
478   *   at ...
479   *   at ...
480   *   at com...MyClass.someMethodThatAcquiresLockB(MyClass.java:123)
481   * </pre>
482   */
483  private static class ExampleStackTrace extends IllegalStateException {
484
485    static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0];
486
487    static final ImmutableSet<String> EXCLUDED_CLASS_NAMES =
488        ImmutableSet.of(
489            CycleDetectingLockFactory.class.getName(),
490            ExampleStackTrace.class.getName(),
491            LockGraphNode.class.getName());
492
493    ExampleStackTrace(LockGraphNode node1, LockGraphNode node2) {
494      super(node1.getLockName() + " -> " + node2.getLockName());
495      StackTraceElement[] origStackTrace = getStackTrace();
496      for (int i = 0, n = origStackTrace.length; i < n; i++) {
497        if (WithExplicitOrdering.class.getName().equals(origStackTrace[i].getClassName())) {
498          // For pre-populated disallowedPriorLocks edges, omit the stack trace.
499          setStackTrace(EMPTY_STACK_TRACE);
500          break;
501        }
502        if (!EXCLUDED_CLASS_NAMES.contains(origStackTrace[i].getClassName())) {
503          setStackTrace(Arrays.copyOfRange(origStackTrace, i, n));
504          break;
505        }
506      }
507    }
508  }
509
510  /**
511   * Represents a detected cycle in lock acquisition ordering. The exception includes a causal chain
512   * of {@code ExampleStackTrace} instances to illustrate the cycle, e.g.
513   *
514   * <pre>
515   * com....PotentialDeadlockException: Potential Deadlock from LockC -&gt; ReadWriteA
516   *   at ...
517   *   at ...
518   * Caused by: com...ExampleStackTrace: LockB -&gt; LockC
519   *   at ...
520   *   at ...
521   * Caused by: com...ExampleStackTrace: ReadWriteA -&gt; LockB
522   *   at ...
523   *   at ...
524   * </pre>
525   *
526   * <p>Instances are logged for the {@code Policies.WARN}, and thrown for {@code Policies.THROW}.
527   *
528   * @since 13.0
529   */
530  public static final class PotentialDeadlockException extends ExampleStackTrace {
531
532    private final ExampleStackTrace conflictingStackTrace;
533
534    private PotentialDeadlockException(
535        LockGraphNode node1, LockGraphNode node2, ExampleStackTrace conflictingStackTrace) {
536      super(node1, node2);
537      this.conflictingStackTrace = conflictingStackTrace;
538      initCause(conflictingStackTrace);
539    }
540
541    public ExampleStackTrace getConflictingStackTrace() {
542      return conflictingStackTrace;
543    }
544
545    /**
546     * Appends the chain of messages from the {@code conflictingStackTrace} to the original {@code
547     * message}.
548     */
549    @Override
550    public String getMessage() {
551      // requireNonNull is safe because ExampleStackTrace sets a non-null message.
552      StringBuilder message = new StringBuilder(requireNonNull(super.getMessage()));
553      for (Throwable t = conflictingStackTrace; t != null; t = t.getCause()) {
554        message.append(", ").append(t.getMessage());
555      }
556      return message.toString();
557    }
558  }
559
560  /**
561   * Internal Lock implementations implement the {@code CycleDetectingLock} interface, allowing the
562   * detection logic to treat all locks in the same manner.
563   */
564  private interface CycleDetectingLock {
565
566    /**
567     * @return the {@link LockGraphNode} associated with this lock.
568     */
569    LockGraphNode getLockGraphNode();
570
571    /**
572     * @return {@code true} if the current thread has acquired this lock.
573     */
574    boolean isAcquiredByCurrentThread();
575  }
576
577  /**
578   * A {@code LockGraphNode} associated with each lock instance keeps track of the directed edges in
579   * the lock acquisition graph.
580   */
581  private static class LockGraphNode {
582
583    /**
584     * The map tracking the locks that are known to be acquired before this lock, each associated
585     * with an example stack trace. Locks are weakly keyed to allow proper garbage collection when
586     * they are no longer referenced.
587     */
588    final Map<LockGraphNode, ExampleStackTrace> allowedPriorLocks =
589        new MapMaker().weakKeys().makeMap();
590
591    /**
592     * The map tracking lock nodes that can cause a lock acquisition cycle if acquired before this
593     * node.
594     */
595    final Map<LockGraphNode, PotentialDeadlockException> disallowedPriorLocks =
596        new MapMaker().weakKeys().makeMap();
597
598    final String lockName;
599
600    LockGraphNode(String lockName) {
601      this.lockName = Preconditions.checkNotNull(lockName);
602    }
603
604    String getLockName() {
605      return lockName;
606    }
607
608    void checkAcquiredLocks(Policy policy, List<LockGraphNode> acquiredLocks) {
609      for (LockGraphNode acquiredLock : acquiredLocks) {
610        checkAcquiredLock(policy, acquiredLock);
611      }
612    }
613
614    /**
615     * Checks the acquisition-ordering between {@code this}, which is about to be acquired, and the
616     * specified {@code acquiredLock}.
617     *
618     * <p>When this method returns, the {@code acquiredLock} should be in either the {@code
619     * preAcquireLocks} map, for the case in which it is safe to acquire {@code this} after the
620     * {@code acquiredLock}, or in the {@code disallowedPriorLocks} map, in which case it is not
621     * safe.
622     */
623    void checkAcquiredLock(Policy policy, LockGraphNode acquiredLock) {
624      // checkAcquiredLock() should never be invoked by a lock that has already
625      // been acquired. For unordered locks, aboutToAcquire() ensures this by
626      // checking isAcquiredByCurrentThread(). For ordered locks, however, this
627      // can happen because multiple locks may share the same LockGraphNode. In
628      // this situation, throw an IllegalStateException as defined by contract
629      // described in the documentation of WithExplicitOrdering.
630      Preconditions.checkState(
631          this != acquiredLock,
632          "Attempted to acquire multiple locks with the same rank %s",
633          acquiredLock.getLockName());
634
635      if (allowedPriorLocks.containsKey(acquiredLock)) {
636        // The acquisition ordering from "acquiredLock" to "this" has already
637        // been verified as safe. In a properly written application, this is
638        // the common case.
639        return;
640      }
641      PotentialDeadlockException previousDeadlockException = disallowedPriorLocks.get(acquiredLock);
642      if (previousDeadlockException != null) {
643        // Previously determined to be an unsafe lock acquisition.
644        // Create a new PotentialDeadlockException with the same causal chain
645        // (the example cycle) as that of the cached exception.
646        PotentialDeadlockException exception =
647            new PotentialDeadlockException(
648                acquiredLock, this, previousDeadlockException.getConflictingStackTrace());
649        policy.handlePotentialDeadlock(exception);
650        return;
651      }
652      // Otherwise, it's the first time seeing this lock relationship. Look for
653      // a path from the acquiredLock to this.
654      Set<LockGraphNode> seen = Sets.newIdentityHashSet();
655      ExampleStackTrace path = acquiredLock.findPathTo(this, seen);
656
657      if (path == null) {
658        // this can be safely acquired after the acquiredLock.
659        //
660        // Note that there is a race condition here which can result in missing
661        // a cyclic edge: it's possible for two threads to simultaneous find
662        // "safe" edges which together form a cycle. Preventing this race
663        // condition efficiently without _introducing_ deadlock is probably
664        // tricky. For now, just accept the race condition---missing a warning
665        // now and then is still better than having no deadlock detection.
666        allowedPriorLocks.put(acquiredLock, new ExampleStackTrace(acquiredLock, this));
667      } else {
668        // Unsafe acquisition order detected. Create and cache a
669        // PotentialDeadlockException.
670        PotentialDeadlockException exception =
671            new PotentialDeadlockException(acquiredLock, this, path);
672        disallowedPriorLocks.put(acquiredLock, exception);
673        policy.handlePotentialDeadlock(exception);
674      }
675    }
676
677    /**
678     * Performs a depth-first traversal of the graph edges defined by each node's {@code
679     * allowedPriorLocks} to find a path between {@code this} and the specified {@code lock}.
680     *
681     * @return If a path was found, a chained {@link ExampleStackTrace} illustrating the path to the
682     *     {@code lock}, or {@code null} if no path was found.
683     */
684    private @Nullable ExampleStackTrace findPathTo(LockGraphNode node, Set<LockGraphNode> seen) {
685      if (!seen.add(this)) {
686        return null; // Already traversed this node.
687      }
688      ExampleStackTrace found = allowedPriorLocks.get(node);
689      if (found != null) {
690        return found; // Found a path ending at the node!
691      }
692      // Recurse the edges.
693      for (Entry<LockGraphNode, ExampleStackTrace> entry : allowedPriorLocks.entrySet()) {
694        LockGraphNode preAcquiredLock = entry.getKey();
695        found = preAcquiredLock.findPathTo(node, seen);
696        if (found != null) {
697          // One of this node's allowedPriorLocks found a path. Prepend an
698          // ExampleStackTrace(preAcquiredLock, this) to the returned chain of
699          // ExampleStackTraces.
700          ExampleStackTrace path = new ExampleStackTrace(preAcquiredLock, this);
701          path.setStackTrace(entry.getValue().getStackTrace());
702          path.initCause(found);
703          return path;
704        }
705      }
706      return null;
707    }
708  }
709
710  /**
711   * CycleDetectingLock implementations must call this method before attempting to acquire the lock.
712   */
713  private void aboutToAcquire(CycleDetectingLock lock) {
714    if (!lock.isAcquiredByCurrentThread()) {
715      // requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
716      ArrayList<LockGraphNode> acquiredLockList = requireNonNull(acquiredLocks.get());
717      LockGraphNode node = lock.getLockGraphNode();
718      node.checkAcquiredLocks(policy, acquiredLockList);
719      acquiredLockList.add(node);
720    }
721  }
722
723  /**
724   * CycleDetectingLock implementations must call this method in a {@code finally} clause after any
725   * attempt to change the lock state, including both lock and unlock attempts. Failure to do so can
726   * result in corrupting the acquireLocks set.
727   */
728  private static void lockStateChanged(CycleDetectingLock lock) {
729    if (!lock.isAcquiredByCurrentThread()) {
730      // requireNonNull accommodates Android's @RecentlyNullable annotation on ThreadLocal.get
731      ArrayList<LockGraphNode> acquiredLockList = requireNonNull(acquiredLocks.get());
732      LockGraphNode node = lock.getLockGraphNode();
733      // Iterate in reverse because locks are usually locked/unlocked in a
734      // LIFO order.
735      for (int i = acquiredLockList.size() - 1; i >= 0; i--) {
736        if (acquiredLockList.get(i) == node) {
737          acquiredLockList.remove(i);
738          break;
739        }
740      }
741    }
742  }
743
744  final class CycleDetectingReentrantLock extends ReentrantLock implements CycleDetectingLock {
745
746    private final LockGraphNode lockGraphNode;
747
748    private CycleDetectingReentrantLock(LockGraphNode lockGraphNode, boolean fair) {
749      super(fair);
750      this.lockGraphNode = Preconditions.checkNotNull(lockGraphNode);
751    }
752
753    ///// CycleDetectingLock methods. /////
754
755    @Override
756    public LockGraphNode getLockGraphNode() {
757      return lockGraphNode;
758    }
759
760    @Override
761    public boolean isAcquiredByCurrentThread() {
762      return isHeldByCurrentThread();
763    }
764
765    ///// Overridden ReentrantLock methods. /////
766
767    @Override
768    public void lock() {
769      aboutToAcquire(this);
770      try {
771        super.lock();
772      } finally {
773        lockStateChanged(this);
774      }
775    }
776
777    @Override
778    public void lockInterruptibly() throws InterruptedException {
779      aboutToAcquire(this);
780      try {
781        super.lockInterruptibly();
782      } finally {
783        lockStateChanged(this);
784      }
785    }
786
787    @Override
788    public boolean tryLock() {
789      aboutToAcquire(this);
790      try {
791        return super.tryLock();
792      } finally {
793        lockStateChanged(this);
794      }
795    }
796
797    @Override
798    public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
799      aboutToAcquire(this);
800      try {
801        return super.tryLock(timeout, unit);
802      } finally {
803        lockStateChanged(this);
804      }
805    }
806
807    @Override
808    public void unlock() {
809      try {
810        super.unlock();
811      } finally {
812        lockStateChanged(this);
813      }
814    }
815  }
816
817  final class CycleDetectingReentrantReadWriteLock extends ReentrantReadWriteLock
818      implements CycleDetectingLock {
819
820    // These ReadLock/WriteLock implementations shadow those in the
821    // ReentrantReadWriteLock superclass. They are simply wrappers around the
822    // internal Sync object, so this is safe since the shadowed locks are never
823    // exposed or used.
824    private final CycleDetectingReentrantReadLock readLock;
825    private final CycleDetectingReentrantWriteLock writeLock;
826
827    private final LockGraphNode lockGraphNode;
828
829    private CycleDetectingReentrantReadWriteLock(LockGraphNode lockGraphNode, boolean fair) {
830      super(fair);
831      this.readLock = new CycleDetectingReentrantReadLock(this);
832      this.writeLock = new CycleDetectingReentrantWriteLock(this);
833      this.lockGraphNode = Preconditions.checkNotNull(lockGraphNode);
834    }
835
836    ///// Overridden ReentrantReadWriteLock methods. /////
837
838    @Override
839    public ReadLock readLock() {
840      return readLock;
841    }
842
843    @Override
844    public WriteLock writeLock() {
845      return writeLock;
846    }
847
848    ///// CycleDetectingLock methods. /////
849
850    @Override
851    public LockGraphNode getLockGraphNode() {
852      return lockGraphNode;
853    }
854
855    @Override
856    public boolean isAcquiredByCurrentThread() {
857      return isWriteLockedByCurrentThread() || getReadHoldCount() > 0;
858    }
859  }
860
861  private class CycleDetectingReentrantReadLock extends ReentrantReadWriteLock.ReadLock {
862
863    @Weak final CycleDetectingReentrantReadWriteLock readWriteLock;
864
865    CycleDetectingReentrantReadLock(CycleDetectingReentrantReadWriteLock readWriteLock) {
866      super(readWriteLock);
867      this.readWriteLock = readWriteLock;
868    }
869
870    @Override
871    public void lock() {
872      aboutToAcquire(readWriteLock);
873      try {
874        super.lock();
875      } finally {
876        lockStateChanged(readWriteLock);
877      }
878    }
879
880    @Override
881    public void lockInterruptibly() throws InterruptedException {
882      aboutToAcquire(readWriteLock);
883      try {
884        super.lockInterruptibly();
885      } finally {
886        lockStateChanged(readWriteLock);
887      }
888    }
889
890    @Override
891    public boolean tryLock() {
892      aboutToAcquire(readWriteLock);
893      try {
894        return super.tryLock();
895      } finally {
896        lockStateChanged(readWriteLock);
897      }
898    }
899
900    @Override
901    public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
902      aboutToAcquire(readWriteLock);
903      try {
904        return super.tryLock(timeout, unit);
905      } finally {
906        lockStateChanged(readWriteLock);
907      }
908    }
909
910    @Override
911    public void unlock() {
912      try {
913        super.unlock();
914      } finally {
915        lockStateChanged(readWriteLock);
916      }
917    }
918  }
919
920  private class CycleDetectingReentrantWriteLock extends ReentrantReadWriteLock.WriteLock {
921
922    @Weak final CycleDetectingReentrantReadWriteLock readWriteLock;
923
924    CycleDetectingReentrantWriteLock(CycleDetectingReentrantReadWriteLock readWriteLock) {
925      super(readWriteLock);
926      this.readWriteLock = readWriteLock;
927    }
928
929    @Override
930    public void lock() {
931      aboutToAcquire(readWriteLock);
932      try {
933        super.lock();
934      } finally {
935        lockStateChanged(readWriteLock);
936      }
937    }
938
939    @Override
940    public void lockInterruptibly() throws InterruptedException {
941      aboutToAcquire(readWriteLock);
942      try {
943        super.lockInterruptibly();
944      } finally {
945        lockStateChanged(readWriteLock);
946      }
947    }
948
949    @Override
950    public boolean tryLock() {
951      aboutToAcquire(readWriteLock);
952      try {
953        return super.tryLock();
954      } finally {
955        lockStateChanged(readWriteLock);
956      }
957    }
958
959    @Override
960    public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
961      aboutToAcquire(readWriteLock);
962      try {
963        return super.tryLock(timeout, unit);
964      } finally {
965        lockStateChanged(readWriteLock);
966      }
967    }
968
969    @Override
970    public void unlock() {
971      try {
972        super.unlock();
973      } finally {
974        lockStateChanged(readWriteLock);
975      }
976    }
977  }
978}