Skip to content

Commit

Permalink
Internal change.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 646915665
  • Loading branch information
java-team-github-bot authored and Google Java Core Libraries committed Jun 26, 2024
1 parent b50df33 commit 4b96e43
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,8 @@ private static List<Striped<?>> strongImplementations() {
Striped.readWriteLock(256),
Striped.lock(100),
Striped.lock(256),
Striped.custom(
100,
new Supplier<Lock>() {
@Override
public Lock get() {
return new ReentrantLock(true);
}
}),
Striped.custom(
256,
new Supplier<Lock>() {
@Override
public Lock get() {
return new ReentrantLock(true);
}
}),
Striped.custom(100, FAIR_LOCK_SUPPLER),
Striped.custom(256, FAIR_LOCK_SUPPLER),
Striped.semaphore(100, 1),
Striped.semaphore(256, 1));
}
Expand All @@ -86,6 +72,14 @@ public Lock get() {
}
};

private static final Supplier<Lock> FAIR_LOCK_SUPPLER =
new Supplier<Lock>() {
@Override
public Lock get() {
return new ReentrantLock(true);
}
};

private static final Supplier<Semaphore> SEMAPHORE_SUPPLER =
new Supplier<Semaphore>() {
@Override
Expand All @@ -108,6 +102,8 @@ private static List<Striped<?>> weakImplementations() {
.add(new Striped.SmallLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
.add(new Striped.LargeLazyStriped<Semaphore>(50, SEMAPHORE_SUPPLER))
.add(new Striped.LargeLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
.add(Striped.lazyWeakCustom(50, FAIR_LOCK_SUPPLER))
.add(Striped.lazyWeakCustom(64, FAIR_LOCK_SUPPLER))
.build();
}

Expand Down
16 changes: 12 additions & 4 deletions android/guava/src/com/google/common/util/concurrent/Striped.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,18 @@ public static Striped<Lock> lock(int stripes) {
* @return a new {@code Striped<Lock>}
*/
public static Striped<Lock> lazyWeakLock(int stripes) {
return lazy(stripes, () -> new ReentrantLock(false));
return lazyWeakCustom(stripes, () -> new ReentrantLock(false));
}

private static <L> Striped<L> lazy(int stripes, Supplier<L> supplier) {
/**
* Creates a {@code Striped<L>} with lazily initialized, weakly referenced locks. Every lock is
* obtained from the passed supplier.
*
* @param stripes the minimum number of stripes (locks) required
* @param supplier a {@code Supplier<L>} object to obtain locks from
* @return a new {@code Striped<L>}
*/
static <L> Striped<L> lazyWeakCustom(int stripes, Supplier<L> supplier) {
return stripes < LARGE_LAZY_CUTOFF
? new SmallLazyStriped<L>(stripes, supplier)
: new LargeLazyStriped<L>(stripes, supplier);
Expand All @@ -247,7 +255,7 @@ public static Striped<Semaphore> semaphore(int stripes, int permits) {
* @return a new {@code Striped<Semaphore>}
*/
public static Striped<Semaphore> lazyWeakSemaphore(int stripes, int permits) {
return lazy(stripes, () -> new Semaphore(permits, false));
return lazyWeakCustom(stripes, () -> new Semaphore(permits, false));
}

/**
Expand All @@ -269,7 +277,7 @@ public static Striped<ReadWriteLock> readWriteLock(int stripes) {
* @return a new {@code Striped<ReadWriteLock>}
*/
public static Striped<ReadWriteLock> lazyWeakReadWriteLock(int stripes) {
return lazy(stripes, WeakSafeReadWriteLock::new);
return lazyWeakCustom(stripes, WeakSafeReadWriteLock::new);
}
/**
* ReadWriteLock implementation whose read and write locks retain a reference back to this lock.
Expand Down
28 changes: 12 additions & 16 deletions guava-tests/test/com/google/common/util/concurrent/StripedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,8 @@ private static List<Striped<?>> strongImplementations() {
Striped.readWriteLock(256),
Striped.lock(100),
Striped.lock(256),
Striped.custom(
100,
new Supplier<Lock>() {
@Override
public Lock get() {
return new ReentrantLock(true);
}
}),
Striped.custom(
256,
new Supplier<Lock>() {
@Override
public Lock get() {
return new ReentrantLock(true);
}
}),
Striped.custom(100, FAIR_LOCK_SUPPLER),
Striped.custom(256, FAIR_LOCK_SUPPLER),
Striped.semaphore(100, 1),
Striped.semaphore(256, 1));
}
Expand All @@ -86,6 +72,14 @@ public Lock get() {
}
};

private static final Supplier<Lock> FAIR_LOCK_SUPPLER =
new Supplier<Lock>() {
@Override
public Lock get() {
return new ReentrantLock(true);
}
};

private static final Supplier<Semaphore> SEMAPHORE_SUPPLER =
new Supplier<Semaphore>() {
@Override
Expand All @@ -108,6 +102,8 @@ private static List<Striped<?>> weakImplementations() {
.add(new Striped.SmallLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
.add(new Striped.LargeLazyStriped<Semaphore>(50, SEMAPHORE_SUPPLER))
.add(new Striped.LargeLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
.add(Striped.lazyWeakCustom(50, FAIR_LOCK_SUPPLER))
.add(Striped.lazyWeakCustom(64, FAIR_LOCK_SUPPLER))
.build();
}

Expand Down
16 changes: 12 additions & 4 deletions guava/src/com/google/common/util/concurrent/Striped.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,18 @@ public static Striped<Lock> lock(int stripes) {
* @return a new {@code Striped<Lock>}
*/
public static Striped<Lock> lazyWeakLock(int stripes) {
return lazy(stripes, () -> new ReentrantLock(false));
return lazyWeakCustom(stripes, () -> new ReentrantLock(false));
}

private static <L> Striped<L> lazy(int stripes, Supplier<L> supplier) {
/**
* Creates a {@code Striped<L>} with lazily initialized, weakly referenced locks. Every lock is
* obtained from the passed supplier.
*
* @param stripes the minimum number of stripes (locks) required
* @param supplier a {@code Supplier<L>} object to obtain locks from
* @return a new {@code Striped<L>}
*/
static <L> Striped<L> lazyWeakCustom(int stripes, Supplier<L> supplier) {
return stripes < LARGE_LAZY_CUTOFF
? new SmallLazyStriped<L>(stripes, supplier)
: new LargeLazyStriped<L>(stripes, supplier);
Expand All @@ -247,7 +255,7 @@ public static Striped<Semaphore> semaphore(int stripes, int permits) {
* @return a new {@code Striped<Semaphore>}
*/
public static Striped<Semaphore> lazyWeakSemaphore(int stripes, int permits) {
return lazy(stripes, () -> new Semaphore(permits, false));
return lazyWeakCustom(stripes, () -> new Semaphore(permits, false));
}

/**
Expand All @@ -269,7 +277,7 @@ public static Striped<ReadWriteLock> readWriteLock(int stripes) {
* @return a new {@code Striped<ReadWriteLock>}
*/
public static Striped<ReadWriteLock> lazyWeakReadWriteLock(int stripes) {
return lazy(stripes, WeakSafeReadWriteLock::new);
return lazyWeakCustom(stripes, WeakSafeReadWriteLock::new);
}
/**
* ReadWriteLock implementation whose read and write locks retain a reference back to this lock.
Expand Down

0 comments on commit 4b96e43

Please sign in to comment.