/*
 * Copyright (c) 2023, 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.
 */

/**
 * @test
 * @bug 8312174
 * @summary missing JVMTI events from vthreads parked during JVMTI attach
 * @requires vm.continuations
 * @requires vm.jvmti
 * @run main/othervm/native -agentlib:FieldWatchTest FieldWatchTest
 */

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.List;
import java.util.ArrayList;

class Counter {
    private int count = 0;

    Counter (int count) {
        this.count = count;
    }

    public synchronized void decr() {
        count--;
        notify();
    }

    public synchronized void await() {
        try {
            while (count > 0) {
                wait(1);
            }
        } catch (InterruptedException ex) {
            throw new RuntimeException("wait was interrupted: " + ex);
        }
    }
}

public class FieldWatchTest {
    static final int THREAD_CNT = 10;
    static final long TIMEOUT_BASE = 1_000_000L;

    private static void log(String msg) { System.out.println("main: " + msg); }

    private static native void enableEvents();
    private static native void disableEvents();
    private static native int fieldAccessCount();
    private static native int fieldModifyCount();

    private static boolean failed;
    private static volatile int dummy = 1000;
    private static List<Thread> threads = new ArrayList(THREAD_CNT);

    private static Counter ready1 = new Counter(THREAD_CNT);
    private static Counter ready2 = new Counter(THREAD_CNT);
    private static Counter mready = new Counter(1);

    private static int incrementDummy() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException ex) {
            // expected to be interrupted - ignore
        }
        dummy = dummy + 1;
        return dummy;
    }

    static final Runnable FOO = () -> {
        synchronized (threads) {
            threads.add(Thread.currentThread());
        }
        ready1.decr();
        mready.await();
        incrementDummy();
        ready2.decr();
    };

    static void startVThreads() throws Exception {
        log("\n# Java: Starting threads");
      //try (ExecutorService executorService = Executors.newWorkStealingPool(10)) { // platform threads
        try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()) { // virtual threads
            for (int i = 0; i < THREAD_CNT; i++) {
                executorService.execute(FOO);
            }
            ready1.await();
            mready.decr();
            Thread.sleep(1000);
            enableEvents();
            for (Thread t : threads) {
                t.interrupt();
            }
            ready2.await();
        }
    }

    public static void main(String[] args) throws Exception {
        startVThreads();

        // wait not more than 5 secs until all FieldAccess events are sent
        for (int sleepNo = 0; sleepNo < 10 && fieldAccessCount() < THREAD_CNT; sleepNo++) {
            log("wait iter: " + sleepNo);
            Thread.sleep(500);
        }
        disableEvents();

        int fieldAccessCnt = fieldAccessCount();
        int fieldModifyCnt = fieldModifyCount();

        log("dummy: " + dummy);
        log("FieldAccess cnt: "    + fieldAccessCnt    + " (expected: " + THREAD_CNT + ")");
        log("FieldModify cnt: "    + fieldModifyCnt    + " (expected: " + THREAD_CNT + ")");

        if (fieldAccessCnt != THREAD_CNT) {
            log("unexpected count of FieldAccess events");
            failed = true;
        }
        if (fieldModifyCnt != THREAD_CNT) {
            log("unexpected count of FieldModification events");
            failed = true;
        }
        if (failed) {
            throw new RuntimeException("FAILED: event count is wrong");
        }
    }

}
