1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package io.netty.util.internal;
24
25 import static io.netty.util.internal.ObjectUtil.checkPositive;
26
27 import io.netty.util.internal.logging.InternalLogger;
28 import io.netty.util.internal.logging.InternalLoggerFactory;
29
30 import java.lang.Thread.UncaughtExceptionHandler;
31 import java.security.SecureRandom;
32 import java.util.Random;
33 import java.util.concurrent.BlockingQueue;
34 import java.util.concurrent.LinkedBlockingQueue;
35 import java.util.concurrent.TimeUnit;
36 import java.util.concurrent.atomic.AtomicLong;
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 @SuppressWarnings("all")
63 public final class ThreadLocalRandom extends Random {
64
65 private static final InternalLogger logger = InternalLoggerFactory.getInstance(ThreadLocalRandom.class);
66
67 private static final AtomicLong seedUniquifier = new AtomicLong();
68
69 private static volatile long initialSeedUniquifier;
70
71 private static final Thread seedGeneratorThread;
72 private static final BlockingQueue<Long> seedQueue;
73 private static final long seedGeneratorStartTime;
74 private static volatile long seedGeneratorEndTime;
75
76 static {
77 initialSeedUniquifier = SystemPropertyUtil.getLong("io.netty.initialSeedUniquifier", 0);
78 if (initialSeedUniquifier == 0) {
79 boolean secureRandom = SystemPropertyUtil.getBoolean("java.util.secureRandomSeed", false);
80 if (secureRandom) {
81 seedQueue = new LinkedBlockingQueue<Long>();
82 seedGeneratorStartTime = System.nanoTime();
83
84
85
86 seedGeneratorThread = new Thread("initialSeedUniquifierGenerator") {
87 @Override
88 public void run() {
89 final SecureRandom random = new SecureRandom();
90 final byte[] seed = random.generateSeed(8);
91 seedGeneratorEndTime = System.nanoTime();
92 long s = ((long) seed[0] & 0xff) << 56 |
93 ((long) seed[1] & 0xff) << 48 |
94 ((long) seed[2] & 0xff) << 40 |
95 ((long) seed[3] & 0xff) << 32 |
96 ((long) seed[4] & 0xff) << 24 |
97 ((long) seed[5] & 0xff) << 16 |
98 ((long) seed[6] & 0xff) << 8 |
99 (long) seed[7] & 0xff;
100 seedQueue.add(s);
101 }
102 };
103 seedGeneratorThread.setDaemon(true);
104 seedGeneratorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
105 @Override
106 public void uncaughtException(Thread t, Throwable e) {
107 logger.debug("An exception has been raised by {}", t.getName(), e);
108 }
109 });
110 seedGeneratorThread.start();
111 } else {
112 initialSeedUniquifier = mix64(System.currentTimeMillis()) ^ mix64(System.nanoTime());
113 seedGeneratorThread = null;
114 seedQueue = null;
115 seedGeneratorStartTime = 0L;
116 }
117 } else {
118 seedGeneratorThread = null;
119 seedQueue = null;
120 seedGeneratorStartTime = 0L;
121 }
122 }
123
124 public static void setInitialSeedUniquifier(long initialSeedUniquifier) {
125 ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
126 }
127
128 public static long getInitialSeedUniquifier() {
129
130 long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
131 if (initialSeedUniquifier != 0) {
132 return initialSeedUniquifier;
133 }
134
135 synchronized (ThreadLocalRandom.class) {
136 initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
137 if (initialSeedUniquifier != 0) {
138 return initialSeedUniquifier;
139 }
140
141
142 final long timeoutSeconds = 3;
143 final long deadLine = seedGeneratorStartTime + TimeUnit.SECONDS.toNanos(timeoutSeconds);
144 boolean interrupted = false;
145 for (;;) {
146 final long waitTime = deadLine - System.nanoTime();
147 try {
148 final Long seed;
149 if (waitTime <= 0) {
150 seed = seedQueue.poll();
151 } else {
152 seed = seedQueue.poll(waitTime, TimeUnit.NANOSECONDS);
153 }
154
155 if (seed != null) {
156 initialSeedUniquifier = seed;
157 break;
158 }
159 } catch (InterruptedException e) {
160 interrupted = true;
161 logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException.");
162 break;
163 }
164
165 if (waitTime <= 0) {
166 seedGeneratorThread.interrupt();
167 logger.warn(
168 "Failed to generate a seed from SecureRandom within {} seconds. " +
169 "Not enough entropy?", timeoutSeconds
170 );
171 break;
172 }
173 }
174
175
176 initialSeedUniquifier ^= 0x3255ecdc33bae119L;
177 initialSeedUniquifier ^= Long.reverse(System.nanoTime());
178
179 ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
180
181 if (interrupted) {
182
183 Thread.currentThread().interrupt();
184
185
186
187 seedGeneratorThread.interrupt();
188 }
189
190 if (seedGeneratorEndTime == 0) {
191 seedGeneratorEndTime = System.nanoTime();
192 }
193
194 return initialSeedUniquifier;
195 }
196 }
197
198 private static long newSeed() {
199 for (;;) {
200 final long current = seedUniquifier.get();
201 final long actualCurrent = current != 0? current : getInitialSeedUniquifier();
202
203
204 final long next = actualCurrent * 181783497276652981L;
205
206 if (seedUniquifier.compareAndSet(current, next)) {
207 if (current == 0 && logger.isDebugEnabled()) {
208 if (seedGeneratorEndTime != 0) {
209 logger.debug(String.format(
210 "-Dio.netty.initialSeedUniquifier: 0x%016x (took %d ms)",
211 actualCurrent,
212 TimeUnit.NANOSECONDS.toMillis(seedGeneratorEndTime - seedGeneratorStartTime)));
213 } else {
214 logger.debug(String.format("-Dio.netty.initialSeedUniquifier: 0x%016x", actualCurrent));
215 }
216 }
217 return next ^ System.nanoTime();
218 }
219 }
220 }
221
222
223
224 private static long mix64(long z) {
225 z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
226 z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
227 return z ^ (z >>> 33);
228 }
229
230
231 private static final long multiplier = 0x5DEECE66DL;
232 private static final long addend = 0xBL;
233 private static final long mask = (1L << 48) - 1;
234
235
236
237
238 private long rnd;
239
240
241
242
243
244
245
246 boolean initialized;
247
248
249
250
251 private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
252
253
254
255
256 ThreadLocalRandom() {
257 super(newSeed());
258 initialized = true;
259 }
260
261
262
263
264
265
266 public static ThreadLocalRandom current() {
267 return InternalThreadLocalMap.get().random();
268 }
269
270
271
272
273
274
275
276 @Override
277 public void setSeed(long seed) {
278 if (initialized) {
279 throw new UnsupportedOperationException();
280 }
281 rnd = (seed ^ multiplier) & mask;
282 }
283
284 @Override
285 protected int next(int bits) {
286 rnd = (rnd * multiplier + addend) & mask;
287 return (int) (rnd >>> (48 - bits));
288 }
289
290
291
292
293
294
295
296
297
298
299
300 public int nextInt(int least, int bound) {
301 if (least >= bound) {
302 throw new IllegalArgumentException();
303 }
304 return nextInt(bound - least) + least;
305 }
306
307
308
309
310
311
312
313
314
315
316 public long nextLong(long n) {
317 checkPositive(n, "n");
318
319
320
321
322
323
324 long offset = 0;
325 while (n >= Integer.MAX_VALUE) {
326 int bits = next(2);
327 long half = n >>> 1;
328 long nextn = ((bits & 2) == 0) ? half : n - half;
329 if ((bits & 1) == 0) {
330 offset += n - nextn;
331 }
332 n = nextn;
333 }
334 return offset + nextInt((int) n);
335 }
336
337
338
339
340
341
342
343
344
345
346
347 public long nextLong(long least, long bound) {
348 if (least >= bound) {
349 throw new IllegalArgumentException();
350 }
351 return nextLong(bound - least) + least;
352 }
353
354
355
356
357
358
359
360
361
362
363 public double nextDouble(double n) {
364 checkPositive(n, "n");
365 return nextDouble() * n;
366 }
367
368
369
370
371
372
373
374
375
376
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 }