/*
 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
 * 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.
 */

package vm.mlvm.indy.stress.java.loopsAndThreads;

import java.dyn.CallSite;
import java.dyn.InvokeDynamic;
import java.dyn.Linkage;
import java.dyn.MethodHandles;
import java.dyn.MethodType;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import vm.mlvm.share.MlvmTest;
import vm.mlvm.share.MultiThreadedTest;

public class Test extends MultiThreadedTest {

    private static final int THREAD_COUNT = 100;
    private static final int CYCLES_COUNT = 10000; // Provoke compiler
    private static final long EXPECTED_TARGET_CALLS = 100L * THREAD_COUNT * CYCLES_COUNT;

    private AtomicLong _counter = new AtomicLong();

    static {
        Linkage.registerBootstrapMethod(Test.class, "bootstrap");
    }

    public Test() {
        super(THREAD_COUNT);
    }

    public static CallSite bootstrap(Class<?> c, String name, MethodType mt) {
        getLog().trace(0, "Class " + c + "; method name = " + name + "; method type = " + mt);
        CallSite cs = new CallSite(c, name, mt);
        cs.setTarget(MethodHandles.publicLookup().findVirtual(
                Test.class, "target", MethodType.methodType(Object.class, String.class, int.class)));
        return cs;
    }

    public Object target(String s, int i) {
        _counter.incrementAndGet();
        return null;
    }

    @Override
    public boolean runThread() throws Throwable {
        final Test x = this;
        for ( int i = 0; i < CYCLES_COUNT; i++ ) {
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);

            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);

            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);

            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);

            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);

            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);

            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);

            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);

            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);

            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "world", 123);
            InvokeDynamic.greet(x, "mundus", 456);
            InvokeDynamic.greet(x, "kosmos", 789);
            InvokeDynamic.greet(x, "mundus", 123);
        }

        return true;
    }

    protected boolean tearDown() throws Throwable {
        getLog().display("Target was called " + _counter + " times of " + EXPECTED_TARGET_CALLS);
        return _counter.get() == EXPECTED_TARGET_CALLS;
    }

    public static void main(String[] args) { MlvmTest.launch(args); }
}
