1/*
2 * Copyright 2009 D.E. Shaw.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25/**
26 * @test
27 * @bug 6863420
28 * @summary os::javaTimeNanos() go backward on Solaris x86
29 *
30 * @run main/othervm/timeout=300 compiler.runtime.Test6863420
31 */
32/*
33 * Notice the internal timeout in timeout thread Test6863420.TOT.
34 */
35
36package compiler.runtime;
37
38public class Test6863420 {
39
40    static final int INTERNAL_TIMEOUT=240;
41    static class TOT extends Thread {
42       public void run() {
43           try {
44               Thread.sleep(INTERNAL_TIMEOUT*1000);
45           } catch (InterruptedException ex) {
46           }
47           done = true;
48       }
49    }
50
51    static long value = 0;
52    static boolean got_backward_time = false;
53    static volatile boolean done = false;
54
55    public static void main(String args[]) {
56        final int count = 100000;
57
58        TOT tot = new TOT();
59        tot.setDaemon(true);
60        tot.start();
61
62        for (int numThreads = 1; !done && numThreads <= 32; numThreads++) {
63            final int numRuns = 1;
64            for (int t=1; t <= numRuns; t++) {
65                final int curRun = t;
66
67                System.out.println("Spawning " + numThreads + " threads");
68                final Thread threads[] = new Thread[numThreads];
69                for (int i = 0; i < threads.length; i++) {
70                    Runnable thread =
71                        new Runnable() {
72                            public void run() {
73                                for (long l = 0; !done && l < 100000; l++) {
74                                    final long start = System.nanoTime();
75                                    if (value == 12345678) {
76                                        System.out.println("Wow!");
77                                    }
78                                    final long end = System.nanoTime();
79                                    final long time = end - start;
80                                    value += time;
81                                    if (time < 0) {
82                                        System.out.println(
83                                            "Backwards: " +
84                                            "start=" + start + " " +
85                                            "end=" + end + " " +
86                                            "time= " + time
87                                        );
88                                        got_backward_time = true;
89                                    }
90                                }
91                            }
92                        };
93                    threads[i] = new Thread(thread, "Thread" + i);
94                }
95                for (int i = 0; i < threads.length; i++) {
96                    threads[i].start();
97                }
98                for (int i = 0; i < threads.length; i++) {
99                    try {
100                        threads[i].join();
101                    }
102                    catch (InterruptedException e) {
103                        continue;
104                    }
105                }
106            }
107        }
108
109        if (got_backward_time) {
110            System.exit(97);
111        }
112    }
113}
114