001    /*
002     * Copyright (C) 2011 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.util.concurrent;
018    
019    import com.google.common.annotations.Beta;
020    import com.google.common.base.Functions;
021    import com.google.common.base.Preconditions;
022    import com.google.common.base.Supplier;
023    import com.google.common.collect.Iterables;
024    import com.google.common.collect.MapMaker;
025    import com.google.common.math.IntMath;
026    import com.google.common.primitives.Ints;
027    
028    import java.math.RoundingMode;
029    import java.util.Arrays;
030    import java.util.Collections;
031    import java.util.List;
032    import java.util.concurrent.ConcurrentMap;
033    import java.util.concurrent.Semaphore;
034    import java.util.concurrent.locks.Lock;
035    import java.util.concurrent.locks.ReadWriteLock;
036    import java.util.concurrent.locks.ReentrantLock;
037    import java.util.concurrent.locks.ReentrantReadWriteLock;
038    
039    /**
040     * A striped {@code Lock/Semaphore/ReadWriteLock}. This offers the underlying lock striping
041     * similar to that of {@code ConcurrentHashMap} in a reusable form, and extends it for
042     * semaphores and read-write locks. Conceptually, lock striping is the technique of dividing a lock
043     * into many <i>stripes</i>, increasing the granularity of a single lock and allowing independent
044     * operations to lock different stripes and proceed concurrently, instead of creating contention
045     * for a single lock.
046     *
047     * <p>The guarantee provided by this class is that equal keys lead to the same lock (or semaphore),
048     * i.e. {@code if (key1.equals(key2))} then {@code striped.get(key1) == striped.get(key2)}
049     * (assuming {@link Object#hashCode()} is correctly implemented for the keys). Note
050     * that if {@code key1} is <strong>not</strong> equal to {@code key2}, it is <strong>not</strong>
051     * guaranteed that {@code striped.get(key1) != striped.get(key2)}; the elements might nevertheless
052     * be mapped to the same lock. The lower the number of stripes, the higher the probability of this
053     * happening.
054     *
055     * <p>There are three flavors of this class: {@code Striped<Lock>}, {@code Striped<Semaphore>},
056     * and {@code Striped<ReadWriteLock>}. For each type, two implementations are offered:
057     * {@linkplain #lock(int) strong} and {@linkplain #lazyWeakLock(int) weak}
058     * {@code Striped<Lock>}, {@linkplain #semaphore(int, int) strong} and {@linkplain
059     * #lazyWeakSemaphore(int, int) weak} {@code Striped<Semaphore>}, and {@linkplain
060     * #readWriteLock(int) strong} and {@linkplain #lazyWeakReadWriteLock(int) weak}
061     * {@code Striped<ReadWriteLock>}. <i>Strong</i> means that all stripes (locks/semaphores) are
062     * initialized eagerly, and are not reclaimed unless {@code Striped} itself is reclaimable.
063     * <i>Weak</i> means that locks/semaphores are created lazily, and they are allowed to be reclaimed
064     * if nobody is holding on to them. This is useful, for example, if one wants to create a {@code
065     * Striped<Lock>} of many locks, but worries that in most cases only a small portion of these
066     * would be in use.
067     *
068     * <p>Prior to this class, one might be tempted to use {@code Map<K, Lock>}, where {@code K}
069     * represents the task. This maximizes concurrency by having each unique key mapped to a unique
070     * lock, but also maximizes memory footprint. On the other extreme, one could use a single lock
071     * for all tasks, which minimizes memory footprint but also minimizes concurrency. Instead of
072     * choosing either of these extremes, {@code Striped} allows the user to trade between required
073     * concurrency and memory footprint. For example, if a set of tasks are CPU-bound, one could easily
074     * create a very compact {@code Striped<Lock>} of {@code availableProcessors() * 4} stripes,
075     * instead of possibly thousands of locks which could be created in a {@code Map<K, Lock>}
076     * structure.
077     *
078     * @author Dimitris Andreou
079     */
080    @Beta
081    public abstract class Striped<L> {
082      private Striped() {}
083    
084      /**
085       * Returns the stripe that corresponds to the passed key. It is always guaranteed that if
086       * {@code key1.equals(key2)}, then {@code get(key1) == get(key2)}.
087       *
088       * @param key an arbitrary, non-null key
089       * @return the stripe that the passed key corresponds to
090       */
091      public abstract L get(Object key);
092    
093      /**
094       * Returns the stripe at the specified index. Valid indexes are 0, inclusively, to
095       * {@code size()}, exclusively.
096       *
097       * @param index the index of the stripe to return; must be in {@code [0...size())}
098       * @return the stripe at the specified index
099       */
100      public abstract L getAt(int index);
101    
102      /**
103       * Returns the index to which the given key is mapped, so that getAt(indexFor(key)) == get(key).
104       */
105      abstract int indexFor(Object key);
106    
107      /**
108       * Returns the total number of stripes in this instance.
109       */
110      public abstract int size();
111    
112      /**
113       * Returns the stripes that correspond to the passed objects, in ascending (as per
114       * {@link #getAt(int)}) order. Thus, threads that use the stripes in the order returned
115       * by this method are guaranteed to not deadlock each other.
116       *
117       * <p>It should be noted that using a {@code Striped<L>} with relatively few stripes, and
118       * {@code bulkGet(keys)} with a relative large number of keys can cause an excessive number
119       * of shared stripes (much like the birthday paradox, where much fewer than anticipated birthdays
120       * are needed for a pair of them to match). Please consider carefully the implications of the
121       * number of stripes, the intended concurrency level, and the typical number of keys used in a
122       * {@code bulkGet(keys)} operation. See <a href="http://www.mathpages.com/home/kmath199.htm">Balls
123       * in Bins model</a> for mathematical formulas that can be used to estimate the probability of
124       * collisions.
125       *
126       * @param keys arbitrary non-null keys
127       * @return the stripes corresponding to the objects (one per each object, derived by delegating
128       *         to {@link #get(Object)}; may contain duplicates), in an increasing index order.
129       */
130      public Iterable<L> bulkGet(Iterable<?> keys) {
131        // Initially using the array to store the keys, then reusing it to store the respective L's
132        final Object[] array = Iterables.toArray(keys, Object.class);
133        int[] stripes = new int[array.length];
134        for (int i = 0; i < array.length; i++) {
135          stripes[i] = indexFor(array[i]);
136        }
137        Arrays.sort(stripes);
138        for (int i = 0; i < array.length; i++) {
139          array[i] = getAt(stripes[i]);
140        }
141        /*
142         * Note that the returned Iterable holds references to the returned stripes, to avoid
143         * error-prone code like:
144         *
145         * Striped<Lock> stripedLock = Striped.lazyWeakXXX(...)'
146         * Iterable<Lock> locks = stripedLock.bulkGet(keys);
147         * for (Lock lock : locks) {
148         *   lock.lock();
149         * }
150         * operation();
151         * for (Lock lock : locks) {
152         *   lock.unlock();
153         * }
154         *
155         * If we only held the int[] stripes, translating it on the fly to L's, the original locks
156         * might be garbage collected after locking them, ending up in a huge mess.
157         */
158        @SuppressWarnings("unchecked") // we carefully replaced all keys with their respective L's
159        List<L> asList = (List<L>) Arrays.asList(array);
160        return Collections.unmodifiableList(asList);
161      }
162    
163      // Static factories
164    
165      /**
166       * Creates a {@code Striped<Lock>} with eagerly initialized, strongly referenced locks, with the
167       * specified fairness. Every lock is reentrant.
168       *
169       * @param stripes the minimum number of stripes (locks) required
170       * @return a new {@code Striped<Lock>}
171       */
172      public static Striped<Lock> lock(int stripes) {
173        return new CompactStriped<Lock>(stripes, new Supplier<Lock>() {
174          public Lock get() {
175            return new PaddedLock();
176          }
177        });
178      }
179    
180      /**
181       * Creates a {@code Striped<Lock>} with lazily initialized, weakly referenced locks, with the
182       * specified fairness. Every lock is reentrant.
183       *
184       * @param stripes the minimum number of stripes (locks) required
185       * @return a new {@code Striped<Lock>}
186       */
187      public static Striped<Lock> lazyWeakLock(int stripes) {
188        return new LazyStriped<Lock>(stripes, new Supplier<Lock>() {
189          public Lock get() {
190            return new ReentrantLock(false);
191          }
192        });
193      }
194    
195      /**
196       * Creates a {@code Striped<Semaphore>} with eagerly initialized, strongly referenced semaphores,
197       * with the specified number of permits and fairness.
198       *
199       * @param stripes the minimum number of stripes (semaphores) required
200       * @param permits the number of permits in each semaphore
201       * @return a new {@code Striped<Semaphore>}
202       */
203      public static Striped<Semaphore> semaphore(int stripes, final int permits) {
204        return new CompactStriped<Semaphore>(stripes, new Supplier<Semaphore>() {
205          public Semaphore get() {
206            return new PaddedSemaphore(permits);
207          }
208        });
209      }
210    
211      /**
212       * Creates a {@code Striped<Semaphore>} with lazily initialized, weakly referenced semaphores,
213       * with the specified number of permits and fairness.
214       *
215       * @param stripes the minimum number of stripes (semaphores) required
216       * @param permits the number of permits in each semaphore
217       * @return a new {@code Striped<Semaphore>}
218       */
219      public static Striped<Semaphore> lazyWeakSemaphore(int stripes, final int permits) {
220        return new LazyStriped<Semaphore>(stripes, new Supplier<Semaphore>() {
221          public Semaphore get() {
222            return new Semaphore(permits, false);
223          }
224        });
225      }
226    
227      /**
228       * Creates a {@code Striped<ReadWriteLock>} with eagerly initialized, strongly referenced
229       * read-write locks, with the specified fairness. Every lock is reentrant.
230       *
231       * @param stripes the minimum number of stripes (locks) required
232       * @return a new {@code Striped<ReadWriteLock>}
233       */
234      public static Striped<ReadWriteLock> readWriteLock(int stripes) {
235        return new CompactStriped<ReadWriteLock>(stripes, READ_WRITE_LOCK_SUPPLIER);
236      }
237    
238      /**
239       * Creates a {@code Striped<ReadWriteLock>} with lazily initialized, weakly referenced
240       * read-write locks, with the specified fairness. Every lock is reentrant.
241       *
242       * @param stripes the minimum number of stripes (locks) required
243       * @return a new {@code Striped<ReadWriteLock>}
244       */
245      public static Striped<ReadWriteLock> lazyWeakReadWriteLock(int stripes) {
246        return new LazyStriped<ReadWriteLock>(stripes, READ_WRITE_LOCK_SUPPLIER);
247      }
248    
249      // ReentrantReadWriteLock is large enough to make padding probably unnecessary
250      private static final Supplier<ReadWriteLock> READ_WRITE_LOCK_SUPPLIER =
251          new Supplier<ReadWriteLock>() {
252        public ReadWriteLock get() {
253          return new ReentrantReadWriteLock();
254        }
255      };
256    
257      private abstract static class PowerOfTwoStriped<L> extends Striped<L> {
258        /** Capacity (power of two) minus one, for fast mod evaluation */
259        final int mask;
260    
261        PowerOfTwoStriped(int stripes) {
262          Preconditions.checkArgument(stripes > 0, "Stripes must be positive");
263          this.mask = stripes > Ints.MAX_POWER_OF_TWO ? ALL_SET : ceilToPowerOfTwo(stripes) - 1;
264        }
265    
266        @Override final int indexFor(Object key) {
267          int hash = smear(key.hashCode());
268          return hash & mask;
269        }
270    
271        @Override public final L get(Object key) {
272          return getAt(indexFor(key));
273        }
274      }
275    
276      /**
277       * Implementation of Striped where 2^k stripes are represented as an array of the same length,
278       * eagerly initialized.
279       */
280      private static class CompactStriped<L> extends PowerOfTwoStriped<L> {
281        /** Size is a power of two. */
282        private final Object[] array;
283    
284        private CompactStriped(int stripes, Supplier<L> supplier) {
285          super(stripes);
286          Preconditions.checkArgument(stripes <= Ints.MAX_POWER_OF_TWO, "Stripes must be <= 2^30)");
287    
288          this.array = new Object[mask + 1];
289          for (int i = 0; i < array.length; i++) {
290            array[i] = supplier.get();
291          }
292        }
293    
294        @SuppressWarnings("unchecked") // we only put L's in the array
295        @Override public L getAt(int index) {
296          return (L) array[index];
297        }
298    
299        @Override public int size() {
300          return array.length;
301        }
302      }
303    
304      /**
305       * Implementation of Striped where up to 2^k stripes can be represented, using a Cache
306       * where the key domain is [0..2^k). To map a user key into a stripe, we take a k-bit slice of the
307       * user key's (smeared) hashCode(). The stripes are lazily initialized and are weakly referenced.
308       */
309      private static class LazyStriped<L> extends PowerOfTwoStriped<L> {
310        final ConcurrentMap<Integer, L> cache;
311        final int size;
312    
313        LazyStriped(int stripes, Supplier<L> supplier) {
314          super(stripes);
315          this.size = (mask == ALL_SET) ? Integer.MAX_VALUE : mask + 1;
316          this.cache = new MapMaker().weakValues().makeComputingMap(Functions.forSupplier(supplier));
317        }
318    
319        @Override public L getAt(int index) {
320          Preconditions.checkElementIndex(index, size());
321          return cache.get(index);
322        }
323    
324        @Override public int size() {
325          return size;
326        }
327      }
328    
329      /**
330       * A bit mask were all bits are set.
331       */
332      private static final int ALL_SET = ~0;
333    
334      private static int ceilToPowerOfTwo(int x) {
335        return 1 << IntMath.log2(x, RoundingMode.CEILING);
336      }
337    
338      /*
339       * This method was written by Doug Lea with assistance from members of JCP
340       * JSR-166 Expert Group and released to the public domain, as explained at
341       * http://creativecommons.org/licenses/publicdomain
342       *
343       * As of 2010/06/11, this method is identical to the (package private) hash
344       * method in OpenJDK 7's java.util.HashMap class.
345       */
346      // Copied from java/com/google/common/collect/Hashing.java
347      private static int smear(int hashCode) {
348        hashCode ^= (hashCode >>> 20) ^ (hashCode >>> 12);
349        return hashCode ^ (hashCode >>> 7) ^ (hashCode >>> 4);
350      }
351    
352      private static class PaddedLock extends ReentrantLock {
353        /*
354         * Padding from 40 into 64 bytes, same size as cache line. Might be beneficial to add
355         * a fourth long here, to minimize chance of interference between consecutive locks,
356         * but I couldn't observe any benefit from that.
357         */
358        @SuppressWarnings("unused")
359        long q1, q2, q3;
360    
361        PaddedLock() {
362          super(false);
363        }
364      }
365    
366      private static class PaddedSemaphore extends Semaphore {
367        // See PaddedReentrantLock comment
368        @SuppressWarnings("unused")
369        long q1, q2, q3;
370    
371        PaddedSemaphore(int permits) {
372          super(permits, false);
373        }
374      }
375    }