Method: waiting(int, int)

1: package org.overture.codegen.runtime;
2:
3: /**
4: * This is the Sentinel class which the inner classes are extending. It contains methods and variables for holding and
5: * manipulating the history counters existing in VDM++
6: *
7: * @author gkanos
8: */
9: public class Sentinel
10: {
11:         public volatile long[] act; // holds the #act history counter.
12:         public volatile long[] fin; // holds the #fin history counter.
13:         public volatile long[] req; // holds the #req history counter.
14:         public volatile long[] active; // holds the #active history counter.
15:         public volatile long[] waiting; // holds the #waiting history counter.
16:
17:         public EvaluatePP instance; // hold the instance of the class that the evaluatePP() is going to be used.
18:
19:         // the initialization of the instance of Sentinel class.
20:         // Takes as parameters the instance of the class implementing the EvaluatePP
21:         // and the number of methods to define the size of the arrays.
22:         public void init(EvaluatePP inst, long nrf)
23:         {
24:                 instance = inst;
25:                 act = new long[(int) nrf];
26:                 fin = new long[(int) nrf];
27:                 req = new long[(int) nrf];
28:                 active = new long[(int) nrf];
29:                 waiting = new long[(int) nrf];
30:         }
31:
32:         // This methods is used to enable the activation of a method.
33:         public synchronized void entering(long fnr2)
34:         {
35:                 int fnr = (int) fnr2; // Here the long value is casted into in to be used as index to the arrays.
36:                 requesting(fnr);// the #req counter is changed to add one to it.
37:                 try
38:                 {
39:                         if (!instance.evaluatePP(fnr).booleanValue()) // the first evaluation of the permition predicate.
40:                         {
41:                                 waiting(fnr, +1);// if the permission predicate is false. It add one to the #waiting counter.
42:                                 while (!instance.evaluatePP(fnr).booleanValue())// reevaluation of the permission predicate.
43:                                 {
44:                                         this.wait(); // actual thread wait method. This freeze the thread waiting for the execution of its
45:                                                                         // method.
46:                                 }
47:                                 waiting(fnr, -1); // if predicate changes to true, #waiting is changed to remove one.
48:                         }
49:                 } catch (InterruptedException e)
50:                 {
51:                 }
52:                 activating(fnr);// the #act and the #active counters are change to add one to them
53:         }
54:
55:         // this method is registering the termination of a method.
56:         public synchronized void leaving(long fnr2)
57:         {
58:                 int fn = (int) fnr2;
59:                 fin[fn]++; // changes the #fin counter adding one to it.
60:                 active[fn]--; // changes the #active counter removing one to it.
61:                 stateChanged();
62:         }
63:
64:         // this method notifies the threads that a counter has be changed to reevaluate their permission predicates.
65:         public synchronized void stateChanged()
66:         {
67:                 notifyAll();
68:         }
69:
70:         // The method that actually changes the #req history counter.
71:         private synchronized void requesting(int fn)
72:         {
73:                 req[fn]++;
74:                 stateChanged();
75:         }
76:
77:         // The method that actually changing the #act and #active history counters.
78:         private synchronized void activating(int fn)
79:         {
80:                 act[fn]++;
81:                 active[fn]++;
82:                 stateChanged();
83:         }
84:
85:         // The method that actually changing the #waiting history counter.
86:         // The offset defines how many methods of the same name are waiting.
87:         private synchronized void waiting(int fn, int offset)
88:         {
89:                 waiting[fn] += offset;
90:                 stateChanged();
91:         }
92: }