Skip to content

Commit 0fbaa95

Browse files
authored
fix: alter synchronisation of getblocks trim implementation (#3288)
1 parent 66d919b commit 0fbaa95

File tree

8 files changed

+149
-80
lines changed

8 files changed

+149
-80
lines changed

worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightGetBlocks.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,19 +1001,29 @@ public boolean hasNonEmptySection(int layer) {
10011001

10021002
@Override
10031003
@SuppressWarnings("unchecked")
1004-
public synchronized boolean trim(boolean aggressive) {
1005-
skyLight = new DataLayer[getSectionCount()];
1006-
blockLight = new DataLayer[getSectionCount()];
1004+
public boolean trim(boolean aggressive) {
1005+
synchronized (this) {
1006+
if (sections == null && (!aggressive || levelChunk == null)) {
1007+
skyLight = new DataLayer[getSectionCount()];
1008+
blockLight = new DataLayer[getSectionCount()];
1009+
return !aggressive || super.trim(true);
1010+
}
1011+
}
10071012
if (aggressive) {
10081013
sectionLock.writeLock().lock();
1009-
sections = null;
1010-
levelChunk = null;
1011-
sectionLock.writeLock().unlock();
1012-
return super.trim(true);
1013-
} else if (sections == null) {
1014-
// don't bother trimming if there are no sections stored.
1015-
return true;
1016-
} else {
1014+
try {
1015+
synchronized (this) {
1016+
skyLight = new DataLayer[getSectionCount()];
1017+
blockLight = new DataLayer[getSectionCount()];
1018+
sections = null;
1019+
levelChunk = null;
1020+
return super.trim(true);
1021+
}
1022+
} finally {
1023+
sectionLock.writeLock().unlock();
1024+
}
1025+
}
1026+
synchronized (this) {
10171027
for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) {
10181028
int layer = i - getMinSectionPosition();
10191029
if (!hasSection(i) || super.blocks[layer] == null) {

worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightGetBlocks.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,19 +1002,29 @@ public boolean hasNonEmptySection(int layer) {
10021002

10031003
@Override
10041004
@SuppressWarnings("unchecked")
1005-
public synchronized boolean trim(boolean aggressive) {
1006-
skyLight = new DataLayer[getSectionCount()];
1007-
blockLight = new DataLayer[getSectionCount()];
1005+
public boolean trim(boolean aggressive) {
1006+
synchronized (this) {
1007+
if (sections == null && (!aggressive || levelChunk == null)) {
1008+
skyLight = new DataLayer[getSectionCount()];
1009+
blockLight = new DataLayer[getSectionCount()];
1010+
return !aggressive || super.trim(true);
1011+
}
1012+
}
10081013
if (aggressive) {
10091014
sectionLock.writeLock().lock();
1010-
sections = null;
1011-
levelChunk = null;
1012-
sectionLock.writeLock().unlock();
1013-
return super.trim(true);
1014-
} else if (sections == null) {
1015-
// don't bother trimming if there are no sections stored.
1016-
return true;
1017-
} else {
1015+
try {
1016+
synchronized (this) {
1017+
skyLight = new DataLayer[getSectionCount()];
1018+
blockLight = new DataLayer[getSectionCount()];
1019+
sections = null;
1020+
levelChunk = null;
1021+
return super.trim(true);
1022+
}
1023+
} finally {
1024+
sectionLock.writeLock().unlock();
1025+
}
1026+
}
1027+
synchronized (this) {
10181028
for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) {
10191029
int layer = i - getMinSectionPosition();
10201030
if (!hasSection(i) || super.blocks[layer] == null) {

worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightGetBlocks.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,19 +1003,29 @@ public boolean hasNonEmptySection(int layer) {
10031003

10041004
@Override
10051005
@SuppressWarnings("unchecked")
1006-
public synchronized boolean trim(boolean aggressive) {
1007-
skyLight = new DataLayer[getSectionCount()];
1008-
blockLight = new DataLayer[getSectionCount()];
1006+
public boolean trim(boolean aggressive) {
1007+
synchronized (this) {
1008+
if (sections == null && (!aggressive || levelChunk == null)) {
1009+
skyLight = new DataLayer[getSectionCount()];
1010+
blockLight = new DataLayer[getSectionCount()];
1011+
return !aggressive || super.trim(true);
1012+
}
1013+
}
10091014
if (aggressive) {
10101015
sectionLock.writeLock().lock();
1011-
sections = null;
1012-
levelChunk = null;
1013-
sectionLock.writeLock().unlock();
1014-
return super.trim(true);
1015-
} else if (sections == null) {
1016-
// don't bother trimming if there are no sections stored.
1017-
return true;
1018-
} else {
1016+
try {
1017+
synchronized (this) {
1018+
skyLight = new DataLayer[getSectionCount()];
1019+
blockLight = new DataLayer[getSectionCount()];
1020+
sections = null;
1021+
levelChunk = null;
1022+
return super.trim(true);
1023+
}
1024+
} finally {
1025+
sectionLock.writeLock().unlock();
1026+
}
1027+
}
1028+
synchronized (this) {
10191029
for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) {
10201030
int layer = i - getMinSectionPosition();
10211031
if (!hasSection(i) || super.blocks[layer] == null) {

worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,19 +1037,29 @@ public boolean hasNonEmptySection(int layer) {
10371037

10381038
@Override
10391039
@SuppressWarnings("unchecked")
1040-
public synchronized boolean trim(boolean aggressive) {
1041-
skyLight = new DataLayer[getSectionCount()];
1042-
blockLight = new DataLayer[getSectionCount()];
1040+
public boolean trim(boolean aggressive) {
1041+
synchronized (this) {
1042+
if (sections == null && (!aggressive || levelChunk == null)) {
1043+
skyLight = new DataLayer[getSectionCount()];
1044+
blockLight = new DataLayer[getSectionCount()];
1045+
return !aggressive || super.trim(true);
1046+
}
1047+
}
10431048
if (aggressive) {
10441049
sectionLock.writeLock().lock();
1045-
sections = null;
1046-
levelChunk = null;
1047-
sectionLock.writeLock().unlock();
1048-
return super.trim(true);
1049-
} else if (sections == null) {
1050-
// don't bother trimming if there are no sections stored.
1051-
return true;
1052-
} else {
1050+
try {
1051+
synchronized (this) {
1052+
skyLight = new DataLayer[getSectionCount()];
1053+
blockLight = new DataLayer[getSectionCount()];
1054+
sections = null;
1055+
levelChunk = null;
1056+
return super.trim(true);
1057+
}
1058+
} finally {
1059+
sectionLock.writeLock().unlock();
1060+
}
1061+
}
1062+
synchronized (this) {
10531063
for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) {
10541064
int layer = i - getMinSectionPosition();
10551065
if (!hasSection(i) || super.blocks[layer] == null) {

worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -996,19 +996,29 @@ public boolean hasNonEmptySection(int layer) {
996996

997997
@Override
998998
@SuppressWarnings("unchecked")
999-
public synchronized boolean trim(boolean aggressive) {
1000-
skyLight = new DataLayer[getSectionCount()];
1001-
blockLight = new DataLayer[getSectionCount()];
999+
public boolean trim(boolean aggressive) {
1000+
synchronized (this) {
1001+
if (sections == null && (!aggressive || levelChunk == null)) {
1002+
skyLight = new DataLayer[getSectionCount()];
1003+
blockLight = new DataLayer[getSectionCount()];
1004+
return !aggressive || super.trim(true);
1005+
}
1006+
}
10021007
if (aggressive) {
10031008
sectionLock.writeLock().lock();
1004-
sections = null;
1005-
levelChunk = null;
1006-
sectionLock.writeLock().unlock();
1007-
return super.trim(true);
1008-
} else if (sections == null) {
1009-
// don't bother trimming if there are no sections stored.
1010-
return true;
1011-
} else {
1009+
try {
1010+
synchronized (this) {
1011+
skyLight = new DataLayer[getSectionCount()];
1012+
blockLight = new DataLayer[getSectionCount()];
1013+
sections = null;
1014+
levelChunk = null;
1015+
return super.trim(true);
1016+
}
1017+
} finally {
1018+
sectionLock.writeLock().unlock();
1019+
}
1020+
}
1021+
synchronized (this) {
10121022
for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) {
10131023
int layer = i - getMinSectionPosition();
10141024
if (!hasSection(i) || super.blocks[layer] == null) {

worldedit-bukkit/adapters/adapter-1_21_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_5/PaperweightGetBlocks.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -996,19 +996,29 @@ public boolean hasNonEmptySection(int layer) {
996996

997997
@Override
998998
@SuppressWarnings("unchecked")
999-
public synchronized boolean trim(boolean aggressive) {
1000-
skyLight = new DataLayer[getSectionCount()];
1001-
blockLight = new DataLayer[getSectionCount()];
999+
public boolean trim(boolean aggressive) {
1000+
synchronized (this) {
1001+
if (sections == null && (!aggressive || levelChunk == null)) {
1002+
skyLight = new DataLayer[getSectionCount()];
1003+
blockLight = new DataLayer[getSectionCount()];
1004+
return !aggressive || super.trim(true);
1005+
}
1006+
}
10021007
if (aggressive) {
10031008
sectionLock.writeLock().lock();
1004-
sections = null;
1005-
levelChunk = null;
1006-
sectionLock.writeLock().unlock();
1007-
return super.trim(true);
1008-
} else if (sections == null) {
1009-
// don't bother trimming if there are no sections stored.
1010-
return true;
1011-
} else {
1009+
try {
1010+
synchronized (this) {
1011+
skyLight = new DataLayer[getSectionCount()];
1012+
blockLight = new DataLayer[getSectionCount()];
1013+
sections = null;
1014+
levelChunk = null;
1015+
return super.trim(true);
1016+
}
1017+
} finally {
1018+
sectionLock.writeLock().unlock();
1019+
}
1020+
}
1021+
synchronized (this) {
10121022
for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) {
10131023
int layer = i - getMinSectionPosition();
10141024
if (!hasSection(i) || super.blocks[layer] == null) {

worldedit-bukkit/adapters/adapter-1_21_6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_6/PaperweightGetBlocks.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,19 +1004,29 @@ public boolean hasNonEmptySection(int layer) {
10041004

10051005
@Override
10061006
@SuppressWarnings("unchecked")
1007-
public synchronized boolean trim(boolean aggressive) {
1008-
skyLight = new DataLayer[getSectionCount()];
1009-
blockLight = new DataLayer[getSectionCount()];
1007+
public boolean trim(boolean aggressive) {
1008+
synchronized (this) {
1009+
if (sections == null && (!aggressive || levelChunk == null)) {
1010+
skyLight = new DataLayer[getSectionCount()];
1011+
blockLight = new DataLayer[getSectionCount()];
1012+
return !aggressive || super.trim(true);
1013+
}
1014+
}
10101015
if (aggressive) {
10111016
sectionLock.writeLock().lock();
1012-
sections = null;
1013-
levelChunk = null;
1014-
sectionLock.writeLock().unlock();
1015-
return super.trim(true);
1016-
} else if (sections == null) {
1017-
// don't bother trimming if there are no sections stored.
1018-
return true;
1019-
} else {
1017+
try {
1018+
synchronized (this) {
1019+
skyLight = new DataLayer[getSectionCount()];
1020+
blockLight = new DataLayer[getSectionCount()];
1021+
sections = null;
1022+
levelChunk = null;
1023+
return super.trim(true);
1024+
}
1025+
} finally {
1026+
sectionLock.writeLock().unlock();
1027+
}
1028+
}
1029+
synchronized (this) {
10201030
for (int i = getMinSectionPosition(); i <= getMaxSectionPosition(); i++) {
10211031
int layer = i - getMinSectionPosition();
10221032
if (!hasSection(i) || super.blocks[layer] == null) {

worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/implementation/chunk/ChunkCache.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ public synchronized boolean trim(boolean aggressive) {
6262
if (!aggressive) {
6363
return false;
6464
}
65-
synchronized (igb) {
66-
igb.trim(true);
67-
}
65+
// Don't synchronise here, let the implementation handle it
66+
igb.trim(true);
6867
}
6968
}
7069
return result;

0 commit comments

Comments
 (0)