/*
 * 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.lang.invoke.CallSite;
import java.lang.invoke.ConstantCallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.concurrent.atomic.AtomicLong;

import vm.mlvm.share.MlvmTest;
import vm.mlvm.share.Env;
import vm.mlvm.share.MultiThreadedTest;
import nsk.share.test.Stresser;
import nsk.share.Failure;

public class INDIFY_Test extends MultiThreadedTest {

#
# final int INDY_SITES_COUNT = 1000;
#
    private AtomicLong _counter = new AtomicLong();
    private AtomicLong _expectedTargetCalls = new AtomicLong();

    public INDIFY_Test() {
        super();
    }

    private static MethodType MT_bootstrap() {
        return MethodType.methodType(Object.class, Object.class, Object.class, Object.class);
    }

    private static MethodHandle MH_bootstrap() throws Exception {
        return MethodHandles.lookup().findStatic(
                INDIFY_Test.class,
                "bootstrap",
                MT_bootstrap());
    }

    public static Object bootstrap(Object c, Object name, Object mt) throws Throwable {
        Env.traceVerbose("bootstrap: Class " + c + "; method name = " + name + "; method type = " + mt);
        CallSite cs = new ConstantCallSite(
                MethodHandles.lookup().findVirtual(
                INDIFY_Test.class,
                "target",
                MethodType.methodType(Object.class, String.class, int.class)));
        return cs;
    }

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

#
# for ( int j = 0; j < INDY_SITES_COUNT; j++ ) {
#
    private static MethodHandle INDY_call@j;
    private static MethodHandle INDY_call@j () throws Throwable {
        if (INDY_call@j != null)
            return INDY_call@j;

        CallSite cs = (CallSite) MH_bootstrap().invokeWithArguments(
                MethodHandles.lookup(),
                "greet",
                MethodType.methodType(Object.class, INDIFY_Test.class, String.class, int.class));

        return cs.dynamicInvoker();
    }
#
# }
#

    public boolean runThread(int threadNum) throws Throwable {
        final INDIFY_Test x = this;
        final String s = "todo el mundo";
        final int i = 123;

        Stresser stresser = createStresser();

        stresser.start(1);
        while ( stresser.continueExecution() ) {
            stresser.iteration();

            long e;
            do {
                e = _expectedTargetCalls.get();
            } while ( ! _expectedTargetCalls.compareAndSet(e, e + @INDY_SITES_COUNT) );

#
# for ( int j = 0; j < INDY_SITES_COUNT; j++ ) {
#
            Object o@j = (Object) INDY_call@j ().invokeExact(x, s, i);
#
# }
#
        }

        return true;
    }

    protected void finalizeTest(boolean ok) throws Throwable {
        String msg = "Target was called " + _counter.get() + " times of " + _expectedTargetCalls.get();
        if ( ok && _counter.get() != _expectedTargetCalls.get() )
            throw new Failure(msg);
        else
            Env.display(msg);
    }

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