KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DEOS > PriorityListOfThreads


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package DEOS;
20
21 import gov.nasa.jpf.jvm.Verify;
22
23
24 /**
25  * DOCUMENT ME!
26  */

27 class PriorityListOfThreads {
28   static final int numberOfThreadPriorities = 4; // from spin code
29
int itsHighestPriorityMember;
30   threadList[] itsList;
31
32   public PriorityListOfThreads () {
33     //System.out.println("PriorityListOfThreads Constructor");
34
itsHighestPriorityMember = 0;
35     itsList = new threadList[numberOfThreadPriorities];
36
37     for (int i = 0; i < numberOfThreadPriorities; i++) {
38       itsList[i] = new threadList();
39     }
40   }
41
42   public boolean isEmpty () {
43     return highestPriorityMember().isEmpty();
44   }
45
46   public void addAtBeginning (threadListNode theNode) {
47     //Verify.beginAtomic();
48
//System.out.println("PriorityListOfThreads.addAtBeginning");
49
int threadPriority = theNode.parent().currentPriority();
50     itsList[threadPriority].addAtBeginning(theNode);
51
52     if (itsHighestPriorityMember < threadPriority) {
53       itsHighestPriorityMember = threadPriority;
54     }
55
56     //Verify.endAtomic();
57
}
58
59   public void addAtEnd (threadListNode theNode) {
60     //Verify.beginAtomic();
61
int threadPriority = theNode.parent().currentPriority();
62     itsList[threadPriority].addAtEnd(theNode);
63
64     if (itsHighestPriorityMember < threadPriority) {
65       itsHighestPriorityMember = threadPriority;
66     }
67
68     //Verify.endAtomic();
69
}
70
71   public threadListNode head () {
72     return highestPriorityMember().head();
73   }
74
75   public void mergeList (PriorityListOfThreads otherList) {
76     //Verify.beginAtomic();
77
threadList mine = itsList[0];
78     threadList his = otherList.itsList[0];
79     int end = otherList.itsHighestPriorityMember + 1;
80     int i = 0;
81
82     do {
83       mine.mergeList(his);
84       i++;
85       mine = itsList[i];
86       his = otherList.itsList[i];
87     } while (i != end);
88
89     if (itsHighestPriorityMember < otherList.itsHighestPriorityMember) {
90       itsHighestPriorityMember = otherList.itsHighestPriorityMember;
91     }
92
93     otherList.itsHighestPriorityMember = 0;
94
95     //Verify.endAtomic();
96
}
97
98   public void mergeList (threadList otherList) {
99     //Verify.beginAtomic();
100
//System.out.println("PriorityListOfThreads.mergeList");
101
if (!otherList.isEmpty()) {
102       int otherListPriority = otherList.head().parent().currentPriority();
103
104
105       //System.out.println("otherListPriority = " + otherListPriority);
106
//System.out.println("itsHighestPriorityMember = "
107
// +itsHighestPriorityMember);
108
itsList[otherListPriority].mergeList(otherList);
109
110       if (itsHighestPriorityMember < otherListPriority) {
111         itsHighestPriorityMember = otherListPriority;
112       }
113     }
114
115     //Verify.endAtomic();
116
}
117
118   private threadList highestPriorityMember () {
119     //Verify.beginAtomic();
120
int hipri = itsHighestPriorityMember;
121
122     for (; hipri > 0; hipri--) {
123       if (!itsList[hipri].isEmpty()) {
124         break;
125       }
126     }
127
128     itsHighestPriorityMember = hipri;
129
130     //System.out.println("HIPRI = " + hipri);
131
//Verify.endAtomic();
132
return itsList[hipri];
133   }
134 }
Popular Tags