001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.adaptive;
014
015import org.jikesrvm.adaptive.controller.Controller;
016import org.jikesrvm.classloader.RVMMethod;
017import org.jikesrvm.compilers.common.CompiledMethod;
018import org.jikesrvm.compilers.common.CompiledMethods;
019import org.jikesrvm.scheduler.RVMThread;
020import org.vmmagic.pragma.NoInline;
021import org.vmmagic.pragma.Unpreemptible;
022import org.vmmagic.unboxed.Offset;
023
024/**
025 * Trigger an OSR from a running thread.
026 */
027public class OnStackReplacementTrigger {
028
029  @NoInline
030  @Unpreemptible
031  public static void trigger(int ypTakenInCMID, Offset tsFromFPoff, Offset ypTakenFPoff, int whereFrom) {
032
033    RVMThread thread = RVMThread.getCurrentThread();
034    CompiledMethod ypTakenInCM = CompiledMethods.getCompiledMethod(ypTakenInCMID);
035    RVMMethod ypTakenInMethod = ypTakenInCM.getMethod();
036    boolean isInBootImage = ypTakenInMethod.getDeclaringClass().isInBootImage();
037
038    if (isInBootImage) return;
039    OnStackReplacementEvent event = (OnStackReplacementEvent) thread.onStackReplacementEvent;
040    event.suspendedThread = thread;
041    event.whereFrom = whereFrom;
042    event.CMID = ypTakenInCMID;
043    event.tsFromFPoff = tsFromFPoff;
044    event.ypTakenFPoff = ypTakenFPoff;
045
046    thread.monitor().lockNoHandshake();
047    thread.requesting_osr = true;
048    thread.monitor().unlock();
049
050    Controller.osrOrganizer.activate();
051    // PNT: Assumes that OSR doesn't need access to our context regs
052    thread.monitor().lockNoHandshake();
053    while (!thread.osr_done) {
054      thread.monitor().waitWithHandshake();
055    }
056    thread.osr_done = false;
057    thread.monitor().unlock();
058  }
059}