Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / vm / bytecode / InvokeInstruction.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.vm.bytecode;
20
21 import gov.nasa.jpf.JPFException;
22 import gov.nasa.jpf.vm.ElementInfo;
23 import gov.nasa.jpf.vm.Instruction;
24 import gov.nasa.jpf.vm.MethodInfo;
25 import gov.nasa.jpf.vm.Scheduler;
26 import gov.nasa.jpf.vm.ThreadInfo;
27
28 /**
29  * abstract base for InvokeInstructions
30  */
31 public abstract class InvokeInstruction extends Instruction {
32
33   public abstract MethodInfo getInvokedMethod();
34   
35   public abstract String getInvokedMethodName();
36   public abstract String getInvokedMethodSignature();
37   public abstract String getInvokedMethodClassName();
38   
39   /**
40    * this does the lock registration/acquisition and respective transition break 
41    * return true if the caller has to re-execute
42    */
43   protected boolean reschedulesLockAcquisition (ThreadInfo ti, ElementInfo ei){
44     Scheduler scheduler = ti.getScheduler();
45     ei = ei.getModifiableInstance();
46     
47     if (!ti.isLockOwner(ei)){ // we only need to register, block and/or reschedule if this is not a recursive lock
48       if (ei.canLock(ti)) {
49         // record that this thread would lock the object upon next execution if we break the transition
50         // (note this doesn't re-add if already registered)
51         ei.registerLockContender(ti);
52         if (scheduler.setsLockAcquisitionCG(ti, ei)) { // optional scheduling point
53           return true;
54         }
55         
56       } else { // we need to block
57         ei.block(ti); // this means we only re-execute once we can acquire the lock
58         if (scheduler.setsBlockedThreadCG(ti, ei)){ // mandatory scheduling point
59           return true;
60         }
61         throw new JPFException("blocking synchronized call without transition break");            
62       }
63     }
64     
65     // locking will be done by ti.enter()
66     return false;
67   }
68
69 }