CycleDetectingLockFactory.java 17.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
package com.google.common.util.concurrent;

import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.MapMaker;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

@Beta
@ThreadSafe
public class CycleDetectingLockFactory
{
  private static final ConcurrentMap<Class<? extends Enum>, Map<? extends Enum, g>> b = new MapMaker().weakKeys().makeMap();
  private static final Logger c = Logger.getLogger(CycleDetectingLockFactory.class.getName());
  private static final ThreadLocal<ArrayList<g>> d = new ThreadLocal() {};
  final Policy a;
  
  private CycleDetectingLockFactory(Policy paramPolicy)
  {
    this.a = ((Policy)Preconditions.checkNotNull(paramPolicy));
  }
  
  public static CycleDetectingLockFactory newInstance(Policy paramPolicy)
  {
    return new CycleDetectingLockFactory(paramPolicy);
  }
  
  public static <E extends Enum<E>> WithExplicitOrdering<E> newInstanceWithExplicitOrdering(Class<E> paramClass, Policy paramPolicy)
  {
    int k = 0;
    Preconditions.checkNotNull(paramClass);
    Preconditions.checkNotNull(paramPolicy);
    Object localObject1 = (Map)b.get(paramClass);
    if (localObject1 != null) {}
    for (paramClass = (Class<E>)localObject1;; paramClass = (Map)MoreObjects.firstNonNull((Map)b.putIfAbsent(paramClass, localObject1), localObject1))
    {
      return new WithExplicitOrdering(paramPolicy, paramClass);
      localObject1 = Maps.newEnumMap(paramClass);
      Enum[] arrayOfEnum = (Enum[])paramClass.getEnumConstants();
      int m = arrayOfEnum.length;
      ArrayList localArrayList = Lists.newArrayListWithCapacity(m);
      int j = arrayOfEnum.length;
      int i = 0;
      while (i < j)
      {
        Enum localEnum = arrayOfEnum[i];
        Object localObject2 = String.valueOf(String.valueOf(localEnum.getDeclaringClass().getSimpleName()));
        String str = String.valueOf(String.valueOf(localEnum.name()));
        localObject2 = new g(((String)localObject2).length() + 1 + str.length() + (String)localObject2 + "." + str);
        localArrayList.add(localObject2);
        ((EnumMap)localObject1).put(localEnum, localObject2);
        i += 1;
      }
      i = 1;
      for (;;)
      {
        j = k;
        if (i >= m) {
          break;
        }
        ((g)localArrayList.get(i)).a(Policies.THROW, localArrayList.subList(0, i));
        i += 1;
      }
      while (j < m - 1)
      {
        ((g)localArrayList.get(j)).a(Policies.DISABLED, localArrayList.subList(j + 1, m));
        j += 1;
      }
      localObject1 = Collections.unmodifiableMap((Map)localObject1);
    }
  }
  
  public ReentrantLock newReentrantLock(String paramString)
  {
    return newReentrantLock(paramString, false);
  }
  
  public ReentrantLock newReentrantLock(String paramString, boolean paramBoolean)
  {
    if (this.a == Policies.DISABLED) {
      return new ReentrantLock(paramBoolean);
    }
    return new b(new g(paramString), paramBoolean, (byte)0);
  }
  
  public ReentrantReadWriteLock newReentrantReadWriteLock(String paramString)
  {
    return newReentrantReadWriteLock(paramString, false);
  }
  
  public ReentrantReadWriteLock newReentrantReadWriteLock(String paramString, boolean paramBoolean)
  {
    if (this.a == Policies.DISABLED) {
      return new ReentrantReadWriteLock(paramBoolean);
    }
    return new d(new g(paramString), paramBoolean, (byte)0);
  }
  
  @Beta
  public static abstract enum Policies
    implements CycleDetectingLockFactory.Policy
  {
    private Policies() {}
  }
  
