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

Try to remove some locking

Ideally we would be able to avoid locking at all on the main thread, but
needs must.
This commit is contained in:
SquidDev 2018-11-19 20:56:44 +00:00
parent 62cf921cc6
commit f9c91f288f
3 changed files with 43 additions and 34 deletions

View File

@ -26,7 +26,7 @@ import static dan200.computercraft.core.apis.ArgumentHelper.getInt;
public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPacketReceiver public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPacketReceiver
{ {
private IPacketNetwork m_network; private IPacketNetwork m_network;
private Set<IComputerAccess> m_computers = new HashSet<>( 1 ); private final Set<IComputerAccess> m_computers = new HashSet<>( 1 );
private final ModemState m_state; private final ModemState m_state;
protected ModemPeripheral( ModemState state ) protected ModemPeripheral( ModemState state )
@ -58,7 +58,7 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
setNetwork( getNetwork() ); setNetwork( getNetwork() );
} }
public synchronized void destroy() public void destroy()
{ {
setNetwork( null ); setNetwork( null );
} }
@ -68,7 +68,7 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
{ {
if( packet.getSender() == this || !m_state.isOpen( packet.getChannel() ) ) return; if( packet.getSender() == this || !m_state.isOpen( packet.getChannel() ) ) return;
synchronized( this ) synchronized( m_computers )
{ {
for( IComputerAccess computer : m_computers ) for( IComputerAccess computer : m_computers )
{ {
@ -84,7 +84,7 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
{ {
if( packet.getSender() == this || !m_state.isOpen( packet.getChannel() ) ) return; if( packet.getSender() == this || !m_state.isOpen( packet.getChannel() ) ) return;
synchronized( this ) synchronized( m_computers )
{ {
for( IComputerAccess computer : m_computers ) for( IComputerAccess computer : m_computers )
{ {
@ -201,21 +201,33 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
@Override @Override
public synchronized void attach( @Nonnull IComputerAccess computer ) public synchronized void attach( @Nonnull IComputerAccess computer )
{
synchronized( m_computers )
{ {
m_computers.add( computer ); m_computers.add( computer );
}
setNetwork( getNetwork() ); setNetwork( getNetwork() );
} }
@Override @Override
public synchronized void detach( @Nonnull IComputerAccess computer ) public synchronized void detach( @Nonnull IComputerAccess computer )
{
boolean empty;
synchronized( m_computers )
{ {
m_computers.remove( computer ); m_computers.remove( computer );
if( m_computers.isEmpty() ) setNetwork( null ); empty = m_computers.isEmpty();
}
if( empty ) setNetwork( null );
} }
@Nonnull @Nonnull
@Override @Override
public synchronized String getSenderID() public String getSenderID()
{
synchronized( m_computers )
{ {
if( m_computers.size() != 1 ) if( m_computers.size() != 1 )
{ {
@ -228,3 +240,4 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
} }
} }
} }
}

View File

@ -21,6 +21,12 @@ public abstract class WirelessModemPeripheral extends ModemPeripheral
m_advanced = advanced; m_advanced = advanced;
} }
@Deprecated
public WirelessModemPeripheral( boolean advanced )
{
this( new ModemState(), advanced );
}
@Override @Override
public boolean isInterdimensional() public boolean isInterdimensional()
{ {

View File

@ -13,8 +13,9 @@ import dan200.computercraft.api.network.IPacketSender;
import dan200.computercraft.api.network.Packet; import dan200.computercraft.api.network.Packet;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.HashSet; import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class WirelessNetwork implements IPacketNetwork public class WirelessNetwork implements IPacketNetwork
{ {
@ -34,45 +35,34 @@ public class WirelessNetwork implements IPacketNetwork
s_universalNetwork = null; s_universalNetwork = null;
} }
private final Set<IPacketReceiver> m_receivers; private final Set<IPacketReceiver> m_receivers = Collections.newSetFromMap( new ConcurrentHashMap<>() );
private WirelessNetwork()
{
m_receivers = new HashSet<>();
}
@Override @Override
public synchronized void addReceiver( @Nonnull IPacketReceiver receiver ) public void addReceiver( @Nonnull IPacketReceiver receiver )
{ {
Preconditions.checkNotNull( receiver, "device cannot be null" ); Preconditions.checkNotNull( receiver, "device cannot be null" );
m_receivers.add( receiver ); m_receivers.add( receiver );
} }
@Override @Override
public synchronized void removeReceiver( @Nonnull IPacketReceiver receiver ) public void removeReceiver( @Nonnull IPacketReceiver receiver )
{ {
Preconditions.checkNotNull( receiver, "device cannot be null" ); Preconditions.checkNotNull( receiver, "device cannot be null" );
m_receivers.remove( receiver ); m_receivers.remove( receiver );
} }
@Override @Override
public synchronized void transmitSameDimension( @Nonnull Packet packet, double range ) public void transmitSameDimension( @Nonnull Packet packet, double range )
{ {
Preconditions.checkNotNull( packet, "packet cannot be null" ); Preconditions.checkNotNull( packet, "packet cannot be null" );
for( IPacketReceiver device : m_receivers ) for( IPacketReceiver device : m_receivers ) tryTransmit( device, packet, range, false );
{
tryTransmit( device, packet, range, false );
}
} }
@Override @Override
public synchronized void transmitInterdimensional( @Nonnull Packet packet ) public void transmitInterdimensional( @Nonnull Packet packet )
{ {
Preconditions.checkNotNull( packet, "packet cannot be null" ); Preconditions.checkNotNull( packet, "packet cannot be null" );
for (IPacketReceiver device : m_receivers) for( IPacketReceiver device : m_receivers ) tryTransmit( device, packet, 0, true );
{
tryTransmit( device, packet, 0, true );
}
} }
private void tryTransmit( IPacketReceiver receiver, Packet packet, double range, boolean interdimensional ) private void tryTransmit( IPacketReceiver receiver, Packet packet, double range, boolean interdimensional )