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  
69      private static final Thread seedGeneratorThread;
70      private static final BlockingQueue<Long> seedQueue;
71      private static final long seedGeneratorStartTime;
72      private static volatile long seedGeneratorEndTime;
73  
74      static {
75          initialSeedUniquifier = SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0);
76          if (initialSeedUniquifier == 0) {
77              boolean secureRandom = SystemPropertyUtil.getBoolean("java.util.secureRandomSeed", false);
78              if (secureRandom) {
79                  seedQueue = new LinkedBlockingQueue<Long>();
80                  seedGeneratorStartTime = System.nanoTime();
81  
82                  // Try to generate a real random number from /dev/random.
83                  // Get from a different thread to avoid blocking indefinitely on a machine without much entropy.
84                  seedGeneratorThread = new Thread("initialSeedUniquifierGenerator") {
85                      @Override
86                      public void run() {
87                          final SecureRandom random = new SecureRandom(); // Get the real random seed from /dev/random
88                          final byte[] seed = random.generateSeed(8);
89                          seedGeneratorEndTime = System.nanoTime();
90                          long s = ((long) seed[0] & 0xff) << 56 |
91                                   ((long) seed[1] & 0xff) << 48 |
92                                   ((long) seed[2] & 0xff) << 40 |
93                                   ((long) seed[3] & 0xff) << 32 |
94                                   ((long) seed[4] & 0xff) << 24 |
95                                   ((long) seed[5] & 0xff) << 16 |
96                                   ((long) seed[6] & 0xff) <<  8 |
97                                   (long) seed[7] & 0xff;
98                          seedQueue.add(s);
99                      }
100                 };
101                 seedGeneratorThread.setDaemon(true);
102                 seedGeneratorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
103                     @Override
104                     public void uncaughtException(Thread t, Throwable e) {
105                         logger.debug("An exception has been raised by {}", t.getName(), e);
106                     }
107                 });
108                 seedGeneratorThread.start();
109             } else {
110                 initialSeedUniquifier = mix64(System.currentTimeMillis()) ^ mix64(System.nanoTime());
111                 seedGeneratorThread = null;
112                 seedQueue = null;
113                 seedGeneratorStartTime = 0L;
114             }
115         } else {
116             seedGeneratorThread = null;
117             seedQueue = null;
118             seedGeneratorStartTime = 0L;
119         }
120     }
121 
122     public static void setInitialSeedUniquifier(long initialSeedUniquifier) {
123         ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
124     }
125 
126     public static long getInitialSeedUniquifier() {
127         // Use the value set via the setter.
128         long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
129         if (initialSeedUniquifier != 0) {
130             return initialSeedUniquifier;
131         }
132 
133         synchronized (ThreadLocalRandom.class) {
134             initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
135             if (initialSeedUniquifier != 0) {
136                 return initialSeedUniquifier;
137             }
138 
139             // Get the random seed from the generator thread with timeout.
140             final long timeoutSeconds = 3;
141             final long deadLine = seedGeneratorStartTime + TimeUnit.SECONDS.toNanos(timeoutSeconds);
142             boolean interrupted = false;
143             for (;;) {
144                 final long waitTime = deadLine - System.nanoTime();
145                 try {
146                     final Long seed;
147                     if (waitTime <= 0) {
148                         seed = seedQueue.poll();
149                     } else {
150                         seed = seedQueue.poll(waitTime, TimeUnit.NANOSECONDS);
151                     }
152 
153                     if (seed != null) {
154                         initialSeedUniquifier = seed;
155                         break;
156                     }
157                 } catch (InterruptedException e) {
158                     interrupted = true;
159                     logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException.");
160                     break;
161                 }
162 
163                 if (waitTime <= 0) {
164                     seedGeneratorThread.interrupt();
165                     logger.warn(
166                             "Failed to generate a seed from SecureRandom within {} seconds. " +
167                             "Not enough entropy?", timeoutSeconds
168                     );
169                     break;
170                 }
171             }
172 
173             // Just in case the initialSeedUniquifier is zero or some other constant
174             initialSeedUniquifier ^= 0x3255ecdc33bae119L; // just a meaningless random number
175             initialSeedUniquifier ^= Long.reverse(System.nanoTime());
176 
177             ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
178 
179             if (interrupted) {
180                 // Restore the interrupt status because we don't know how to/don't need to handle it here.
181                 Thread.currentThread().interrupt();
182 
183                 // Interrupt the generator thread if it's still running,
184                 // in the hope that the SecureRandom provider raises an exception on interruption.
185                 seedGeneratorThread.interrupt();
186             }
187 
188             if (seedGeneratorEndTime == 0) {
189                 seedGeneratorEndTime = System.nanoTime();
190             }
191 
192             return initialSeedUniquifier;
193         }
194     }
195 
196     private static long newSeed() {
197         for (;;) {
198             final long current = seedUniquifier.get();
199             final long actualCurrent = current != 0? current : getInitialSeedUniquifier();
200 
201             // L'Ecuyer, "Tables of Linear Congruential Generators of Different Sizes and Good Lattice Structure", 1999
202             final long next = actualCurrent * 181783497276652981L;
203 
204             if (seedUniquifier.compareAndSet(current, next)) {
205                 if (current == 0 && logger.isDebugEnabled()) {
206                     if (seedGeneratorEndTime != 0) {
207                         logger.debug(String.format(
208                                 "-Dio.netty.initialSeedUniquifier: 0x%016x (took %d ms)",
209                                 actualCurrent,
210                                 TimeUnit.NANOSECONDS.toMillis(seedGeneratorEndTime - seedGeneratorStartTime)));
211                     } else {
212                         logger.debug(String.format("-Dio.netty.initialSeedUniquifier: 0x%016x", actualCurrent));
213                     }
214                 }
215                 return next ^ System.nanoTime();
216             }
217         }
218     }
219 
220     // Borrowed from
221     // http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ThreadLocalRandom.java
222     private static long mix64(long z) {
223         z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
224         z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
225         return z ^ (z >>> 33);
226     }
227 
228     // same constants as Random, but must be redeclared because private
229     private static final long multiplier = 0x5DEECE66DL;
230     private static final long addend = 0xBL;
231     private static final long mask = (1L << 48) - 1;
232 
233     /**
234      * The random seed. We can't use super.seed.
235      */
236     private long rnd;
237 
238     /**
239      * Initialization flag to permit calls to setSeed to succeed only
240      * while executing the Random constructor.  We can't allow others
241      * since it would cause setting seed in one part of a program to
242      * unintentionally impact other usages by the thread.
243      */
244     boolean initialized;
245 
246     // Padding to help avoid memory contention among seed updates in
247     // different TLRs in the common case that they are located near
248     // each other.
249     private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
250 
251     /**
252      * Constructor called only by localRandom.initialValue.
253      */
254     ThreadLocalRandom() {
255         super(newSeed());
256         initialized = true;
257     }
258 
259     /**
260      * Returns the current thread's {@code ThreadLocalRandom}.
261      *
262      * @return the current thread's {@code ThreadLocalRandom}
263      */
264     public static ThreadLocalRandom current() {
265         return InternalThreadLocalMap.get().random();
266     }
267 
268     /**
269      * Throws {@code UnsupportedOperationException}.  Setting seeds in
270      * this generator is not supported.
271      *
272      * @throws UnsupportedOperationException always
273      */
274     public void setSeed(long seed) {
275         if (initialized) {
276             throw new UnsupportedOperationException();
277         }
278         rnd = (seed ^ multiplier) & mask;
279     }
280 
281     protected int next(int bits) {
282         rnd = (rnd * multiplier + addend) & mask;
283         return (int) (rnd >>> (48 - bits));
284     }
285 
286     /**
287      * Returns a pseudorandom, uniformly distributed value between the
288      * given least value (inclusive) and bound (exclusive).
289      *
290      * @param least the least value returned
291      * @param bound the upper bound (exclusive)
292      * @throws IllegalArgumentException if least greater than or equal
293      * to bound
294      * @return the next value
295      */
296     public int nextInt(int least, int bound) {
297         if (least >= bound) {
298             throw new IllegalArgumentException();
299         }
300         return nextInt(bound - least) + least;
301     }
302 
303     /**
304      * Returns a pseudorandom, uniformly distributed value
305      * between 0 (inclusive) and the specified value (exclusive).
306      *
307      * @param n the bound on the random number to be returned.  Must be
308      *        positive.
309      * @return the next value
310      * @throws IllegalArgumentException if n is not positive
311      */
312     public long nextLong(long n) {
313         if (n <= 0) {
314             throw new IllegalArgumentException("n must be positive");
315         }
316 
317         // Divide n by two until small enough for nextInt. On each
318         // iteration (at most 31 of them but usually much less),
319         // randomly choose both whether to include high bit in result
320         // (offset) and whether to continue with the lower vs upper
321         // half (which makes a difference only if odd).
322         long offset = 0;
323         while (n >= Integer.MAX_VALUE) {
324             int bits = next(2);
325             long half = n >>> 1;
326             long nextn = ((bits & 2) == 0) ? half : n - half;
327             if ((bits & 1) == 0) {
328                 offset += n - nextn;
329             }
330             n = nextn;
331         }
332         return offset + nextInt((int) n);
333     }
334 
335     /**
336      * Returns a pseudorandom, uniformly distributed value between the
337      * given least value (inclusive) and bound (exclusive).
338      *
339      * @param least the least value returned
340      * @param bound the upper bound (exclusive)
341      * @return the next value
342      * @throws IllegalArgumentException if least greater than or equal
343      * to bound
344      */
345     public long nextLong(long least, long bound) {
346         if (least >= bound) {
347             throw new IllegalArgumentException();
348         }
349         return nextLong(bound - least) + least;
350     }
351 
352     /**
353      * Returns a pseudorandom, uniformly distributed {@code double} value
354      * between 0 (inclusive) and the specified value (exclusive).
355      *
356      * @param n the bound on the random number to be returned.  Must be
357      *        positive.
358      * @return the next value
359      * @throws IllegalArgumentException if n is not positive
360      */
361     public double nextDouble(double n) {
362         if (n <= 0) {
363             throw new IllegalArgumentException("n must be positive");
364         }
365         return nextDouble() * n;
366     }
367 
368     /**
369      * Returns a pseudorandom, uniformly distributed value between the
370      * given least value (inclusive) and bound (exclusive).
371      *
372      * @param least the least value returned
373      * @param bound the upper bound (exclusive)
374      * @return the next value
375      * @throws IllegalArgumentException if least greater than or equal
376      * to bound
377      */
378     public double nextDouble(double least, double bound) {
379         if (least >= bound) {
380             throw new IllegalArgumentException();
381         }
382         return nextDouble() * (bound - least) + least;
383     }
384 
385     private static final long serialVersionUID = -5851777807851030925L;
386 }