Fixes null captured parameters
[jpf-core.git] / src / main / gov / nasa / jpf / vm / TidSet.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;
20
21 import gov.nasa.jpf.util.UnsortedArrayIntSet;
22
23 /**
24  * set that stores threads via (search global) thread ids. Used to detect shared objects/classes,
25  * created by configured SharedObjectPolicy factory
26  * 
27  * Note - this class modified contents of instances, i.e. it does destructive updates
28  * and hence has state carry-over between paths
29  */
30 public class TidSet extends UnsortedArrayIntSet implements ThreadInfoSet, Memento<ThreadInfoSet> {
31   
32   protected VM vm;
33   
34   public TidSet (ThreadInfo ti){
35     vm = ti.getVM();
36     
37     add( ti.getId());
38   }  
39   
40   //--- set update
41   
42   @Override
43   public ThreadInfoSet add (ThreadInfo ti) {
44     add( ti.getId());
45     return this;
46   }
47   
48   @Override
49   public ThreadInfoSet remove (ThreadInfo ti) {
50     remove( ti.getId());
51     return this;
52   }
53   
54   
55   //--- set query
56   
57   @Override
58   public boolean contains (ThreadInfo ti) {
59     return contains( ti.getId());
60   }
61   
62   @Override
63   public boolean isShared (ThreadInfo ti, ElementInfo ei){
64     return hasMultipleLiveThreads();
65   }
66   
67   @Override
68   public boolean hasMultipleLiveThreads(){
69     if (size == 0){
70       return false;
71       
72     } else {
73       boolean alreadyHadOne = false;
74       ThreadList tl = vm.getThreadList();
75       
76       for (int i=0; i<size; i++){
77         ThreadInfo ti = tl.getThreadInfoForId(elements[i]);
78         if (ti != null && !ti.isTerminated()){
79           if (alreadyHadOne){
80             return true;
81           }
82           alreadyHadOne = true;
83         }
84       }
85       
86       return false;
87     }
88   }
89
90   @Override
91   public boolean hasMultipleRunnableThreads(){
92     if (size == 0){
93       return false;
94       
95     } else {
96       boolean alreadyHadOne = false;
97       ThreadList tl = vm.getThreadList();
98       
99       for (int i=0; i<size; i++){
100         ThreadInfo ti = tl.getThreadInfoForId(elements[i]);
101         if (ti != null && ti.isRunnable()){
102           if (alreadyHadOne){
103             return true;
104           }
105           alreadyHadOne = true;
106         }
107       }
108       
109       return false;
110     }
111   }
112   
113   @Override
114   public String toString() {
115     StringBuilder sb = new StringBuilder();
116     sb.append(getClass().getName());
117     sb.append('{');
118     for (int i = 0; i<size; i++) {
119       if (i>0) {
120         sb.append(',');
121       }
122       sb.append(elements[i]);
123     }
124     sb.append('}');
125     
126     return sb.toString();
127   }
128
129   
130   //--- state management (TidSet instance per default are their own mementos)
131   
132   @Override
133   public Memento<ThreadInfoSet> getMemento(){
134     return this;
135   }
136
137   @Override
138   public ThreadInfoSet restore(ThreadInfoSet inSitu) {
139     return this;
140   }
141 }