1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 11:10: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
{
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;
protected ModemPeripheral( ModemState state )
@ -58,7 +58,7 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
setNetwork( getNetwork() );
}
public synchronized void destroy()
public void destroy()
{
setNetwork( null );
}
@ -68,7 +68,7 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
{
if( packet.getSender() == this || !m_state.isOpen( packet.getChannel() ) ) return;
synchronized( this )
synchronized( 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;
synchronized( this )
synchronized( m_computers )
{
for( IComputerAccess computer : m_computers )
{
@ -202,29 +202,42 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
@Override
public synchronized void attach( @Nonnull IComputerAccess computer )
{
m_computers.add( computer );
synchronized( m_computers )
{
m_computers.add( computer );
}
setNetwork( getNetwork() );
}
@Override
public synchronized void detach( @Nonnull IComputerAccess computer )
{
m_computers.remove( computer );
if( m_computers.isEmpty() ) setNetwork( null );
boolean empty;
synchronized( m_computers )
{
m_computers.remove( computer );
empty = m_computers.isEmpty();
}
if( empty ) setNetwork( null );
}
@Nonnull
@Override
public synchronized String getSenderID()
public String getSenderID()
{
if( m_computers.size() != 1 )
synchronized( m_computers )
{
return "unknown";
}
else
{
IComputerAccess computer = m_computers.iterator().next();
return computer.getID() + "_" + computer.getAttachmentName();
if( m_computers.size() != 1 )
{
return "unknown";
}
else
{
IComputerAccess computer = m_computers.iterator().next();
return computer.getID() + "_" + computer.getAttachmentName();
}
}
}
}

View File

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

View File

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