jdk/test/java/util/concurrent/locks/Lock/TimedAcquireLeak.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7518 0282db800fe1
child 12049 de3e33eb5fa1
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7518
diff changeset
     2
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6460501 6236036 6500694 6490770
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Repeated failed timed waits shouldn't leak memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import static java.util.concurrent.TimeUnit.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class TimedAcquireLeak {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static String javahome() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        String jh = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        return (jh.endsWith("jre")) ? jh.substring(0, jh.length() - 4) : jh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final File bin = new File(javahome(), "bin");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static String javaProgramPath(String programName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        return new File(bin, programName).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final String java = javaProgramPath("java");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final String jmap = javaProgramPath("jmap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final String jps  = javaProgramPath("jps");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static String outputOf(Reader r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        final StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        final char[] buf = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        while ((n = r.read(buf)) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            sb.append(buf, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static String outputOf(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return outputOf(new InputStreamReader(is, "UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    67
    static final ExecutorService drainers = Executors.newFixedThreadPool(12);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static Future<String> futureOutputOf(final InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return drainers.submit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            new Callable<String>() { public String call() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    return outputOf(is); }});}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static String outputOf(final Process p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            Future<String> outputFuture = futureOutputOf(p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Future<String> errorFuture = futureOutputOf(p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            final String output = outputFuture.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            final String error = errorFuture.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            // Check for successful process completion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            equal(error, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            equal(p.waitFor(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            equal(p.exitValue(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (Throwable t) { unexpected(t); throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static String commandOutputOf(String... cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try { return outputOf(new ProcessBuilder(cmd).start()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        catch (Throwable t) { unexpected(t); throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // To be called exactly twice by the parent process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static <T> T rendezvousParent(Process p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                  Callable<T> callable) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        p.getInputStream().read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        T result = callable.call();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        OutputStream os = p.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        os.write((byte)'\n'); os.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // To be called exactly twice by the child process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static void rendezvousChild() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            for (int i = 0; i < 100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                System.gc(); System.runFinalization(); Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.out.write((byte)'\n'); System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            System.in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static String match(String s, String regex, int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        Matcher matcher = Pattern.compile(regex).matcher(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        matcher.find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return matcher.group(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static int objectsInUse(final Process child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            final String childPid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            final String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        final String regex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            "(?m)^ *[0-9]+: +([0-9]+) +[0-9]+ +\\Q"+className+"\\E$";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        final Callable<Integer> objectsInUse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            new Callable<Integer>() { public Integer call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                Integer i = Integer.parseInt(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    match(commandOutputOf(jmap, "-histo:live", childPid),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                          regex, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if (i > 100)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    System.out.print(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        commandOutputOf(jmap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                        "-dump:file=dump,format=b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                        childPid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        try { return rendezvousParent(child, objectsInUse); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        catch (Throwable t) { unexpected(t); return -1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // jmap doesn't work on Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (System.getProperty("os.name").startsWith("Windows"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        final String childClassName = Job.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        final String classToCheckForLeaks = Job.classToCheckForLeaks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        final String uniqueID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            String.valueOf(new Random().nextInt(Integer.MAX_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        final String[] jobCmd = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            java, "-Xmx8m",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            "-classpath", System.getProperty("test.classes", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            childClassName, uniqueID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        final Process p = new ProcessBuilder(jobCmd).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        final String childPid =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            match(commandOutputOf(jps, "-m"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                  "(?m)^ *([0-9]+) +\\Q"+childClassName+"\\E *"+uniqueID+"$", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        final int n0 = objectsInUse(p, childPid, classToCheckForLeaks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        final int n1 = objectsInUse(p, childPid, classToCheckForLeaks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        equal(p.waitFor(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        equal(p.exitValue(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        failed += p.exitValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // Check that no objects were leaked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        System.out.printf("%d -> %d%n", n0, n1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        check(Math.abs(n1 - n0) < 2); // Almost always n0 == n1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        check(n1 < 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        drainers.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // The main class of the child process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // Job's job is to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // - provide the name of a class to check for leaks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // - call rendezvousChild exactly twice, while quiescent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // - in between calls to rendezvousChild, run code that may leak.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public static class Job {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        static String classToCheckForLeaks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                "java.util.concurrent.locks.AbstractQueuedSynchronizer$Node";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            final ReentrantReadWriteLock rwlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                = new ReentrantReadWriteLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            final ReentrantReadWriteLock.ReadLock readLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                = rwlock.readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            final ReentrantReadWriteLock.WriteLock writeLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                = rwlock.writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            rwlock.writeLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            final BlockingQueue<Object> q = new LinkedBlockingQueue<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            final Semaphore fairSem = new Semaphore(0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            final Semaphore unfairSem = new Semaphore(0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            //final int threads =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            //rnd.nextInt(Runtime.getRuntime().availableProcessors() + 1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            final int threads = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // On Linux, this test runs very slowly for some reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // so use a smaller number of iterations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // Solaris can handle 1 << 18.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // On the other hand, jmap is much slower on Solaris...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            final int iterations = 1 << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            final CyclicBarrier cb = new CyclicBarrier(threads+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            for (int i = 0; i < threads; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                new Thread() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        final Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        for (int j = 0; j < iterations; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                            if (j == iterations/10 || j == iterations - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                cb.await(); // Quiesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                cb.await(); // Resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            //int t = rnd.nextInt(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            int t = rnd.nextInt(900);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            check(! lock.tryLock(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                            check(! readLock.tryLock(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            check(! writeLock.tryLock(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                            equal(null, q.poll(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            check(! fairSem.tryAcquire(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                            check(! unfairSem.tryAcquire(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                }}.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            cb.await();         // Quiesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            rendezvousChild();  // Measure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            cb.await();         // Resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            cb.await();         // Quiesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            rendezvousChild();  // Measure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            cb.await();         // Resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            System.exit(failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // If something goes wrong, we might never see it, since IO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // streams are connected to the parent.  So we need a special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // purpose print method to debug Jobs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        static void debugPrintf(String format, Object... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                new PrintStream(new FileOutputStream("/dev/tty"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    .printf(format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}