jdk/test/java/util/concurrent/locks/ReentrantLock/LoopHelpers.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7518 0282db800fe1
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Misc utilities in JSR166 performance tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.atomic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class LoopHelpers {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // Some mindless computation to do between synchronizations...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * generates 32 bit pseudo-random numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Adapted from http://www.snippets.org
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static int compute1(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int lo = 16807 * (x & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        int hi = 16807 * (x >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        lo += (hi & 0x7FFF) << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if ((lo & 0x80000000) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            lo &= 0x7fffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            ++lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        lo += hi >>> 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (lo == 0 || (lo & 0x80000000) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            lo &= 0x7fffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            ++lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *  Computes a linear congruential random number a random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *  of times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static int compute2(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        int loops = (x >>> 4) & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        while (loops-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            x = (x * 2147483647) % 16807;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * An actually useful random number generator, but unsynchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Basically same as java.util.Random.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static class SimpleRandom {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        private final static long multiplier = 0x5DEECE66DL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        private final static long addend = 0xBL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        private final static long mask = (1L << 48) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        static final AtomicLong seq = new AtomicLong(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        private long seed = System.nanoTime() + seq.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public void setSeed(long s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            seed = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        public int next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            long nextseed = (seed * multiplier + addend) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            seed = nextseed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static class BarrierTimer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public volatile long startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        public volatile long endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            long t = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (startTime == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                startTime = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                endTime = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            startTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            endTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return endTime - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static String rightJustify(long n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // There's probably a better way to do this...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String field = "         ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String num = Long.toString(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (num.length() >= field.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        StringBuffer b = new StringBuffer(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        b.replace(b.length()-num.length(), b.length(), num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return b.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}