properly implement locks

This commit is contained in:
Tahg 2011-03-10 21:37:55 -05:00
parent c0b3e80d1d
commit c06f3e0d76

View file

@ -29,8 +29,6 @@ public class LongHashset extends LongHash
public void add(long key) {
int mainIdx = (int) (key & 255);
int outerIdx = (int) ((key >> 32) & 255);
rl.lock();
try {
wl.lock();
try {
long outer[][] = values[mainIdx], inner[];
@ -58,9 +56,6 @@ public class LongHashset extends LongHash
} finally {
wl.unlock();
}
} finally {
rl.unlock();
}
}
public boolean containsKey(long key) {
@ -87,7 +82,7 @@ public class LongHashset extends LongHash
}
public void remove(long key) {
rl.lock();
wl.lock();
try {
long[][] outer = this.values[(int) (key & 255)];
if (outer == null)
@ -100,26 +95,21 @@ public class LongHashset extends LongHash
int max = inner.length - 1;
for (int i = 0; i <= max; i++) {
if (inner[i] == key) {
wl.lock();
try {
count--;
if (i != max) {
inner[i] = inner[max];
}
outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays_copyOf(inner, max));
} finally {
wl.unlock();
}
return;
}
}
} finally {
rl.unlock();
wl.unlock();
}
}
public long popFirst() {
rl.lock();
wl.lock();
try {
for (long[][] outer : values) {
if (outer == null)
@ -128,21 +118,15 @@ public class LongHashset extends LongHash
long[] inner = outer[i];
if (inner == null || inner.length == 0)
continue;
wl.lock();
try {
count--;
long ret = inner[inner.length - 1];
outer[i] = Arrays_copyOf(inner, inner.length - 1);
return ret;
}
}
} finally {
wl.unlock();
}
}
}
} finally {
rl.unlock();
}
return 0;
}