  @Beta
  @ThreadSafe
  public static abstract interface Policy
  {
    public abstract void handlePotentialDeadlock(CycleDetectingLockFactory.PotentialDeadlockException paramPotentialDeadlockException);
  }
  
  @Beta
  public static final class PotentialDeadlockException
    extends CycleDetectingLockFactory.f
  {
    private final CycleDetectingLockFactory.f c;
    
    private PotentialDeadlockException(CycleDetectingLockFactory.g paramg1, CycleDetectingLockFactory.g paramg2, CycleDetectingLockFactory.f paramf)
    {
      super(paramg2);
      this.c = paramf;
      initCause(paramf);
    }
    
    public final CycleDetectingLockFactory.f getConflictingStackTrace()
    {
      return this.c;
    }
    
    public final String getMessage()
    {
      StringBuilder localStringBuilder = new StringBuilder(super.getMessage());
      for (Object localObject = this.c; localObject != null; localObject = ((Throwable)localObject).getCause()) {
        localStringBuilder.append(", ").append(((Throwable)localObject).getMessage());
      }
      return localStringBuilder.toString();
    }
  }
  
  @Beta
  public static final class WithExplicitOrdering<E extends Enum<E>>
    extends CycleDetectingLockFactory
  {
    private final Map<E, CycleDetectingLockFactory.g> b;
    
    @VisibleForTesting
    WithExplicitOrdering(CycleDetectingLockFactory.Policy paramPolicy, Map<E, CycleDetectingLockFactory.g> paramMap)
    {
      super((byte)0);
      this.b = paramMap;
    }
    
    public final ReentrantLock newReentrantLock(E paramE)
    {
      return newReentrantLock(paramE, false);
    }
    
    public final ReentrantLock newReentrantLock(E paramE, boolean paramBoolean)
    {
      if (this.a == CycleDetectingLockFactory.Policies.DISABLED) {
        return new ReentrantLock(paramBoolean);
      }
      return new CycleDetectingLockFactory.b(this, (CycleDetectingLockFactory.g)this.b.get(paramE), paramBoolean, (byte)0);
    }
    
    public final ReentrantReadWriteLock newReentrantReadWriteLock(E paramE)
    {
      return newReentrantReadWriteLock(paramE, false);
    }
    
    public final ReentrantReadWriteLock newReentrantReadWriteLock(E paramE, boolean paramBoolean)
    {
      if (this.a == CycleDetectingLockFactory.Policies.DISABLED) {
        return new ReentrantReadWriteLock(paramBoolean);
      }
      return new CycleDetectingLockFactory.d(this, (CycleDetectingLockFactory.g)this.b.get(paramE), paramBoolean, (byte)0);
    }
  }
  
  static abstract interface a
  {
    public abstract CycleDetectingLockFactory.g a();
    
    public abstract boolean b();
  }
  
