File: Basic.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (673 lines) | stat: -rw-r--r-- 27,604 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * This file is available under and governed by the GNU General Public
 * License version 2 only, as published by the Free Software Foundation.
 * However, the following notice accompanied the original version of this
 * file:
 *
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/*
 * @test
 * @bug 8005697
 * @summary Basic tests for StampedLock
 * @library /lib/testlibrary/
 * @author Chris Hegarty
 */

import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;

import java.util.Iterator;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.StampedLock;
import jdk.testlibrary.Utils;

public class Basic {
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);

    static void checkResult(Locker l, Class<? extends Throwable> c) {
        Throwable t = l.thrown();
        if (! ((t == null && c == null) || (c != null && c.isInstance(t)))) {
            fail("Mismatch in thread " +
                 l.getName() + ": " +
                 t + ", " +
                 (c == null ? "<null>" : c.getName()));
        }

        if (c == null)
            check(l.stamp() != 0L);  // must have acquired the lock
        else
            check(l.stamp() == 0L);  // must NOT have acquired the lock
    }

    //----------------------------------------------------------------
    // Mechanism to get all test threads into "running" mode.
    //----------------------------------------------------------------
    static void toTheStartingGate(Phaser gate) {
        try {
            gate.arriveAndAwaitAdvance();
        } catch (Throwable t) {
            unexpected(t);
        }
    }

    abstract static class Locker extends Thread {
        static AtomicInteger count = new AtomicInteger(1);
        private volatile Throwable thrown;
        private volatile long stamp;
        protected void thrown(Throwable thrown) { this.thrown = thrown; }
        public Throwable thrown() { return thrown; }
        protected void stamp(long stamp) { this.stamp = stamp; }
        public long stamp() { return stamp; }

        Locker() {
            this("Locker");
        }

        Locker(String name) {
            this.setName(name + ":" + count.getAndIncrement());
            this.setDaemon(true);
        }
    }

    abstract static class Reader extends Locker {
        Reader() { super("Reader"); }
        Reader(String name) { super(name); }
    }

    static Reader reader(final StampedLock sl, final Phaser gate) {
        return new Reader() { public void run() {
            if (gate != null ) toTheStartingGate(gate);
            stamp(sl.readLock());
            try {
                check(sl.validate(stamp()));
                check(sl.isReadLocked());
                check(!sl.isWriteLocked());
            } finally { sl.unlockRead(stamp()); } }};
    }

    static Reader readerView(final StampedLock sl, final Phaser gate) {
        return new Reader("ReaderView") { public void run() {
            if (gate != null ) toTheStartingGate(gate);
            final Lock rl = sl.asReadLock();
            rl.lock();
            try {
                stamp(1L);   // got the lock
                check(sl.isReadLocked());
                check(!sl.isWriteLocked());
            } finally { rl.unlock(); } }};
    }

    static Reader reader(StampedLock sl, Phaser gate, boolean view) {
        return view ? readerView(sl, gate) : reader(sl, gate);
    }

    static Reader interruptibleReader(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate) {
        return new Reader("InterruptibleReader") { public void run() {
            if (gate != null ) toTheStartingGate(gate);
            try {
                if (timeout < 0)
                    stamp(sl.readLockInterruptibly());
                else
                    stamp(sl.tryReadLock(timeout, unit));
                check(sl.validate(stamp()));
                check(sl.isReadLocked());
                check(!sl.isWriteLocked());
            } catch (Throwable x) { thrown(x);
            } finally { if (stamp() != 0L) sl.unlockRead(stamp()); } }};
    }

    static Reader interruptibleReaderView(final StampedLock sl,
                                          final long timeout,
                                          final TimeUnit unit,
                                          final Phaser gate) {
        return new Reader("InterruptibleReaderView") { public void run() {
            if (gate != null ) toTheStartingGate(gate);
            final Lock rl = sl.asReadLock();

            try {
                if (timeout < 0)
                    rl.lockInterruptibly();
                else
                    rl.tryLock(timeout, unit);
                stamp(1L);  // got the lock
                check(sl.isReadLocked());
                check(!sl.isWriteLocked());
            } catch (Throwable x) { thrown(x);
            } finally { if (stamp() != 0L) rl.unlock(); } }};
    }

    static Reader interruptibleReader(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate,
                                      final boolean view) {
        return view ? interruptibleReaderView(sl, timeout, unit, gate)
                    : interruptibleReader(sl, timeout, unit, gate);
    }

    abstract static class Writer extends Locker {
        Writer() { super("Writer"); }
        Writer(String name) { super(name); }
    }

    static Writer writer(final StampedLock sl, final Phaser gate) {
        return new Writer() { public void run() {
            if (gate != null ) toTheStartingGate(gate);
            try {
                stamp(sl.writeLock());
                check(sl.validate(stamp()));
                check(!sl.isReadLocked());
                check(sl.isWriteLocked());
            } finally { sl.unlockWrite(stamp()); } }};
    }

    static Writer writerView(final StampedLock sl, final Phaser gate) {
        return new Writer("WriterView") { public void run() {
            if (gate != null ) toTheStartingGate(gate);
            Lock wl = sl.asWriteLock();
            wl.lock();
            try {
                stamp(1L);  // got the lock
                check(!sl.isReadLocked());
                check(sl.isWriteLocked());
            } finally { wl.unlock(); } }};
    }

    static Writer writer(StampedLock sl, Phaser gate, boolean view) {
        return view ? writerView(sl, gate) : writer(sl, gate);
    }

    static Writer interruptibleWriter(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate) {
        return new Writer("InterruptibleWriter") { public void run() {
            if (gate != null ) toTheStartingGate(gate);
            try {
                if (timeout < 0)
                    stamp(sl.writeLockInterruptibly());
                else
                    stamp(sl.tryWriteLock(timeout, unit));
                check(sl.validate(stamp()));
                check(!sl.isReadLocked());
                check(sl.isWriteLocked());
            } catch (Throwable x) { thrown(x);
            } finally { if (stamp() != 0L) sl.unlockWrite(stamp()); } }};
    }

    static Writer interruptibleWriterView(final StampedLock sl,
                                          final long timeout,
                                          final TimeUnit unit,
                                          final Phaser gate) {
        return new Writer("InterruptibleWriterView") { public void run() {
            if (gate != null ) toTheStartingGate(gate);
            Lock wl = sl.asWriteLock();
            try {
                if (timeout < 0)
                    wl.lockInterruptibly();
                else
                    wl.tryLock(timeout, unit);
                stamp(1L);  // got the lock
                check(!sl.isReadLocked());
                check(sl.isWriteLocked());
            } catch (Throwable x) { thrown(x);
            } finally { if (stamp() != 0L) wl.unlock(); } }};
    }

    static Writer interruptibleWriter(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate,
                                      final boolean view) {
        return view ? interruptibleWriterView(sl, timeout, unit, gate)
                    : interruptibleWriter(sl, timeout, unit, gate);
    }

    // Returns an infinite lazy list of all possible reader combinations.
    static Iterator<Reader> readerIterator(final StampedLock sl,
                                           final Phaser gate) {
        return new Iterator<Reader>() {
            int i = 0;
            boolean view = false;
            public boolean hasNext() { return true; }
            public Reader next() {
                switch ((i++)&7) {
                    case 1: case 4: case 7:
                        return reader(sl, gate, view ^= true);
                    case 2: case 5:
                        return interruptibleReader(sl, -1, SECONDS, gate, view ^= true);
                    default:
                        return interruptibleReader(sl, LONG_DELAY_MS, MILLISECONDS, gate, view ^= true); }}
            public void remove() {throw new UnsupportedOperationException();}};
    }

    // Returns an infinite lazy list of all possible writer combinations.
    static Iterator<Writer> writerIterator(final StampedLock sl,
                                           final Phaser gate) {
        return new Iterator<Writer>() {
            int i = 0;
            boolean view = false;
            public boolean hasNext() { return true; }
            public Writer next() {
                switch ((i++)&7) {
                    case 1: case 4: case 7:
                        return writer(sl, gate, view ^= true);
                    case 2: case 5:
                        return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
                    default:
                        return interruptibleWriter(sl, LONG_DELAY_MS, MILLISECONDS, gate, view ^= true); }}
            public void remove() {throw new UnsupportedOperationException();}};
    }

    static class SimpleTimer {
        long startTime = System.nanoTime();
        long elapsedMillis() {
            long now = System.nanoTime();
            long elapsed = NANOSECONDS.toMillis(now - startTime);
            startTime = now;
            return elapsed;
        }
        void printElapsed() { System.out.println(elapsedMillis() + " ms"); }
    }

    static void waitForThreadToBlock(Thread thread) {
        for (long startTime = 0;;) {
            Thread.State state = thread.getState();
            if (state == Thread.State.WAITING ||
                state == Thread.State.TIMED_WAITING)
                break;
            if (startTime == 0) startTime = System.nanoTime();
            else if (System.nanoTime() - startTime > 10L * 1000L * 1000L * 1000L)
                throw new AssertionError("timed out waiting for thread to block");
        }
    }

    private static void realMain(String[] args) throws Throwable {
        SimpleTimer timer = new SimpleTimer();

        //----------------------------------------------------------------
        System.out.print("Some basic sanity: ");
        //----------------------------------------------------------------
        try {
            final StampedLock sl = new StampedLock();
            check(!sl.isReadLocked());
            check(!sl.isWriteLocked());
            long stamp = sl.tryOptimisticRead();
            check(stamp != 0L);
            check(sl.validate(stamp));
            check(!sl.validate(0));

            stamp = sl.writeLock();
            try {
                check(sl.validate(stamp));
                check(!sl.isReadLocked());
                check(sl.isWriteLocked());
                check(sl.tryReadLock() == 0L);
                check(sl.tryReadLock(1, MILLISECONDS) == 0L);
                check(sl.tryOptimisticRead() == 0L);
                check(sl.tryWriteLock() == 0L);
                check(sl.tryWriteLock(1, MILLISECONDS) == 0L);
                check(!sl.tryUnlockRead());
                check(sl.tryConvertToWriteLock(stamp) == stamp);
                try {
                    sl.unlockRead(stamp);
                    fail("Expected unlockRead to throw when not holding read lock");
                } catch (IllegalMonitorStateException x) {
                    pass();
                }
                check(sl.validate(stamp));
            } finally {
                sl.unlockWrite(stamp);
            }
            check(!sl.isWriteLocked());

            stamp = sl.readLock();
            try {
                check(sl.validate(stamp));
                check(sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(sl.tryOptimisticRead() != 0L);
                check(sl.tryWriteLock() == 0L);
                check(sl.tryWriteLock(1, MILLISECONDS) == 0L);
                check(!sl.tryUnlockWrite());
                check(sl.tryConvertToReadLock(stamp) == stamp);
                try {
                    sl.unlockWrite(stamp);
                    fail("Expected unlockWrite to throw when not holding read lock");
                } catch (IllegalMonitorStateException x) {
                    pass();
                }
                check(sl.validate(stamp));
            } finally {
                sl.unlockRead(stamp);
            }
            check(!sl.isReadLocked());

            stamp = sl.tryReadLock(1, MILLISECONDS);
            try {
                check(stamp != 0L);
            } finally {
                sl.unlockRead(stamp);
            }
        } catch (Throwable t) { unexpected(t); }
        timer.printElapsed();

        //----------------------------------------------------------------
        System.out.print("Multiple writers single reader: ");
        //----------------------------------------------------------------
        try {
            StampedLock sl = new StampedLock();
            int nThreads = 10;
            Phaser gate = new Phaser(nThreads + 2);
            Iterator<Writer> writers = writerIterator(sl, gate);
            Iterator<Reader> readers = readerIterator(sl, gate);
            for (int i = 0; i < 2; i++) {
                check(!sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(!sl.tryUnlockRead());
                check(!sl.tryUnlockWrite());
                check(sl.tryOptimisticRead() != 0L);
                Locker[] wThreads = new Locker[nThreads];
                for (int j=0; j<nThreads; j++)
                    wThreads[j] = writers.next();
                for (int j=0; j<nThreads; j++)
                    wThreads[j].start();
                Reader reader = readers.next(); reader.start();
                toTheStartingGate(gate);
                reader.join();
                for (int j=0; j<nThreads; j++)
                    wThreads[j].join();
                for (int j=0; j<nThreads; j++)
                    checkResult(wThreads[j], null);
                checkResult(reader, null);
            }
        } catch (Throwable t) { unexpected(t); }
        timer.printElapsed();

        //----------------------------------------------------------------
        System.out.print("Multiple readers single writer: ");
        //----------------------------------------------------------------
        try {
            StampedLock sl = new StampedLock();
            int nThreads = 10;
            Phaser gate = new Phaser(nThreads + 2);
            Iterator<Writer> writers = writerIterator(sl, gate);
            Iterator<Reader> readers = readerIterator(sl, gate);
            for (int i = 0; i < 2; i++) {
                check(!sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(!sl.tryUnlockRead());
                check(!sl.tryUnlockWrite());
                check(sl.tryOptimisticRead() != 0L);
                Locker[] rThreads = new Locker[nThreads];
                for (int j=0; j<nThreads; j++)
                    rThreads[j] = readers.next();
                for (int j=0; j<nThreads; j++)
                    rThreads[j].start();
                Writer writer = writers.next(); writer.start();
                toTheStartingGate(gate);
                writer.join();
                for (int j=0; j<nThreads; j++)
                    rThreads[j].join();
                for (int j=0; j<nThreads; j++)
                    checkResult(rThreads[j], null);
                checkResult(writer, null);
            }
        } catch (Throwable t) { unexpected(t); }
        timer.printElapsed();

        //----------------------------------------------------------------
        System.out.print("thread interrupted: ");
        //----------------------------------------------------------------
        try {
            // We test interrupting both before and after trying to acquire
            boolean view = false;
            StampedLock sl = new StampedLock();
            for (long timeout : new long[] { -1L, LONG_DELAY_MS, -1L, LONG_DELAY_MS }) {
                long stamp;
                Thread.State state;

                stamp = sl.writeLock();
                try {
                    Reader r = interruptibleReader(sl, timeout, MILLISECONDS, null, view);
                    r.start();
                    r.interrupt();
                    r.join();
                    checkResult(r, InterruptedException.class);
                } finally {
                    sl.unlockWrite(stamp);
                }

                stamp = sl.writeLock();
                try {
                    Reader r = interruptibleReader(sl, timeout, MILLISECONDS, null, view);
                    r.start();
                    waitForThreadToBlock(r);
                    r.interrupt();
                    r.join();
                    checkResult(r, InterruptedException.class);
                } finally {
                    sl.unlockWrite(stamp);
                }

                stamp = sl.readLock();
                try {
                    Writer w = interruptibleWriter(sl, timeout, MILLISECONDS, null, view);
                    w.start();
                    w.interrupt();
                    w.join();
                    checkResult(w, InterruptedException.class);
                } finally {
                    sl.unlockRead(stamp);
                }

                stamp = sl.readLock();
                try {
                    Writer w = interruptibleWriter(sl, timeout, MILLISECONDS, null, view);
                    w.start();
                    waitForThreadToBlock(w);
                    w.interrupt();
                    w.join();
                    checkResult(w, InterruptedException.class);
                } finally {
                    sl.unlockRead(stamp);
                }

                check(!sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(!sl.tryUnlockRead());
                check(!sl.tryUnlockWrite());
                check(sl.tryOptimisticRead() != 0L);
                if (timeout == LONG_DELAY_MS)
                    view = true;
            }
        } catch (Throwable t) { unexpected(t); }
        timer.printElapsed();

        //----------------------------------------------------------------
        System.out.print("timeout: ");
        //----------------------------------------------------------------
        try {
            StampedLock sl = new StampedLock();
            for (long timeout : new long[] { 0L, 5L }) {
                long stamp = sl.writeLock();
                try {
                    check(sl.tryReadLock(timeout, MILLISECONDS) == 0L);
                } finally {
                    sl.unlockWrite(stamp);
                }
                stamp = sl.readLock();
                try {
                    check(sl.tryWriteLock(timeout, MILLISECONDS) == 0L);
                } finally {
                    sl.unlockRead(stamp);
                }
                check(!sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(!sl.tryUnlockRead());
                check(!sl.tryUnlockWrite());
                check(sl.tryOptimisticRead() != 0L);
            }
        } catch (Throwable t) { unexpected(t); }
        timer.printElapsed();

        //----------------------------------------------------------------
        System.out.print("optimistic read: ");
        //----------------------------------------------------------------
        try {
            StampedLock sl = new StampedLock();
            Iterator<Writer> writers = writerIterator(sl, null);
            Iterator<Reader> readers = readerIterator(sl, null);
            for (int i = 0; i < 10; i++) {
                check(!sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(!sl.tryUnlockRead());
                check(!sl.tryUnlockWrite());
                long stamp = sl.tryOptimisticRead();
                check(stamp != 0L);
                check(sl.tryConvertToOptimisticRead(stamp) == stamp);
                Reader r = readers.next(); r.start();
                r.join();
                checkResult(r, null);
                check(sl.validate(stamp));
                check(sl.tryConvertToOptimisticRead(stamp) == stamp);
                Writer w = writers.next(); w.start();
                w.join();
                checkResult(w, null);
                check(sl.validate(stamp) == false);
            }
        } catch (Throwable t) { unexpected(t); }
        timer.printElapsed();

        //----------------------------------------------------------------
        System.out.print("convert: ");
        //----------------------------------------------------------------
        try {
            StampedLock sl = new StampedLock();
            for (int i = 0; i < 2; i++) {
                check(!sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(!sl.tryUnlockRead());
                check(!sl.tryUnlockWrite());
                long stamp = sl.tryOptimisticRead();
                check(stamp != 0L);
                check((stamp = sl.tryConvertToReadLock(stamp)) != 0L);
                check(sl.validate(stamp));
                check(sl.isReadLocked());
                check(sl.tryWriteLock() == 0L);
                check(sl.tryWriteLock(1L, MILLISECONDS) == 0L);
                check((stamp = sl.tryConvertToWriteLock(stamp)) != 0L);
                check(sl.validate(stamp));
                check(!sl.isReadLocked());
                check(sl.isWriteLocked());
                check(sl.tryReadLock(1L, MILLISECONDS) == 0L);
                if (i != 0) {
                    sl.unlockWrite(stamp);
                    continue;
                }
                // convert down
                check((stamp = sl.tryConvertToReadLock(stamp)) != 0L);
                check(sl.validate(stamp));
                check(sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(sl.tryWriteLock() == 0L);
                check(sl.tryWriteLock(1L, MILLISECONDS) == 0L);
                check((stamp = sl.tryConvertToOptimisticRead(stamp)) != 0L);
                check(sl.validate(stamp));
                check(!sl.isReadLocked());
                check(!sl.isWriteLocked());
                check(sl.validate(stamp));
            }
        } catch (Throwable t) { unexpected(t); }
        timer.printElapsed();

        //----------------------------------------------------------------
        System.out.print("views: ");
        //----------------------------------------------------------------
        try {
            StampedLock sl = new StampedLock();

            Lock rl = sl.asReadLock();
            Lock wl = sl.asWriteLock();
            for (int i = 0; i < 2; i++) {
                rl.lock();
                try {
                    check(sl.isReadLocked());
                    check(!sl.isWriteLocked());
                    check(sl.tryWriteLock() == 0L);
                    check(sl.tryWriteLock(1L, MILLISECONDS) == 0L);
                } finally {
                    rl.unlock();
                }
                check(!sl.isReadLocked());
                check(!sl.isWriteLocked());

                wl.lock();
                try {
                    check(!sl.isReadLocked());
                    check(sl.isWriteLocked());
                    check(sl.tryWriteLock() == 0L);
                    check(sl.tryWriteLock(1L, MILLISECONDS) == 0L);
                } finally {
                    wl.unlock();
                }
                check(!sl.isReadLocked());
                check(!sl.isWriteLocked());

                ReadWriteLock rwl = sl.asReadWriteLock();
                rl = rwl.readLock();
                wl = rwl.writeLock();
            }
        } catch (Throwable t) { unexpected(t); }
        timer.printElapsed();
    }

    //--------------------- Infrastructure ---------------------------
    static volatile int passed = 0, failed = 0;
    static void pass() {passed++;}
    static void fail() {failed++; Thread.dumpStack();}
    static void fail(String msg) {System.out.println(msg); fail();}
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
    static void check(boolean cond) {if (cond) pass(); else fail();}
    static void equal(Object x, Object y) {
        if (x == null ? y == null : x.equals(y)) pass();
        else fail(x + " not equal to " + y);}
    public static void main(String[] args) throws Throwable {
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
        if (failed > 0) throw new AssertionError("Some tests failed");}
}