Skip to content
Permalink
Browse files

Fix data race with BlockType lazy fields

  • Loading branch information...
kenzierocks committed Jul 18, 2019
1 parent 5078c21 commit 6f594473841dabd66150adcb9332ff455c00b251
@@ -0,0 +1,75 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.util.concurrency;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

/**
* Thread-safe lazy reference.
*/
public class LazyReference<T> {

public static <T> LazyReference<T> from(Supplier<T> valueComputation) {
return new LazyReference<>(valueComputation);
}

// Memory saving technique: hold the computation info in the same reference field that we'll
// put the value into, so the memory possibly retained by those parts is GC'able as soon as
// it's no longer needed.

private static final class RefInfo<T> {
private final Lock lock = new ReentrantLock();
private final Supplier<T> valueComputation;

private RefInfo(Supplier<T> valueComputation) {
this.valueComputation = valueComputation;
}
}

private Object value;

private LazyReference(Supplier<T> valueComputation) {
this.value = new RefInfo<>(valueComputation);
}

// casts are safe, value is either RefInfo or T
@SuppressWarnings("unchecked")
public T getValue() {
Object v = value;
if (!(v instanceof RefInfo)) {
return (T) v;
}
RefInfo<T> refInfo = (RefInfo<T>) v;
refInfo.lock.lock();
try {
v = value;
if (!(v instanceof RefInfo)) {
return (T) v;
}
value = v = refInfo.valueComputation.get();
return (T) v;
} finally {
refInfo.lock.unlock();
}
}

}
@@ -19,40 +19,45 @@

package com.sk89q.worldedit.world.block;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.concurrency.LazyReference;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import com.sk89q.worldedit.world.registry.LegacyMapper;

import java.util.ArrayList;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Supplier;

import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkArgument;

public class BlockType implements Keyed {

public static final NamespacedRegistry<BlockType> REGISTRY = new NamespacedRegistry<>("block type");

private final String id;
private final Function<BlockState, BlockState> values;
private final AtomicReference<BlockState> defaultState = new AtomicReference<>();
private final AtomicReference<FuzzyBlockState> emptyFuzzy = new AtomicReference<>();
private final AtomicReference<Map<String, ? extends Property<?>>> properties = new AtomicReference<>();
private final AtomicReference<BlockMaterial> blockMaterial = new AtomicReference<>();
private final AtomicReference<Map<Map<Property<?>, Object>, BlockState>> blockStatesMap = new AtomicReference<>();
private final LazyReference<BlockState> defaultState
= LazyReference.from(this::computeDefaultState);
private final LazyReference<FuzzyBlockState> emptyFuzzy
= LazyReference.from(() -> new FuzzyBlockState(this));
private final LazyReference<Map<String, ? extends Property<?>>> properties
= LazyReference.from(() -> ImmutableMap.copyOf(WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getProperties(this)));
private final LazyReference<BlockMaterial> blockMaterial
= LazyReference.from(() -> WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getMaterial(this));
private final LazyReference<Map<Map<Property<?>, Object>, BlockState>> blockStatesMap
= LazyReference.from(() -> BlockState.generateStateMap(this));

public BlockType(String id) {
this(id, null);
@@ -67,24 +72,16 @@ public BlockType(String id, Function<BlockState, BlockState> values) {
this.values = values;
}

private <T> T updateField(AtomicReference<T> field, Supplier<T> value) {
T result = field.get();
if (result == null) {
// swap in new value, if someone doesn't beat us
T update = value.get();
if (field.compareAndSet(null, update)) {
// use ours
result = update;
} else {
// update to real value
result = field.get();
}
private BlockState computeDefaultState() {
BlockState defaultState = Iterables.getFirst(getBlockStatesMap().values(), null);
if (values != null) {
defaultState = values.apply(defaultState);
}
return result;
return defaultState;
}

private Map<Map<Property<?>, Object>, BlockState> getBlockStatesMap() {
return updateField(blockStatesMap, () -> BlockState.generateStateMap(this));
return blockStatesMap.getValue();
}

/**
@@ -117,8 +114,7 @@ public String getName() {
* @return The properties map
*/
public Map<String, ? extends Property<?>> getPropertyMap() {
return updateField(properties, () -> ImmutableMap.copyOf(WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getProperties(this)));
return properties.getValue();
}

/**
@@ -150,17 +146,11 @@ public String getName() {
* @return The default state
*/
public BlockState getDefaultState() {
return updateField(defaultState, () -> {
BlockState defaultState = new ArrayList<>(getBlockStatesMap().values()).get(0);
if (values != null) {
defaultState = values.apply(defaultState);
}
return defaultState;
});
return defaultState.getValue();
}

public FuzzyBlockState getFuzzyMatcher() {
return updateField(emptyFuzzy, () -> new FuzzyBlockState(this));
return emptyFuzzy.getValue();
}

/**
@@ -208,8 +198,7 @@ public ItemType getItemType() {
* @return The material
*/
public BlockMaterial getMaterial() {
return updateField(blockMaterial, () -> WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getMaterial(this));
return blockMaterial.getValue();
}

/**

0 comments on commit 6f59447

Please sign in to comment.
You can’t perform that action at this time.