  final class b
    extends ReentrantLock
    implements CycleDetectingLockFactory.a
  {
    private final CycleDetectingLockFactory.g b;
    
    private b(CycleDetectingLockFactory.g paramg, boolean paramBoolean)
    {
      super();
      this.b = ((CycleDetectingLockFactory.g)Preconditions.checkNotNull(paramg));
    }
    
    public final CycleDetectingLockFactory.g a()
    {
      return this.b;
    }
    
    public final boolean b()
    {
      return isHeldByCurrentThread();
    }
    
    public final void lock()
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this);
      try
      {
        super.lock();
        return;
      }
      finally
      {
        CycleDetectingLockFactory.a(this);
      }
    }
    
    public final void lockInterruptibly()
      throws InterruptedException
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this);
      try
      {
        super.lockInterruptibly();
        return;
      }
      finally
      {
        CycleDetectingLockFactory.a(this);
      }
    }
    
    public final boolean tryLock()
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this);
      try
      {
        boolean bool = super.tryLock();
        return bool;
      }
      finally
      {
        CycleDetectingLockFactory.a(this);
      }
    }
    
    public final boolean tryLock(long paramLong, TimeUnit paramTimeUnit)
      throws InterruptedException
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this);
      try
      {
        boolean bool = super.tryLock(paramLong, paramTimeUnit);
        return bool;
      }
      finally
      {
        CycleDetectingLockFactory.a(this);
      }
    }
    
    public final void unlock()
    {
      try
      {
        super.unlock();
        return;
      }
      finally
      {
        CycleDetectingLockFactory.a(this);
      }
    }
  }
  
  final class c
    extends ReentrantReadWriteLock.ReadLock
  {
    final CycleDetectingLockFactory.d a;
    
    c(CycleDetectingLockFactory.d paramd)
    {
      super();
      this.a = paramd;
    }
    
    public final void lock()
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this.a);
      try
      {
        super.lock();
        return;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
    
    public final void lockInterruptibly()
      throws InterruptedException
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this.a);
      try
      {
        super.lockInterruptibly();
        return;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
    
    public final boolean tryLock()
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this.a);
      try
      {
        boolean bool = super.tryLock();
        return bool;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
    
    public final boolean tryLock(long paramLong, TimeUnit paramTimeUnit)
      throws InterruptedException
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this.a);
      try
      {
        boolean bool = super.tryLock(paramLong, paramTimeUnit);
        return bool;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
    
    public final void unlock()
    {
      try
      {
        super.unlock();
        return;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
  }
  
  final class d
    extends ReentrantReadWriteLock
    implements CycleDetectingLockFactory.a
  {
    private final CycleDetectingLockFactory.c b = new CycleDetectingLockFactory.c(CycleDetectingLockFactory.this, this);
    private final CycleDetectingLockFactory.e c = new CycleDetectingLockFactory.e(CycleDetectingLockFactory.this, this);
    private final CycleDetectingLockFactory.g d;
    
    private d(CycleDetectingLockFactory.g paramg, boolean paramBoolean)
    {
      super();
      this.d = ((CycleDetectingLockFactory.g)Preconditions.checkNotNull(paramg));
    }
    
    public final CycleDetectingLockFactory.g a()
    {
      return this.d;
    }
    
    public final boolean b()
    {
      return (isWriteLockedByCurrentThread()) || (getReadHoldCount() > 0);
    }
    
    public final ReentrantReadWriteLock.ReadLock readLock()
    {
      return this.b;
    }
    
    public final ReentrantReadWriteLock.WriteLock writeLock()
    {
      return this.c;
    }
  }
  
  final class e
    extends ReentrantReadWriteLock.WriteLock
  {
    final CycleDetectingLockFactory.d a;
    
    e(CycleDetectingLockFactory.d paramd)
    {
      super();
      this.a = paramd;
    }
    
    public final void lock()
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this.a);
      try
      {
        super.lock();
        return;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
    
    public final void lockInterruptibly()
      throws InterruptedException
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this.a);
      try
      {
        super.lockInterruptibly();
        return;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
    
    public final boolean tryLock()
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this.a);
      try
      {
        boolean bool = super.tryLock();
        return bool;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
    
    public final boolean tryLock(long paramLong, TimeUnit paramTimeUnit)
      throws InterruptedException
    {
      CycleDetectingLockFactory.a(CycleDetectingLockFactory.this, this.a);
      try
      {
        boolean bool = super.tryLock(paramLong, paramTimeUnit);
        return bool;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
    
    public final void unlock()
    {
      try
      {
        super.unlock();
        return;
      }
      finally
      {
        CycleDetectingLockFactory.a(this.a);
      }
    }
  }
  
  static class f
    extends IllegalStateException
  {
    static final StackTraceElement[] a = new StackTraceElement[0];
    static Set<String> b = ImmutableSet.of(CycleDetectingLockFactory.class.getName(), f.class.getName(), CycleDetectingLockFactory.g.class.getName());
    
    f(CycleDetectingLockFactory.g paramg1, CycleDetectingLockFactory.g paramg2)
    {
      super();
      paramg1 = getStackTrace();
      int i = 0;
      int j = paramg1.length;
      for (;;)
      {
        if (i < j)
        {
          if (CycleDetectingLockFactory.WithExplicitOrdering.class.getName().equals(paramg1[i].getClassName())) {
            setStackTrace(a);
          }
        }
        else {
          return;
        }
        if (!b.contains(paramg1[i].getClassName()))
        {
          setStackTrace((StackTraceElement[])Arrays.copyOfRange(paramg1, i, j));
          return;
        }
        i += 1;
      }
    }
  }
  
  static class g
  {
    final Map<g, CycleDetectingLockFactory.f> a = new MapMaker().weakKeys().makeMap();
    final Map<g, CycleDetectingLockFactory.PotentialDeadlockException> b = new MapMaker().weakKeys().makeMap();
    final String c;
    
    g(String paramString)
    {
      this.c = ((String)Preconditions.checkNotNull(paramString));
    }
    
    @Nullable
    private CycleDetectingLockFactory.f a(g paramg, Set<g> paramSet)
    {
      Object localObject;
      if (!paramSet.add(this)) {
        localObject = null;
      }
      CycleDetectingLockFactory.f localf;
      do
      {
        return (CycleDetectingLockFactory.f)localObject;
        localf = (CycleDetectingLockFactory.f)this.a.get(paramg);
        localObject = localf;
      } while (localf != null);
      Iterator localIterator = this.a.entrySet().iterator();
      while (localIterator.hasNext())
      {
        localObject = (Map.Entry)localIterator.next();
        g localg = (g)((Map.Entry)localObject).getKey();
        localf = localg.a(paramg, paramSet);
        if (localf != null)
        {
          paramg = new CycleDetectingLockFactory.f(localg, this);
          paramg.setStackTrace(((CycleDetectingLockFactory.f)((Map.Entry)localObject).getValue()).getStackTrace());
          paramg.initCause(localf);
          return paramg;
        }
      }
      return null;
    }
    
    final void a(CycleDetectingLockFactory.Policy paramPolicy, List<g> paramList)
    {
      int j = paramList.size();
      int i = 0;
      if (i < j)
      {
        g localg = (g)paramList.get(i);
        boolean bool;
        label37:
        Object localObject;
        if (this != localg)
        {
          bool = true;
          localObject = String.valueOf(localg.c);
          if (((String)localObject).length() == 0) {
            break label141;
          }
          localObject = "Attempted to acquire multiple locks with the same rank ".concat((String)localObject);
          label64:
          Preconditions.checkState(bool, localObject);
          if (!this.a.containsKey(localg))
          {
            localObject = (CycleDetectingLockFactory.PotentialDeadlockException)this.b.get(localg);
            if (localObject == null) {
              break label155;
            }
            paramPolicy.handlePotentialDeadlock(new CycleDetectingLockFactory.PotentialDeadlockException(localg, this, ((CycleDetectingLockFactory.PotentialDeadlockException)localObject).getConflictingStackTrace(), (byte)0));
          }
        }
        for (;;)
        {
          i += 1;
          break;
          bool = false;
          break label37;
          label141:
          localObject = new String("Attempted to acquire multiple locks with the same rank ");
          break label64;
          label155:
          localObject = localg.a(this, Sets.newIdentityHashSet());
          if (localObject == null)
          {
            this.a.put(localg, new CycleDetectingLockFactory.f(localg, this));
          }
          else
          {
            localObject = new CycleDetectingLockFactory.PotentialDeadlockException(localg, this, (CycleDetectingLockFactory.f)localObject, (byte)0);
            this.b.put(localg, localObject);
            paramPolicy.handlePotentialDeadlock((CycleDetectingLockFactory.PotentialDeadlockException)localObject);
          }
        }
      }
    }
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/com/google/common/util/concurrent/CycleDetectingLockFactory.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */