View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  /*
18   * Written by Doug Lea with assistance from members of JCP JSR-166
19   * Expert Group and released to the public domain, as explained at
20   * http://creativecommons.org/publicdomain/zero/1.0/
21   */
22  
23  package io.netty.util.internal;
24  
25  import io.netty.util.internal.logging.InternalLogger;
26  import io.netty.util.internal.logging.InternalLoggerFactory;
27  
28  import java.lang.Thread.UncaughtExceptionHandler;
29  import java.security.SecureRandom;
30  import java.util.Random;
31  import java.util.concurrent.BlockingQueue;
32  import java.util.concurrent.LinkedBlockingQueue;
33  import java.util.concurrent.TimeUnit;
34  import java.util.concurrent.atomic.AtomicLong;
35  
36  /**
37   * A random number generator isolated to the current thread.  Like the
38   * global {@link java.util.Random} generator used by the {@link
39   * java.lang.Math} class, a {@code ThreadLocalRandom} is initialized
40   * with an internally generated seed that may not otherwise be
41   * modified. When applicable, use of {@code ThreadLocalRandom} rather
42   * than shared {@code Random} objects in concurrent programs will
43   * typically encounter much less overhead and contention.  Use of
44   * {@code ThreadLocalRandom} is particularly appropriate when multiple
45   * tasks (for example, each a {@link io.netty.util.internal.chmv8.ForkJoinTask}) use random numbers
46   * in parallel in thread pools.
47   *
48   * <p>Usages of this class should typically be of the form:
49   * {@code ThreadLocalRandom.current().nextX(...)} (where
50   * {@code X} is {@code Int}, {@code Long}, etc).
51   * When all usages are of this form, it is never possible to
52   * accidently share a {@code ThreadLocalRandom} across multiple threads.
53   *
54   * <p>This class also provides additional commonly used bounded random
55   * generation methods.
56   *
57   * //since 1.7
58   * //author Doug Lea
59   */
60  @SuppressWarnings("all")
61  public final class ThreadLocalRandom extends Random {
62  
63      private static final InternalLogger logger = InternalLoggerFactory.getInstance(ThreadLocalRandom.class);
64  
65      private static final AtomicLong seedUniquifier = new AtomicLong();
66  
67      private static volatile long initialSeedUniquifier =
68              SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0);
69  
70      private static final Thread seedGeneratorThread;
71      private static final BlockingQueue<byte[]> seedQueue;
72      private static final long seedGeneratorStartTime;
73      private static volatile long seedGeneratorEndTime;
74  
75      static {
76          if (initialSeedUniquifier == 0) {
77              // Try to generate a real random number from /dev/random.
78              // Get from a different thread to avoid blocking indefinitely on a machine without much entrophy.
79              seedGeneratorThread = new Thread("initialSeedUniquifierGenerator") {
80                  @Override
81                  public void run() {
82                      final SecureRandom random = new SecureRandom(); // Get the real random seed from /dev/random
83                      final byte[] seed = random.generateSeed(8);
84                      seedGeneratorEndTime = System.nanoTime();
85                      seedQueue.add(seed);
86                  }
87              };
88              seedGeneratorThread.setDaemon(true);
89              seedGeneratorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
90                  @Override
91                  public void uncaughtException(Thread t, Throwable e) {
92                      logger.debug("An exception has been raised by {}", t.getName(), e);
93                  }
94              });
95  
96              seedQueue = new LinkedBlockingQueue<byte[]>();
97              seedGeneratorStartTime = System.nanoTime();
98              seedGeneratorThread.start();
99          } else {
100             seedGeneratorThread = null;
101             seedQueue = null;
102             seedGeneratorStartTime = 0L;
103         }
104     }
105 
106     public static void setInitialSeedUniquifier(long initialSeedUniquifier) {
107         ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
108     }
109 
110     public static long getInitialSeedUniquifier() {
111         // Use the value set via the setter.
112         long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
113         if (initialSeedUniquifier != 0) {
114             return initialSeedUniquifier;
115         }
116 
117         synchronized (ThreadLocalRandom.class) {
118             initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
119             if (initialSeedUniquifier != 0) {
120                 return initialSeedUniquifier;
121             }
122 
123             // Get the random seed from the generator thread with timeout.
124             final long timeoutSeconds = 3;
125             final long deadLine = seedGeneratorStartTime + TimeUnit.SECONDS.toNanos(timeoutSeconds);
126             boolean interrupted = false;
127             for (;;) {
128                 final long waitTime = deadLine - System.nanoTime();
129                 try {
130                     final byte[] seed;
131                     if (waitTime <= 0) {
132                         seed = seedQueue.poll();
133                     } else {
134                         seed = seedQueue.poll(waitTime, TimeUnit.NANOSECONDS);
135                     }
136 
137                     if (seed != null) {
138                         initialSeedUniquifier =
139                                 ((long) seed[0] & 0xff) << 56 |
140                                 ((long) seed[1] & 0xff) << 48 |
141                                 ((long) seed[2] & 0xff) << 40 |
142                                 ((long) seed[3] & 0xff) << 32 |
143                                 ((long) seed[4] & 0xff) << 24 |
144                                 ((long) seed[5] & 0xff) << 16 |
145                                 ((long) seed[6] & 0xff) <<  8 |
146                                 (long) seed[7] & 0xff;
147                         break;
148                     }
149                 } catch (InterruptedException e) {
150                     interrupted = true;
151                     logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException.");
152                     break;
153                 }
154 
155                 if (waitTime <= 0) {
156                     seedGeneratorThread.interrupt();
157                     logger.warn(
158                             "Failed to generate a seed from SecureRandom within {} seconds. " +
159                             "Not enough entrophy?", timeoutSeconds
160                     );
161                     break;
162                 }
163             }
164 
165             // Just in case the initialSeedUniquifier is zero or some other constant
166             initialSeedUniquifier ^= 0x3255ecdc33bae119L; // just a meaningless random number
167             initialSeedUniquifier ^= Long.reverse(System.nanoTime());
168 
169             ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
170 
171             if (interrupted) {
172                 // Restore the interrupt status because we don't know how to/don't need to handle it here.
173                 Thread.currentThread().interrupt();
174 
175                 // Interrupt the generator thread if it's still running,
176                 // in the hope that the SecureRandom provider raises an exception on interruption.
177                 seedGeneratorThread.interrupt();
178             }
179 
180             if (seedGeneratorEndTime == 0) {
181                 seedGeneratorEndTime = System.nanoTime();
182             }
183 
184             return initialSeedUniquifier;
185         }
186     }
187 
188     private static long newSeed() {
189         for (;;) {
190             final long current = seedUniquifier.get();
191             final long actualCurrent = current != 0? current : getInitialSeedUniquifier();
192 
193             // L'Ecuyer, "Tables of Linear Congruential Generators of Different Sizes and Good Lattice Structure", 1999
194             final long next = actualCurrent * 181783497276652981L;
195 
196             if (seedUniquifier.compareAndSet(current, next)) {
197                 if (current == 0 && logger.isDebugEnabled()) {
198                     if (seedGeneratorEndTime != 0) {
199                         logger.debug(String.format(
200                                 "-Dio.netty.initialSeedUniquifier: 0x%016x (took %d ms)",
201                                 actualCurrent,
202                                 TimeUnit.NANOSECONDS.toMillis(seedGeneratorEndTime - seedGeneratorStartTime)));
203                     } else {
204                         logger.debug(String.format("-Dio.netty.initialSeedUniquifier: 0x%016x", actualCurrent));
205                     }
206                 }
207                 return next ^ System.nanoTime();
208             }
209         }
210     }
211 
212     // same constants as Random, but must be redeclared because private
213     private static final long multiplier = 0x5DEECE66DL;
214     private static final long addend = 0xBL;
215     private static final long mask = (1L << 48) - 1;
216 
217     /**
218      * The random seed. We can't use super.seed.
219      */
220     private long rnd;
221 
222     /**
223      * Initialization flag to permit calls to setSeed to succeed only
224      * while executing the Random constructor.  We can't allow others
225      * since it would cause setting seed in one part of a program to
226      * unintentionally impact other usages by the thread.
227      */
228     boolean initialized;
229 
230     // Padding to help avoid memory contention among seed updates in
231     // different TLRs in the common case that they are located near
232     // each other.
233     private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
234 
235     /**
236      * Constructor called only by localRandom.initialValue.
237      */
238     ThreadLocalRandom() {
239         super(newSeed());
240         initialized = true;
241     }
242 
243     /**
244      * Returns the current thread's {@code ThreadLocalRandom}.
245      *
246      * @return the current thread's {@code ThreadLocalRandom}
247      */
248     public static ThreadLocalRandom current() {
249         return InternalThreadLocalMap.get().random();
250     }
251 
252     /**
253      * Throws {@code UnsupportedOperationException}.  Setting seeds in
254      * this generator is not supported.
255      *
256      * @throws UnsupportedOperationException always
257      */
258     public void setSeed(long seed) {
259         if (initialized) {
260             throw new UnsupportedOperationException();
261         }
262         rnd = (seed ^ multiplier) & mask;
263     }
264 
265     protected int next(int bits) {
266         rnd = (rnd * multiplier + addend) & mask;
267         return (int) (rnd >>> (48 - bits));
268     }
269 
270     /**
271      * Returns a pseudorandom, uniformly distributed value between the
272      * given least value (inclusive) and bound (exclusive).
273      *
274      * @param least the least value returned
275      * @param bound the upper bound (exclusive)
276      * @throws IllegalArgumentException if least greater than or equal
277      * to bound
278      * @return the next value
279      */
280     public int nextInt(int least, int bound) {
281         if (least >= bound) {
282             throw new IllegalArgumentException();
283         }
284         return nextInt(bound - least) + least;
285     }
286 
287     /**
288      * Returns a pseudorandom, uniformly distributed value
289      * between 0 (inclusive) and the specified value (exclusive).
290      *
291      * @param n the bound on the random number to be returned.  Must be
292      *        positive.
293      * @return the next value
294      * @throws IllegalArgumentException if n is not positive
295      */
296     public long nextLong(long n) {
297         if (n <= 0) {
298             throw new IllegalArgumentException("n must be positive");
299         }
300 
301         // Divide n by two until small enough for nextInt. On each
302         // iteration (at most 31 of them but usually much less),
303         // randomly choose both whether to include high bit in result
304         // (offset) and whether to continue with the lower vs upper
305         // half (which makes a difference only if odd).
306         long offset = 0;
307         while (n >= Integer.MAX_VALUE) {
308             int bits = next(2);
309             long half = n >>> 1;
310             long nextn = ((bits & 2) == 0) ? half : n - half;
311             if ((bits & 1) == 0) {
312                 offset += n - nextn;
313             }
314             n = nextn;
315         }
316         return offset + nextInt((int) n);
317     }
318 
319     /**
320      * Returns a pseudorandom, uniformly distributed value between the
321      * given least value (inclusive) and bound (exclusive).
322      *
323      * @param least the least value returned
324      * @param bound the upper bound (exclusive)
325      * @return the next value
326      * @throws IllegalArgumentException if least greater than or equal
327      * to bound
328      */
329     public long nextLong(long least, long bound) {
330         if (least >= bound) {
331             throw new IllegalArgumentException();
332         }
333         return nextLong(bound - least) + least;
334     }
335 
336     /**
337      * Returns a pseudorandom, uniformly distributed {@code double} value
338      * between 0 (inclusive) and the specified value (exclusive).
339      *
340      * @param n the bound on the random number to be returned.  Must be
341      *        positive.
342      * @return the next value
343      * @throws IllegalArgumentException if n is not positive
344      */
345     public double nextDouble(double n) {
346         if (n <= 0) {
347             throw new IllegalArgumentException("n must be positive");
348         }
349         return nextDouble() * n;
350     }
351 
352     /**
353      * Returns a pseudorandom, uniformly distributed value between the
354      * given least value (inclusive) and bound (exclusive).
355      *
356      * @param least the least value returned
357      * @param bound the upper bound (exclusive)
358      * @return the next value
359      * @throws IllegalArgumentException if least greater than or equal
360      * to bound
361      */
362     public double nextDouble(double least, double bound) {
363         if (least >= bound) {
364             throw new IllegalArgumentException();
365         }
366         return nextDouble() * (bound - least) + least;
367     }
368 
369     private static final long serialVersionUID = -5851777807851030925L;
370 }