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