Closes #1283: Add custom Callable to prevent memory leaks

This commit is contained in:
yueh 2015-08-06 23:30:26 +02:00 committed by thatsIch
parent 20a6e7631f
commit 621952e37d
10 changed files with 163 additions and 77 deletions

View File

@ -25,7 +25,6 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import net.minecraft.world.World;
@ -48,6 +47,7 @@ import appeng.crafting.CraftingJob;
import appeng.me.Grid;
import appeng.me.NetworkList;
import appeng.tile.AEBaseTile;
import appeng.util.IWorldCallable;
import appeng.util.Platform;
import com.google.common.base.Stopwatch;
@ -59,9 +59,9 @@ public class TickHandler
{
public static final TickHandler INSTANCE = new TickHandler();
final Queue<Callable> serverQueue = new LinkedList<Callable>();
final Queue<IWorldCallable<?>> serverQueue = new LinkedList<IWorldCallable<?>>();
final Multimap<World, CraftingJob> craftingJobs = LinkedListMultimap.create();
private final WeakHashMap<World, Queue<Callable>> callQueue = new WeakHashMap<World, Queue<Callable>>();
private final WeakHashMap<World, Queue<IWorldCallable<?>>> callQueue = new WeakHashMap<World, Queue<IWorldCallable<?>>>();
private final HandlerRep server = new HandlerRep();
private final HandlerRep client = new HandlerRep();
private final HashMap<Integer, PlayerColor> cliPlayerColors = new HashMap<Integer, PlayerColor>();
@ -77,7 +77,7 @@ public class TickHandler
return this.cliPlayerColors;
}
public void addCallable( World w, Callable c )
public void addCallable( World w, IWorldCallable<?> c )
{
if( w == null )
{
@ -85,11 +85,12 @@ public class TickHandler
}
else
{
Queue<Callable> queue = this.callQueue.get( w );
Queue<IWorldCallable<?>> queue = this.callQueue.get( w );
if( queue == null )
{
this.callQueue.put( w, queue = new LinkedList<Callable>() );
queue = new LinkedList<IWorldCallable<?>>();
this.callQueue.put( w, queue );
}
queue.add( c );
@ -235,13 +236,15 @@ public class TickHandler
}
// cross world queue.
this.processQueue( this.serverQueue );
this.processQueue( this.serverQueue, null );
}
// world synced queue(s)
if( ev.type == Type.WORLD && ev.phase == Phase.START )
{
this.processQueue( this.callQueue.get( ( (WorldTickEvent) ev ).world ) );
final World world = ( (WorldTickEvent) ev ).world;
final Queue<IWorldCallable<?>> queue = this.callQueue.get( world );
this.processQueue( queue, world );
}
}
@ -259,7 +262,7 @@ public class TickHandler
}
}
private void processQueue( Queue<Callable> queue )
private void processQueue( Queue<IWorldCallable<?>> queue, World world )
{
if( queue == null )
{
@ -268,12 +271,12 @@ public class TickHandler
Stopwatch sw = Stopwatch.createStarted();
Callable c = null;
IWorldCallable<?> c = null;
while( ( c = queue.poll() ) != null )
{
try
{
c.call();
c.call( world );
if( sw.elapsed( TimeUnit.MILLISECONDS ) > 50 )
{

View File

@ -25,7 +25,6 @@ import java.util.Deque;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
@ -53,6 +52,7 @@ import appeng.api.util.IReadOnlyCollection;
import appeng.core.worlddata.WorldData;
import appeng.hooks.TickHandler;
import appeng.me.pathfinding.IPathItem;
import appeng.util.IWorldCallable;
import appeng.util.ReadOnlyCollection;
@ -671,7 +671,7 @@ public class GridNode implements IGridNode, IPathItem
return lastUsedChannels;
}
private static class MachineSecurityBreak implements Callable<Void>
private static class MachineSecurityBreak implements IWorldCallable<Void>
{
private final GridNode node;
@ -681,7 +681,7 @@ public class GridNode implements IGridNode, IPathItem
}
@Override
public Void call() throws Exception
public Void call(World world) throws Exception
{
this.node.getMachine().securityBreak();

View File

@ -20,13 +20,15 @@ package appeng.me.cache.helpers;
import java.util.HashMap;
import java.util.concurrent.Callable;
import net.minecraft.world.World;
import appeng.api.networking.IGridNode;
import appeng.parts.p2p.PartP2PTunnelME;
import appeng.util.IWorldCallable;
public class Connections implements Callable
public class Connections implements IWorldCallable<Void>
{
public final HashMap<IGridNode, TunnelConnection> connections = new HashMap<IGridNode, TunnelConnection>();
@ -40,7 +42,7 @@ public class Connections implements Callable
}
@Override
public Object call() throws Exception
public Void call( World world ) throws Exception
{
this.me.updateConnections( this );

View File

@ -20,7 +20,6 @@ package appeng.parts.automation;
import java.util.List;
import java.util.concurrent.Callable;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
@ -32,6 +31,7 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -63,13 +63,14 @@ import appeng.hooks.TickHandler;
import appeng.me.GridAccessException;
import appeng.parts.PartBasicState;
import appeng.server.ServerHelper;
import appeng.util.IWorldCallable;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
import com.google.common.collect.Lists;
public class PartAnnihilationPlane extends PartBasicState implements IGridTickable, Callable<TickRateModulation>
public class PartAnnihilationPlane extends PartBasicState implements IGridTickable, IWorldCallable<TickRateModulation>
{
private final static IAESprite SIDE_ICON = CableBusTextures.PartPlaneSides.getIcon();
private final static IAESprite BACK_ICON = CableBusTextures.PartTransitionPlaneBack.getIcon();
@ -86,7 +87,7 @@ public class PartAnnihilationPlane extends PartBasicState implements IGridTickab
}
@Override
public TickRateModulation call() throws Exception
public TickRateModulation call(World world) throws Exception
{
this.breaking = false;
return this.breakBlock( true );

View File

@ -19,25 +19,60 @@
package appeng.parts.p2p;
//@InterfaceList(value = { @Interface(iface = "li.cil.oc.api.network.Environment", iname = "OpenComputers"), @Interface(iface = "li.cil.oc.api.network.SidedEnvironment", iname = "OpenComputers") })
//import javax.annotation.Nullable;
//
//import net.minecraft.item.ItemStack;
//import net.minecraft.nbt.NBTTagCompound;
//import net.minecraft.util.IIcon;
//import net.minecraft.world.World;
//import net.minecraftforge.common.util.ForgeDirection;
//
//import cpw.mods.fml.relauncher.Side;
//import cpw.mods.fml.relauncher.SideOnly;
//
//import li.cil.oc.api.API;
//import li.cil.oc.api.Items;
//import li.cil.oc.api.Network;
//import li.cil.oc.api.network.Environment;
//import li.cil.oc.api.network.Message;
//import li.cil.oc.api.network.Node;
//import li.cil.oc.api.network.SidedEnvironment;
//import li.cil.oc.api.network.Visibility;
//
//import appeng.api.networking.IGridNode;
//import appeng.api.networking.ticking.IGridTickable;
//import appeng.api.networking.ticking.TickRateModulation;
//import appeng.api.networking.ticking.TickingRequest;
//import appeng.core.AELog;
//import appeng.core.settings.TickRates;
//import appeng.hooks.TickHandler;
//import appeng.integration.IntegrationRegistry;
//import appeng.integration.IntegrationType;
//import appeng.me.GridAccessException;
//import appeng.transformer.annotations.Integration.Interface;
//import appeng.transformer.annotations.Integration.InterfaceList;
//import appeng.util.IWorldCallable;
//
//
//@InterfaceList( value = { @Interface( iface = "li.cil.oc.api.network.Environment", iname = "OpenComputers" ), @Interface( iface = "li.cil.oc.api.network.SidedEnvironment", iname = "OpenComputers" ) } )
//public final class PartP2POpenComputers extends PartP2PTunnel<PartP2POpenComputers> implements IGridTickable, Environment, SidedEnvironment
//{
// @Nullable
// private final Node node;
//
// private final Callable<Void> updateCallback;
// private final IWorldCallable<Void> updateCallback;
//
// public PartP2POpenComputers(ItemStack is)
// public PartP2POpenComputers( ItemStack is )
// {
// super( is );
//
// if ( !IntegrationRegistry.INSTANCE.isEnabled( IntegrationType.OpenComputers ) )
// if( !IntegrationRegistry.INSTANCE.isEnabled( IntegrationType.OpenComputers ) )
// {
// throw new RuntimeException( "OpenComputers is not installed!" );
// }
//
// // Avoid NPE when called in pre-init phase (part population).
// if ( API.network != null )
// if( API.network != null )
// {
// this.node = Network.newNode( this, Visibility.None ).create();
// }
@ -50,8 +85,8 @@ package appeng.parts.p2p;
// }
//
// @Override
// @SideOnly(Side.CLIENT)
// public TextureAtlasSprite getTypeTexture()
// @SideOnly( Side.CLIENT )
// public IIcon getTypeTexture()
// {
// return Items.get( "adapter" ).block().getBlockTextureFromSide( 2 );
// }
@ -60,7 +95,7 @@ package appeng.parts.p2p;
// public void removeFromWorld()
// {
// super.removeFromWorld();
// if ( this.node != null)
// if( this.node != null )
// {
// this.node.remove();
// }
@ -81,20 +116,20 @@ package appeng.parts.p2p;
// }
//
// @Override
// public void readFromNBT(NBTTagCompound data)
// public void readFromNBT( NBTTagCompound data )
// {
// super.readFromNBT( data );
// if ( this.node != null)
// if( this.node != null )
// {
// this.node.load( data );
// }
// }
//
// @Override
// public void writeToNBT(NBTTagCompound data)
// public void writeToNBT( NBTTagCompound data )
// {
// super.writeToNBT( data );
// if ( this.node != null)
// if( this.node != null )
// {
// this.node.save( data );
// }
@ -113,9 +148,9 @@ package appeng.parts.p2p;
// {
// if( !this.proxy.getPath().isNetworkBooting() )
// {
// if ( this.node() != null ) // Client side doesn't have nodes.
// if( this.node() != null ) // Client side doesn't have nodes.
// {
// TickHandler.INSTANCE.addCallable( this.tile.getWorld(), this.updateCallback );
// TickHandler.INSTANCE.addCallable( this.tile.getWorldObj(), this.updateCallback );
// }
//
// return TickRateModulation.SLEEP;
@ -131,14 +166,14 @@ package appeng.parts.p2p;
//
// private void updateConnections()
// {
// if ( this.proxy.isPowered() && this.proxy.isActive() )
// if( this.proxy.isPowered() && this.proxy.isActive() )
// {
// // Make sure we're connected to existing OC nodes in the world.
// Network.joinOrCreateNetwork( this.getTile() );
//
// if ( this.output )
// if( this.output )
// {
// if ( this.getInput() != null && this.node != null )
// if( this.getInput() != null && this.node != null )
// {
// Network.joinOrCreateNetwork( this.getInput().getTile() );
// this.node.connect( this.getInput().node() );
@ -148,22 +183,22 @@ package appeng.parts.p2p;
// {
// try
// {
// for ( PartP2POpenComputers output : this.getOutputs() )
// for( PartP2POpenComputers output : this.getOutputs() )
// {
// if ( this.node != null )
// if( this.node != null )
// {
// Network.joinOrCreateNetwork( output.getTile() );
// this.node.connect( output.node() );
// }
// }
// }
// catch ( GridAccessException e )
// catch( GridAccessException e )
// {
// AELog.error( e );
// }
// }
// }
// else if ( this.node != null )
// else if( this.node != null )
// {
// this.node.remove();
// }
@ -177,35 +212,38 @@ package appeng.parts.p2p;
// }
//
// @Override
// public void onConnect(Node node) {
// public void onConnect( Node node )
// {
// }
//
// @Override
// public void onDisconnect(Node node) {
// public void onDisconnect( Node node )
// {
// }
//
// @Override
// public void onMessage(Message message) {
// public void onMessage( Message message )
// {
// }
//
// @Nullable
// @Override
// public Node sidedNode(ForgeDirection side)
// public Node sidedNode( ForgeDirection side )
// {
// return side == this.side ? this.node : null;
// }
//
// @Override
// public boolean canConnect(ForgeDirection side)
// public boolean canConnect( ForgeDirection side )
// {
// return side == this.side;
// }
//
// private final class UpdateCallback implements Callable<Void>
// private final class UpdateCallback implements IWorldCallable<Void>
// {
// @Nullable
// @Override
// public Void call() throws Exception
// public Void call( World world ) throws Exception
// {
// PartP2POpenComputers.this.updateConnections();
//

View File

@ -19,12 +19,12 @@
package appeng.tile.spatial;
import java.util.concurrent.Callable;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import appeng.api.config.Actionable;
import appeng.api.config.PowerMultiplier;
import appeng.api.config.YesNo;
@ -46,10 +46,11 @@ import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkInvTile;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.InvOperation;
import appeng.util.IWorldCallable;
import appeng.util.Platform;
public class TileSpatialIOPort extends AENetworkInvTile implements Callable
public class TileSpatialIOPort extends AENetworkInvTile implements IWorldCallable<Void>
{
final int[] sides = { 0, 1 };
@ -122,9 +123,8 @@ public class TileSpatialIOPort extends AENetworkInvTile implements Callable
}
@Override
public Object call() throws Exception
public Void call( World world ) throws Exception
{
ItemStack cell = this.getStackInSlot( 0 );
if( this.isSpatialCell( cell ) && this.getStackInSlot( 1 ) == null )
{

View File

@ -19,30 +19,25 @@
package appeng.util;
import java.util.concurrent.Callable;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
public class BlockUpdate implements Callable
public class BlockUpdate implements IWorldCallable<Boolean>
{
final World w;
final BlockPos pos;
public BlockUpdate( World w, BlockPos pos )
public BlockUpdate( BlockPos pos )
{
this.w = w;
this.pos=pos;
}
@Override
public Object call() throws Exception
public Boolean call( World world ) throws Exception
{
if( this.w.isBlockLoaded( this.pos ) )
if ( world.isBlockLoaded( this.pos ) )
{
this.w.notifyNeighborsOfStateChange( this.pos, Platform.AIR_BLOCK );
world.notifyNeighborsOfStateChange( this.pos, Platform.AIR_BLOCK );
}
return true;

View File

@ -0,0 +1,49 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 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.
*
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import net.minecraft.world.World;
/**
* An interface similar to {@link Callable}, but allowing to pass the {@link World} when calling.
*
* @see Callable
* @author yueh
* @version rv3
* @since rv3
*/
public interface IWorldCallable<T>
{
/**
* Similar to {@link Callable#call()}
*
* @see Callable#call()
* @param world
* @return
* @throws Exception
*/
@Nullable
T call( @Nullable World world ) throws Exception;
}

View File

@ -2063,7 +2063,7 @@ public class Platform
{
if( !worldObj.isRemote )
{
TickHandler.INSTANCE.addCallable( worldObj, new BlockUpdate( worldObj, pos ) );
TickHandler.INSTANCE.addCallable( worldObj, new BlockUpdate( pos ) );
}
}

View File

@ -20,7 +20,6 @@ package appeng.worldgen;
import java.util.Random;
import java.util.concurrent.Callable;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
@ -31,6 +30,7 @@ import appeng.core.AEConfig;
import appeng.core.features.registries.WorldGenRegistry;
import appeng.core.worlddata.WorldData;
import appeng.hooks.TickHandler;
import appeng.util.IWorldCallable;
import appeng.util.Platform;
import appeng.worldgen.meteorite.ChunkOnly;
@ -49,11 +49,11 @@ public final class MeteoriteWorldGen implements IWorldGenerator
int z = r.nextInt( 16 ) + ( chunkZ << 4 );
int depth = 180 + r.nextInt( 20 );
TickHandler.INSTANCE.addCallable( w, new MeteoriteSpawn( x, depth, z, w ) );
TickHandler.INSTANCE.addCallable( w, new MeteoriteSpawn( x, depth, z ) );
}
else
{
TickHandler.INSTANCE.addCallable( w, new MeteoriteSpawn( chunkX << 4, 128, chunkZ << 4, w ) );
TickHandler.INSTANCE.addCallable( w, new MeteoriteSpawn( chunkX << 4, 128, chunkZ << 4 ) );
}
}
else
@ -111,24 +111,22 @@ public final class MeteoriteWorldGen implements IWorldGenerator
return WorldData.instance().spawnData().getNearByMeteorites( w.provider.getDimensionId(), chunkX, chunkZ );
}
class MeteoriteSpawn implements Callable
class MeteoriteSpawn implements IWorldCallable<Object>
{
final int x;
final int z;
final World w;
final int depth;
public MeteoriteSpawn( int x, int depth, int z, World w )
public MeteoriteSpawn( int x, int depth, int z )
{
this.x = x;
this.z = z;
this.w = w;
this.depth = depth;
}
@Override
public Object call() throws Exception
public Object call( World world ) throws Exception
{
int chunkX = this.x >> 4;
int chunkZ = this.z >> 4;
@ -136,10 +134,10 @@ public final class MeteoriteWorldGen implements IWorldGenerator
double minSqDist = Double.MAX_VALUE;
// near by meteorites!
for( NBTTagCompound data : MeteoriteWorldGen.this.getNearByMeteorites( this.w, chunkX, chunkZ ) )
for( NBTTagCompound data : MeteoriteWorldGen.this.getNearByMeteorites( world, chunkX, chunkZ ) )
{
MeteoritePlacer mp = new MeteoritePlacer();
mp.spawnMeteorite( new ChunkOnly( this.w, chunkX, chunkZ ), data );
mp.spawnMeteorite( new ChunkOnly( world, chunkX, chunkZ ), data );
minSqDist = Math.min( minSqDist, mp.getSqDistance( this.x, this.z ) );
}
@ -148,11 +146,11 @@ public final class MeteoriteWorldGen implements IWorldGenerator
if( minSqDist > AEConfig.instance.minMeteoriteDistanceSq || isCluster )
{
MeteoriteWorldGen.this.tryMeteorite( this.w, this.depth, this.x, this.z );
MeteoriteWorldGen.this.tryMeteorite( world, this.depth, this.x, this.z );
}
WorldData.instance().spawnData().setGenerated( this.w.provider.getDimensionId(), chunkX, chunkZ );
WorldData.instance().compassData().service().updateArea( this.w, chunkX, chunkZ );
WorldData.instance().spawnData().setGenerated( world.provider.getDimensionId(), chunkX, chunkZ );
WorldData.instance().compassData().service().updateArea( world, chunkX, chunkZ );
return null;
}