blob: f2a465a9025d31e6733d3f1a35bbe655556a563e (
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
|
package JACE.tests.Concurrency.Condition;
import JACE.ASX.TimeoutException;
import JACE.ASX.TimeValue;
import JACE.Concurrency.*;
public class SimpleMessageQueue
{
private int num_items_ = 0;
private int head_ = 0, tail_ = 0;
private Object[] queue_;
private Mutex lock_ = new Mutex ();
private Condition notFull_ = new Condition (lock_);
private Condition notEmpty_ = new Condition (lock_);
public SimpleMessageQueue(int size)
{
queue_ = new Object[size];
}
public void enqueue(Object element, TimeValue timeout)
throws TimeoutException, InterruptedException
{
try
{
lock_.acquire ();
while (this.isFull ())
notFull_.Wait (timeout);
if (tail_ == queue_.length)
tail_ = 0;
queue_[tail_] = element;
tail_++;
num_items_++;
notEmpty_.signal ();
}
finally
{
lock_.release ();
}
}
public Object dequeue (TimeValue timeout)
throws TimeoutException, InterruptedException
{
Object return_value = null;
try
{
lock_.acquire ();
while (this.isEmpty ())
notEmpty_.Wait (timeout);
return_value = queue_[head_];
head_++;
if (head_ == queue_.length)
head_ = 0;
num_items_--;
notFull_.signal ();
}
finally
{
lock_.release ();
}
return return_value;
}
public boolean isEmpty()
{
return num_items_ == 0;
}
public boolean isFull()
{
return num_items_ == queue_.length;
}
public int size()
{
return num_items_;
}
}
|