From c06f3e0d7627e7fcfd4178bdb3200a1787577783 Mon Sep 17 00:00:00 2001 From: Tahg Date: Thu, 10 Mar 2011 21:37:55 -0500 Subject: [PATCH] properly implement locks --- .../bukkit/craftbukkit/util/LongHashset.java | 82 ++++++++----------- 1 file changed, 33 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java index 4422ec0797..148aedbf69 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java @@ -29,37 +29,32 @@ public class LongHashset extends LongHash public void add(long key) { int mainIdx = (int) (key & 255); int outerIdx = (int) ((key >> 32) & 255); - rl.lock(); + wl.lock(); try { - wl.lock(); - try { - long outer[][] = values[mainIdx], inner[]; - if (outer == null) - values[mainIdx] = outer = new long[256][]; - inner = outer[outerIdx]; - if (inner == null) { - synchronized (this) { - 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; + long outer[][] = values[mainIdx], inner[]; + if (outer == null) + values[mainIdx] = outer = new long[256][]; + inner = outer[outerIdx]; + if (inner == null) { + synchronized (this) { + outer[outerIdx] = inner = new long[1]; + inner[0] = key; count++; } - } finally { - wl.unlock(); + } 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++; } } finally { - rl.unlock(); + wl.unlock(); } } @@ -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(); + count--; + if (i != max) { + inner[i] = inner[max]; } + outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays_copyOf(inner, max)); return; } } } finally { - rl.unlock(); + wl.unlock(); } } public long popFirst() { - rl.lock(); + wl.lock(); try { for (long[][] outer : values) { if (outer == null) @@ -128,20 +118,14 @@ 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(); - } - + count--; + long ret = inner[inner.length - 1]; + outer[i] = Arrays_copyOf(inner, inner.length - 1); + return ret; } } } finally { - rl.unlock(); + wl.unlock(); } return 0; }