nashorn/test/script/basic/JDK-8026367.js
author hannesw
Wed, 25 Jun 2014 14:36:24 +0200
changeset 25243 7a1edca6ce94
parent 24778 2ff5d7041566
child 36141 696d662bcdb7
permissions -rw-r--r--
8048079: Persistent code store is broken after optimistic types merge Reviewed-by: sundar, jlaskey, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20946
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
     1
/*
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 20946
diff changeset
     4
 *
20946
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 20946
diff changeset
     8
 *
20946
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    13
 * accompanied this code).
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 20946
diff changeset
    14
 *
20946
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 20946
diff changeset
    18
 *
20946
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    21
 * questions.
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    22
 */
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    23
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    24
/**
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    25
 * JDK-8026367: Add a sync keyword to mozilla_compat
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    26
 *
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    27
 * @test
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    28
 * @run
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    29
 */
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    30
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    31
if (typeof sync === "undefined") {
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    32
    load("nashorn:mozilla_compat.js");
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    33
}
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    34
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    35
var obj = {
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    36
    count: 0,
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    37
    // Sync called with one argument will synchronize on this-object of invocation
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    38
    inc: sync(function(d) {
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    39
        this.count += d;
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    40
    }),
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    41
    // Pass explicit object to synchronize on as second argument
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    42
    dec: sync(function(d) {
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    43
        this.count -= d;
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    44
    }, obj)
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    45
};
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    46
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    47
var t1 = new java.lang.Thread(function() {
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    48
    for (var i = 0; i < 100000; i++) obj.inc(1);
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    49
});
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    50
var t2 = new java.lang.Thread(function() {
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    51
    for (var i = 0; i < 100000; i++) obj.dec(1);
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    52
});
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    53
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    54
t1.start();
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    55
t2.start();
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    56
t1.join();
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    57
t2.join();
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    58
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    59
if (obj.count !== 0) {
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    60
    throw new Error("Expected count == 0, got " + obj.count);
bd3d6e87ec6b 8026367: Add a sync keyword to mozilla_compat
hannesw
parents:
diff changeset
    61
}