summaryrefslogtreecommitdiff
path: root/java/apps/NexusII/src/MT_Bounded_Queue.java
blob: a9b493fa263d617a8aebc6273257414015d8b44e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

// package NexusII.util ; 

// The minimun functionality to be associated with a queue 
interface Queue 
{
public void nq(Object item);
public Object dq();
public boolean is_full();
public boolean is_empty();
public static final int DEF_SIZE = 1; 
}; 

// Specific Conditions associated with MT_Bounded_Queue 
// Is true if there is space in the queue 

class Not_Full_Condition extends Timed_Wait 
{

public Not_Full_Condition(MT_Bounded_Queue q)
  {
   super(q);
  }
  
public synchronized boolean condition()
  {
    MT_Bounded_Queue mq_ = (MT_Bounded_Queue) object_;
    return !mq_.is_full ();
  }
  
}

// Is true if there's something in the queue 

class Not_Empty_Condition extends Timed_Wait
{

public Not_Empty_Condition(MT_Bounded_Queue q)
  {
    super(q); 
  }
  
public synchronized boolean condition()
  {
    // Delegate to the appropriate conditional
    // check on the MessageQueue.
    MT_Bounded_Queue mq_ = (MT_Bounded_Queue) object_;
    return !mq_.is_empty ();
  }

private MT_Bounded_Queue mq_ ; 
}




// Encapsulates a bounded - synchronized queue 

public class MT_Bounded_Queue implements Queue 
{


private Object[] queue_ ; 
private int front_  ; 
private int back_   ; 
private int max_size_ ; 
private int size_ ; 
private Not_Empty_Condition not_empty_condition_ ; 
private Not_Full_Condition not_full_condition_ ; 
private int nq_count_ ; 
private int dq_count_ ; 

// The counters counts the number of nq's and dq's operations made on this
// instance of the queue 

public int dq_count()
{
  return dq_count_ ; 
}

public int nq_count()
{
  return nq_count_ ; 
}

public MT_Bounded_Queue()
  {
    // 	call the other constructor with DEF_SIZE 
    this(DEF_SIZE);
  }
  
public MT_Bounded_Queue(int max_size)
  {
    this.front_ = 0 ; 
    this.back_  = 0 ; 
    this.max_size_  = max_size ; 
    this.size_ = 0 ; 
    // these are included for STATISTICS 
    this.nq_count_ = 0 ; 
    this.dq_count_ = 0 ; 
    this.queue_ = new Object[this.max_size_];
    not_full_condition_  = new Not_Full_Condition(this);
    not_empty_condition_ = new Not_Empty_Condition(this);
  }

// Blocking nq 
public synchronized void nq(Object item)
  {
    // Wait till the queue has space 
    while(is_full())
      {
	try {
	  wait();
	} catch (InterruptedException e) {}
      }

    // enqueue here 
    queue_[back_] = item ; 
    back_ = (back_ + 1) % max_size_  ; 
    size_++ ; 
    // One more enqueue operation has occured 
    nq_count_ ++ ; 
    // wakeup the sleeping guys 
    notifyAll();
  }

  // Timed nq 
// returns -1 if timed_out 
public synchronized int nq(Object item,long msec_timeout)
  {
    // Wait till the queue has space 
    try {
      not_full_condition_.timed_wait(msec_timeout);
    } catch (InterruptedException e) {}
    catch (TimeoutException t) 
      { 
	return -1 ; 
      }
  
  // enqueue here 
  queue_[back_] = item ; 
  back_ = (back_ + 1) % max_size_  ; 
  size_++ ; 
  	
  // One more enqueue operation has occured 
  nq_count_ ++ ; 	
  // wakeup the sleeping consumers 
  not_empty_condition_.broadcast ();
  return 0 ; 
  }




  // Blockin dq 
public synchronized Object dq()
  {
    // wait till the queue has something in it 
    while(is_empty())
      {
	try {
	  wait();
	} catch (InterruptedException e) {}
      }

    // dequeue here 
    Object return_object = queue_[front_] ; 
    front_ = (front_ + 1) % max_size_ ; 
    size_ -- ; 
    // One more enqueue operation has occured 
    dq_count_ ++ ; 
    //wake up the sleeping producers
    notifyAll(); 
    return return_object ; 
  }


  // Timed dq 

public synchronized Object dq(long msec_timeout)
  {
    // wait till the queue has something in it 
    try {
      not_empty_condition_.timed_wait(msec_timeout);
    } catch (InterruptedException e) {}
    catch (TimeoutException t)
      {
	return null; 
      }

    // dequeue here 
    Object return_object = queue_[front_] ; 
    front_ = (front_ + 1) % max_size_ ; 
    size_ -- ; 
    
    // One more enqueue operation has occured 
    dq_count_ ++ ; 

    //wake up the sleeping guys 
    not_full_condition_.broadcast(); 
    return return_object ; 
  }

public boolean is_empty()
  {
    if (size_ == 0)
      return true ;
    else
      return false ; 
  
  } 

public boolean is_full()
  {
    if (size_ == max_size_)
      return true ; 
    else
      return false ; 
  }

}