import jdk.test.lib.Asserts;

import jdk.internal.value.ValueClass;
import jdk.internal.vm.annotation.LooselyConsistentValue;

public class TestInterpreter {
    static final MyValue[] array1 = (MyValue[])ValueClass.newNullRestrictedAtomicArray(MyValue.class, 1, MyValue.DEFAULT);

    static class Runner extends Thread {
        TestInterpreter test;

        public Runner(TestInterpreter test) {
            this.test = test;
        }

        public void run() {
            for (int i = 0; i < 1_000_000; ++i) {
                array1[0] = array1[0].incrementAndCheck();
                array1[0] = array1[0].incrementAndCheck();

            }
        }
    }

    public static void main(String[] args) throws Exception {
        TestInterpreter test = new TestInterpreter();
        Thread runner = null;
        for (int i = 0; i < 20; ++i) {
            runner = new Runner(test);
            runner.start();
        }
        runner.join();
    }
}

@LooselyConsistentValue
value class MyValue {
    short x;
    short y;

    static final MyValue DEFAULT = new MyValue((short)0, (short)0);

    MyValue(short x, short y) {
        this.x = x;
        this.y = y;
    }

    MyValue incrementAndCheck() {
        Asserts.assertEQ(x, y, "Inconsistent field values");
        return new MyValue((short)(x + 1), (short)(y + 1));
    }
}
