properly implement locks

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

View file

@ -29,37 +29,32 @@ 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(); wl.lock();
try { try {
wl.lock(); long outer[][] = values[mainIdx], inner[];
try { if (outer == null)
long outer[][] = values[mainIdx], inner[]; values[mainIdx] = outer = new long[256][];
if (outer == null) inner = outer[outerIdx];
values[mainIdx] = outer = new long[256][]; if (inner == null) {
inner = outer[outerIdx]; synchronized (this) {
if (inner == null) { outer[outerIdx] = inner = new long[1];
synchronized (this) { inner[0] = key;
outer[outerIdx] = inner = new long[1];
inner[0] = key;
count++;
}
} else {
int i;
for (i = 0; i < inner.length; i++) {
if (inner[i] == key) {
return;
}
}
inner = Arrays_copyOf(inner, i + 1);
outer[outerIdx] = inner;
inner[i] = key;
count++; count++;
} }
} finally { } else {
wl.unlock(); int i;
for (i = 0; i < inner.length; i++) {
if (inner[i] == key) {
return;
}
}
inner = Arrays_copyOf(inner, i + 1);
outer[outerIdx] = inner;
inner[i] = key;
count++;
} }
} finally { } finally {
rl.unlock(); wl.unlock();
} }
} }
@ -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(); count--;
try { if (i != max) {
count--; inner[i] = inner[max];
if (i != max) {
inner[i] = inner[max];
}
outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays_copyOf(inner, max));
} finally {
wl.unlock();
} }
outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays_copyOf(inner, max));
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,20 +118,14 @@ 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(); count--;
try { long ret = inner[inner.length - 1];
count--; outer[i] = Arrays_copyOf(inner, inner.length - 1);
long ret = inner[inner.length - 1]; return ret;
outer[i] = Arrays_copyOf(inner, inner.length - 1);
return ret;
} finally {
wl.unlock();
}
} }
} }
} finally { } finally {
rl.unlock(); wl.unlock();
} }
return 0; return 0;
} }