Fixing a few bugs in the statistics printout.
[jpf-core.git] / src / main / gov / nasa / jpf / vm / ThreadData.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 package gov.nasa.jpf.vm;
19
20 import gov.nasa.jpf.util.HashData;
21
22
23 /**
24  * this is the mutable Thread data we have to keep track of for storing/restoring states
25  */
26 public class ThreadData {
27   /**
28    * Current state of the thread.
29    */
30   ThreadInfo.State state;
31
32   /** the scheduler priority of this thread */
33   int priority;
34
35   /**
36    * the name of this thread
37    * (only temporarily unset, between NEW and INVOKESPECIAL)
38    */
39   String name = "?";
40
41   /** is this a daemon thread */
42   boolean isDaemon;
43
44   /**
45    * The lock counter when the object got into a wait. This value
46    * is used to restore the object lock count once this thread
47    * gets notified
48    */
49   int lockCount;
50
51   /**
52    * The suspend count of the thread. See ThreadInfo.suspend() for a discussion
53    * of how faithful this is (it is an over approximation)
54    */
55   int suspendCount;
56
57
58   @Override
59   public ThreadData clone () {
60     ThreadData t = new ThreadData();
61
62     t.state = state;
63     t.lockCount = lockCount;
64     t.suspendCount = suspendCount;
65
66     t.priority = priority;
67     t.name = name;
68     t.isDaemon = isDaemon;
69
70     return t;
71   }
72
73   @Override
74   public boolean equals (Object o) {
75     if ((o == null) || !(o instanceof ThreadData)) {
76       return false;
77     }
78
79     ThreadData t = (ThreadData) o;
80
81     return ((state == t.state) && 
82             (priority == t.priority) &&
83             (isDaemon == t.isDaemon) && 
84             (lockCount == t.lockCount) &&
85             (suspendCount == t.suspendCount) && 
86             (name.equals(t.name)));
87   }
88
89   public void hash (HashData hd) {
90     hd.add(state);
91     hd.add(lockCount);
92     hd.add(suspendCount);
93     hd.add(priority);
94     hd.add(isDaemon);
95     hd.add(name);
96   }
97
98   @Override
99   public int hashCode () {
100     HashData hd = new HashData();
101
102     hash(hd);
103
104     return hd.getValue();
105   }
106
107   @Override
108   public String toString () {
109     return ("ThreadData{" + getFieldValues() + '}');
110   }
111
112   public String getFieldValues () {
113     StringBuilder sb = new StringBuilder("name:");
114
115     sb.append(name);
116     sb.append(",status:");
117     sb.append(state.name());
118     sb.append(",priority:");
119     sb.append(priority);
120     sb.append(",isDaemon:");
121     sb.append(isDaemon);
122     sb.append(",lockCount:");
123     sb.append(lockCount);
124     sb.append(",suspendCount:");
125     sb.append(suspendCount);
126
127     return sb.toString();
128   }
129
130   public ThreadInfo.State getState() { return state; }
131 }