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