
package test.runtime;

import ovm.core.services.timer.TimerInterruptAction;
import ovm.core.services.memory.*;
/**
 * A test to see if POLLCHECK is doing anything.  Should fail if POLLCHECK
 * is not being executed or inserted.
 * <p>
 * Note that currently, only POLLCHECKs at back-branches are tested.  Should
 * really have a variant of this test that uses recursion.  That may be
 * difficult though...
 * @author Filip Pizlo
 */
public class TestPollCheck extends TestTimerBase {
    public TestPollCheck() {
        super("Poll Check");
    }
    
    public void run() {
        if (timerMan==null) {
            p(" SKIPPED: do not have a timer manager");
            return;
        }

        // do allocation in immortal in case this is a no-heap supporting
        // config and we're watching for heap accesses during event processing

        VM_Area old = MemoryManager.the().setCurrentArea(MemoryManager.the().getImmortalArea());
        try {
            final boolean[] cont=new boolean[1];
        
            TimerInterruptAction tia=new TimerInterruptAction(){
                    public void fire(int ticks) {
                        if (ticks<1) {
                            return;
                        }
                        cont[0]=false;
                    }
                    public String timerInterruptActionShortName() {
                        return "test";
                    }
                };
       
            timerMan.addTimerInterruptAction(tia);
        
            cont[0]=true;
            while (cont[0]) ; // this loop will be infinite if the timer never fires,
                              // or, more likely, if the POLLCHECK does not work.
        
            timerMan.removeTimerInterruptAction(tia);
        }
        finally {
            MemoryManager.the().setCurrentArea(old);
        }
    }
}

