mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-30 16:19:03 +01:00
properly implement locks
By: Tahg <tahgtahv@gmail.com>
This commit is contained in:
parent
b79442a3fb
commit
ff806bfa66
1 changed files with 33 additions and 49 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue