-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelList.java
173 lines (153 loc) · 4.44 KB
/
ParallelList.java
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
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
// Some code repurposed from the textbook. ISBN: 978-0123973375
public class ParallelList {
class Node {
Lock l = new ReentrantLock();
Node next;
int key;
int data;
public int min;
public int max;
public Node(int key) {
this.key = key;
}
public Node(int key, int data) {
this.key = key;
this.data = data;
}
public void unlock() {
l.unlock();
}
public void lock() {
l.lock();
}
}
Node head;
AtomicInteger size;
public ParallelList() {
head = new Node(Integer.MIN_VALUE);
head.next = new Node(Integer.MAX_VALUE);
size = new AtomicInteger(2);
}
public boolean add(int item) {
int key = item;
while (true) {
Node pred = head;
Node curr = pred.next;
while (curr.key < key) {
pred = curr;
curr = curr.next;
}
pred.lock();
curr.lock();
try {
if (validate(pred, curr)) {
if (curr.key == key) {
return false;
} else {
Node node = new Node(item);
node.next = curr;
pred.next = node;
size.getAndIncrement();
return true;
}
}
} finally {
pred.unlock();
curr.unlock();
}
}
}
public boolean add(int item, int data) {
int key = item;
while (true) {
Node pred = head;
Node curr = pred.next;
while (curr.key < key) {
pred = curr;
curr = curr.next;
}
pred.lock();
curr.lock();
try {
if (validate(pred, curr)) {
if (curr.key == key) {
curr.max = item;
curr.min = item;
return false;
} else {
Node node = new Node(item, data);
node.next = curr;
pred.next = node;
size.getAndIncrement();
return true;
}
}
} finally {
pred.unlock();
curr.unlock();
}
}
}
public boolean remove(int item) {
int key = item;
while (true) {
Node pred = head;
Node curr = pred.next;
while (curr.key < key) {
pred = curr;
curr = curr.next;
}
pred.lock();
curr.lock();
try {
if (validate(pred, curr)) {
if (curr.key == key) {
pred.next = curr.next;
size.getAndDecrement();
return true;
} else {
return false;
}
}
} finally {
pred.unlock();
curr.unlock();
}
}
}
public boolean contains(int item) {
int key = item;
while (true) {
Node pred = this.head;
Node curr = pred.next;
while (curr.key < key) {
pred = curr;
curr = curr.next;
}
// lock it up
pred.lock();
curr.lock();
try {
if (validate(pred, curr)) {
return (curr.key == key);
}
} finally {
pred.unlock();
curr.unlock();
}
}
}
public boolean validate(Node pred, Node curr) {
Node node = head;
while (node.key <= pred.key) {
if (node == pred) {
return pred.next == curr;
}
node = node.next;
}
return false;
}
}