1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-13 11:40:29 +00:00

Attempt to cut out a lot of synchronized calls

A lot of these don't actually have any effect as they'll only be called
on the main thread or they are getters where the state is guaranteed to
be consistent whenever it is accessed.

Hopefully this'll reduce the chance of world updates being blocked by
waiting for peripheral locks to be released.
This commit is contained in:
SquidDev 2018-08-25 21:17:48 +01:00
parent efa57521c7
commit c6bd88f3ad
7 changed files with 28 additions and 45 deletions

View File

@ -112,12 +112,12 @@ public abstract class TilePeripheralBase extends TileGeneric
}
}
public synchronized int getAnim()
public int getAnim()
{
return m_anim;
}
public synchronized void setAnim( int anim )
public void setAnim( int anim )
{
if( anim != m_anim )
{
@ -127,12 +127,12 @@ public abstract class TilePeripheralBase extends TileGeneric
}
@Override
public synchronized void update()
public void update()
{
if( m_changed )
{
updateBlock();
m_changed = false;
updateBlock();
}
}

View File

@ -80,12 +80,9 @@ public class TileDiskDrive extends TilePeripheralBase
public void destroy()
{
ejectContents( true );
synchronized( this )
if( m_recordPlaying )
{
if( m_recordPlaying )
{
stopRecord();
}
stopRecord();
}
}
@ -170,13 +167,10 @@ public class TileDiskDrive extends TilePeripheralBase
super.update();
// Ejection
synchronized( this )
if( m_ejectQueued )
{
if( m_ejectQueued )
{
ejectContents( false );
m_ejectQueued = false;
}
ejectContents( false );
m_ejectQueued = false;
}
// Music
@ -391,10 +385,7 @@ public class TileDiskDrive extends TilePeripheralBase
@Override
public void clear()
{
synchronized( this )
{
setInventorySlotContents( 0, ItemStack.EMPTY );
}
setInventorySlotContents( 0, ItemStack.EMPTY );
}
@Override
@ -425,18 +416,12 @@ public class TileDiskDrive extends TilePeripheralBase
@Nonnull
public ItemStack getDiskStack()
{
synchronized( this )
{
return getStackInSlot( 0 );
}
return getStackInSlot( 0 );
}
public void setDiskStack( @Nonnull ItemStack stack )
{
synchronized( this )
{
setInventorySlotContents( 0, stack );
}
setInventorySlotContents( 0, stack );
}
public IMedia getDiskMedia()
@ -569,7 +554,7 @@ public class TileDiskDrive extends TilePeripheralBase
}
}
private synchronized void updateAnim()
private void updateAnim()
{
if( !m_diskStack.isEmpty() )
{

View File

@ -75,7 +75,7 @@ public abstract class ModemPeripheral
m_open = false;
}
public synchronized boolean pollChanged()
public boolean pollChanged()
{
if( m_changed )
{
@ -85,9 +85,9 @@ public abstract class ModemPeripheral
return false;
}
public synchronized boolean isActive()
public boolean isActive()
{
return (m_computer != null) && m_open;
return m_computer != null && m_open;
}
@Override

View File

@ -37,7 +37,7 @@ public abstract class TileModemBase extends TilePeripheralBase
protected abstract ModemPeripheral createPeripheral();
@Override
public synchronized void destroy()
public void destroy()
{
if( m_modem != null )
{

View File

@ -190,10 +190,7 @@ public class TilePrinter extends TilePeripheralBase
@Override
public ItemStack getStackInSlot(int i)
{
synchronized( m_inventory )
{
return m_inventory.get( i );
}
return m_inventory.get( i );
}
@Nonnull

View File

@ -20,6 +20,7 @@ import net.minecraft.world.World;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.atomic.AtomicInteger;
import static dan200.computercraft.core.apis.ArgumentHelper.getString;
import static dan200.computercraft.core.apis.ArgumentHelper.optReal;
@ -28,13 +29,13 @@ public class SpeakerPeripheral implements IPeripheral {
private TileSpeaker m_speaker;
private long m_clock;
private long m_lastPlayTime;
private int m_notesThisTick;
private final AtomicInteger m_notesThisTick;
public SpeakerPeripheral()
{
m_clock = 0;
m_lastPlayTime = 0;
m_notesThisTick = 0;
m_notesThisTick = new AtomicInteger( );
}
SpeakerPeripheral(TileSpeaker speaker)
@ -43,10 +44,10 @@ public class SpeakerPeripheral implements IPeripheral {
m_speaker = speaker;
}
public synchronized void update()
public void update()
{
m_clock++;
m_notesThisTick = 0;
m_notesThisTick.set( 0 );
}
public World getWorld()
@ -59,9 +60,9 @@ public class SpeakerPeripheral implements IPeripheral {
return m_speaker.getPos();
}
public synchronized boolean madeSound(long ticks)
public boolean madeSound(long ticks)
{
return (m_clock - m_lastPlayTime <= ticks) ;
return m_clock - m_lastPlayTime <= ticks;
}
/* IPeripheral implementation */
@ -146,7 +147,7 @@ public class SpeakerPeripheral implements IPeripheral {
if( returnValue[0] instanceof Boolean && (Boolean) returnValue[0] )
{
m_notesThisTick++;
m_notesThisTick.incrementAndGet();
}
return returnValue;
@ -161,7 +162,7 @@ public class SpeakerPeripheral implements IPeripheral {
ResourceLocation resourceName = new ResourceLocation( name );
if( m_clock - m_lastPlayTime >= TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS || ( ( m_clock - m_lastPlayTime == 0 ) && ( m_notesThisTick < ComputerCraft.maxNotesPerTick ) && isNote ) )
if( m_clock - m_lastPlayTime >= TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS || (isNote && m_clock - m_lastPlayTime == 0 && m_notesThisTick.get() < ComputerCraft.maxNotesPerTick) )
{
if( SoundEvent.REGISTRY.containsKey(resourceName) )
{

View File

@ -25,7 +25,7 @@ public class TileSpeaker extends TilePeripheralBase
}
@Override
public synchronized void update()
public void update()
{
m_peripheral.update();
}