Skip to content

Commit c6bd88f

Browse files
committed
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.
1 parent efa5752 commit c6bd88f

File tree

7 files changed

+28
-45
lines changed

7 files changed

+28
-45
lines changed

src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ public void setDirection( EnumFacing dir )
112112
}
113113
}
114114

115-
public synchronized int getAnim()
115+
public int getAnim()
116116
{
117117
return m_anim;
118118
}
119119

120-
public synchronized void setAnim( int anim )
120+
public void setAnim( int anim )
121121
{
122122
if( anim != m_anim )
123123
{
@@ -127,12 +127,12 @@ public synchronized void setAnim( int anim )
127127
}
128128

129129
@Override
130-
public synchronized void update()
130+
public void update()
131131
{
132132
if( m_changed )
133133
{
134-
updateBlock();
135134
m_changed = false;
135+
updateBlock();
136136
}
137137
}
138138

src/main/java/dan200/computercraft/shared/peripheral/diskdrive/TileDiskDrive.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,9 @@ public TileDiskDrive()
8080
public void destroy()
8181
{
8282
ejectContents( true );
83-
synchronized( this )
83+
if( m_recordPlaying )
8484
{
85-
if( m_recordPlaying )
86-
{
87-
stopRecord();
88-
}
85+
stopRecord();
8986
}
9087
}
9188

@@ -170,13 +167,10 @@ public void update()
170167
super.update();
171168

172169
// Ejection
173-
synchronized( this )
170+
if( m_ejectQueued )
174171
{
175-
if( m_ejectQueued )
176-
{
177-
ejectContents( false );
178-
m_ejectQueued = false;
179-
}
172+
ejectContents( false );
173+
m_ejectQueued = false;
180174
}
181175

182176
// Music
@@ -391,10 +385,7 @@ public boolean isUsableByPlayer( @Nonnull EntityPlayer player )
391385
@Override
392386
public void clear()
393387
{
394-
synchronized( this )
395-
{
396-
setInventorySlotContents( 0, ItemStack.EMPTY );
397-
}
388+
setInventorySlotContents( 0, ItemStack.EMPTY );
398389
}
399390

400391
@Override
@@ -425,18 +416,12 @@ public IPeripheral getPeripheral( EnumFacing side )
425416
@Nonnull
426417
public ItemStack getDiskStack()
427418
{
428-
synchronized( this )
429-
{
430-
return getStackInSlot( 0 );
431-
}
419+
return getStackInSlot( 0 );
432420
}
433421

434422
public void setDiskStack( @Nonnull ItemStack stack )
435423
{
436-
synchronized( this )
437-
{
438-
setInventorySlotContents( 0, stack );
439-
}
424+
setInventorySlotContents( 0, stack );
440425
}
441426

442427
public IMedia getDiskMedia()
@@ -569,7 +554,7 @@ private synchronized void unmountDisk( IComputerAccess computer )
569554
}
570555
}
571556

572-
private synchronized void updateAnim()
557+
private void updateAnim()
573558
{
574559
if( !m_diskStack.isEmpty() )
575560
{

src/main/java/dan200/computercraft/shared/peripheral/modem/ModemPeripheral.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public synchronized void destroy()
7575
m_open = false;
7676
}
7777

78-
public synchronized boolean pollChanged()
78+
public boolean pollChanged()
7979
{
8080
if( m_changed )
8181
{
@@ -85,9 +85,9 @@ public synchronized boolean pollChanged()
8585
return false;
8686
}
8787

88-
public synchronized boolean isActive()
88+
public boolean isActive()
8989
{
90-
return (m_computer != null) && m_open;
90+
return m_computer != null && m_open;
9191
}
9292

9393
@Override

src/main/java/dan200/computercraft/shared/peripheral/modem/TileModemBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected TileModemBase()
3737
protected abstract ModemPeripheral createPeripheral();
3838

3939
@Override
40-
public synchronized void destroy()
40+
public void destroy()
4141
{
4242
if( m_modem != null )
4343
{

src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,7 @@ public boolean isEmpty()
190190
@Override
191191
public ItemStack getStackInSlot(int i)
192192
{
193-
synchronized( m_inventory )
194-
{
195-
return m_inventory.get( i );
196-
}
193+
return m_inventory.get( i );
197194
}
198195

199196
@Nonnull

src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import javax.annotation.Nonnull;
2222
import javax.annotation.Nullable;
23+
import java.util.concurrent.atomic.AtomicInteger;
2324

2425
import static dan200.computercraft.core.apis.ArgumentHelper.getString;
2526
import static dan200.computercraft.core.apis.ArgumentHelper.optReal;
@@ -28,13 +29,13 @@ public class SpeakerPeripheral implements IPeripheral {
2829
private TileSpeaker m_speaker;
2930
private long m_clock;
3031
private long m_lastPlayTime;
31-
private int m_notesThisTick;
32+
private final AtomicInteger m_notesThisTick;
3233

3334
public SpeakerPeripheral()
3435
{
3536
m_clock = 0;
3637
m_lastPlayTime = 0;
37-
m_notesThisTick = 0;
38+
m_notesThisTick = new AtomicInteger( );
3839
}
3940

4041
SpeakerPeripheral(TileSpeaker speaker)
@@ -43,10 +44,10 @@ public SpeakerPeripheral()
4344
m_speaker = speaker;
4445
}
4546

46-
public synchronized void update()
47+
public void update()
4748
{
4849
m_clock++;
49-
m_notesThisTick = 0;
50+
m_notesThisTick.set( 0 );
5051
}
5152

5253
public World getWorld()
@@ -59,9 +60,9 @@ public BlockPos getPos()
5960
return m_speaker.getPos();
6061
}
6162

62-
public synchronized boolean madeSound(long ticks)
63+
public boolean madeSound(long ticks)
6364
{
64-
return (m_clock - m_lastPlayTime <= ticks) ;
65+
return m_clock - m_lastPlayTime <= ticks;
6566
}
6667

6768
/* IPeripheral implementation */
@@ -146,7 +147,7 @@ private synchronized Object[] playNote( Object[] arguments, ILuaContext context
146147

147148
if( returnValue[0] instanceof Boolean && (Boolean) returnValue[0] )
148149
{
149-
m_notesThisTick++;
150+
m_notesThisTick.incrementAndGet();
150151
}
151152

152153
return returnValue;
@@ -161,7 +162,7 @@ private synchronized Object[] playSound( Object[] arguments, ILuaContext context
161162

162163
ResourceLocation resourceName = new ResourceLocation( name );
163164

164-
if( m_clock - m_lastPlayTime >= TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS || ( ( m_clock - m_lastPlayTime == 0 ) && ( m_notesThisTick < ComputerCraft.maxNotesPerTick ) && isNote ) )
165+
if( m_clock - m_lastPlayTime >= TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS || (isNote && m_clock - m_lastPlayTime == 0 && m_notesThisTick.get() < ComputerCraft.maxNotesPerTick) )
165166
{
166167
if( SoundEvent.REGISTRY.containsKey(resourceName) )
167168
{

src/main/java/dan200/computercraft/shared/peripheral/speaker/TileSpeaker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public TileSpeaker()
2525
}
2626

2727
@Override
28-
public synchronized void update()
28+
public void update()
2929
{
3030
m_peripheral.update();
3131
}

0 commit comments

Comments
 (0)