Skip to content

Commit

Permalink
Add a helper class for working with attached computers
Browse files Browse the repository at this point in the history
One of the easiest things to mess up with writing a custom peripheral is
handling attached peripherals. IPeripheral.{attach,detach} are called
from multiple threads, so naive implementations that just store
computers in a set/list will at some point throw a CME.

Historically I've suggested using a concurrent collection (i.e.
ConcurrentHashMap). While this solves the problems of CMEs, it still has
some flaws. If a computer is detached while iterating over the
collection, the iterator will still yield the now-detached peripheral,
causing usages of that computer (e.g. queueEvent) to throw an exception.

The only fix here is to use a lock when updating and iterating over the
collection. This does come with some risks, but I think they are not too
serious:

 - Lock contention: Contention is relatively rare in general (as
   peripheral attach/detach is not especially frequent). If we do see
   contention, both iteration and update actions are cheap, so I would
   not expect the other thread to be blocked for a significant time.

 - Deadlocks: One could imagine an implementation if IComputerAccess
   that holds a lock both when detaching a peripheral and inside
   queueEvent.

   If we queue an event on one thread, and try to detach on the other,
   we could see a deadlock:

     Thread 1                         | Thread 2
    ----------------------------------------------------------
     AttachedComputerSet.queueEvent   | MyModem.detach
      (take lock #1)                  |  (take lock #2)

     -> MyModem.queueEvent            | AttachedComputerSet.remove
      (wait on lock #2)               |  (wait on lock #1)

   Such code would have been broken already (some peripherals already
   use locks), so I'm fairly sure we've fixed this in CC. But definitely
   something to watch out for.

Anyway, the long and short of it:
 - Add a new AttachedComputerSet that can be used to track the computers
   attached to a peripheral. We also mention this in the attach/detach
   docs, to hopefully make it a little more obvoius.

 - Update speakers and monitors to use this new class.
  • Loading branch information
SquidDev committed Sep 22, 2024
1 parent 3042950 commit e9aceca
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.google.common.annotations.VisibleForTesting;
import dan200.computercraft.annotations.ForgeOverride;
import dan200.computercraft.api.peripheral.AttachedComputerSet;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.terminal.Terminal;
Expand All @@ -25,9 +26,6 @@
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public class MonitorBlockEntity extends BlockEntity {
Expand All @@ -53,7 +51,7 @@ public class MonitorBlockEntity extends BlockEntity {
private @Nullable ClientMonitor clientMonitor;

private @Nullable MonitorPeripheral peripheral;
private final Set<IComputerAccess> computers = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final AttachedComputerSet computers = new AttachedComputerSet();

private boolean needsUpdate = false;
private boolean needsValidating = false;
Expand Down Expand Up @@ -487,7 +485,7 @@ private void eachComputer(Consumer<IComputerAccess> fun) {
var monitor = getLoadedMonitor(x, y).getMonitor();
if (monitor == null) continue;

for (var computer : monitor.computers) fun.accept(computer);
computers.forEach(fun);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

package dan200.computercraft.shared.peripheral.speaker;

import com.google.errorprone.annotations.concurrent.GuardedBy;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.LuaTable;
import dan200.computercraft.api.peripheral.AttachedComputerSet;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.util.Nullability;
Expand All @@ -33,7 +33,10 @@
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;

import javax.annotation.Nullable;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static dan200.computercraft.api.lua.LuaValues.checkFinite;

Expand All @@ -60,7 +63,7 @@ public abstract class SpeakerPeripheral implements IPeripheral {
public static final int SAMPLE_RATE = 48000;

private final UUID source = UUID.randomUUID();
private final @GuardedBy("computers") Set<IComputerAccess> computers = new HashSet<>();
private final AttachedComputerSet computers = new AttachedComputerSet();

private long clock = 0;
private long lastPositionTime;
Expand Down Expand Up @@ -140,11 +143,7 @@ public void update() {
syncedPosition(position);

// And notify computers that we have space for more audio.
synchronized (computers) {
for (var computer : computers) {
computer.queueEvent("speaker_audio_empty", computer.getAttachmentName());
}
}
computers.forEach(c -> c.queueEvent("speaker_audio_empty", c.getAttachmentName()));
}

// Push position updates to any speakers which have ever played a note,
Expand Down Expand Up @@ -353,16 +352,12 @@ private void syncedPosition(SpeakerPosition position) {

@Override
public void attach(IComputerAccess computer) {
synchronized (computers) {
computers.add(computer);
}
computers.add(computer);
}

@Override
public void detach(IComputerAccess computer) {
synchronized (computers) {
computers.remove(computer);
}
computers.remove(computer);
}

static double clampVolume(double volume) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// SPDX-FileCopyrightText: 2024 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0

package dan200.computercraft.api.peripheral;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;

/**
* A thread-safe collection of computers.
* <p>
* This collection is intended to be used by peripherals that need to maintain a set of all attached computers.
* <p>
* It is recommended to use over Java's built-in concurrent collections (e.g. {@link CopyOnWriteArraySet} or
* {@link ConcurrentHashMap}), as {@link AttachedComputerSet} ensures that computers cannot be accessed after they are
* detached, guaranteeing that {@link NotAttachedException}s will not be thrown.
* <p>
* To ensure this, {@link AttachedComputerSet} is not directly iterable, as we cannot ensure that computers are not
* detached while the iterator is running (and so trying to use the computer would error). Instead, computers should be
* looped over using {@link #forEach(Consumer)}.
*
* <h2>Example</h2>
*
* <pre>{@code
* public class MyPeripheral implements IPeripheral {
* private final AttachedComputerSet computers = new ComputerCollection();
*
* @Override
* public void attach(IComputerAccess computer) {
* computers.add(computer);
* }
*
* @Override
* public void detach(IComputerAccess computer) {
* computers.remove(computer);
* }
* }
* }</pre>
*
* @see IComputerAccess
* @see IPeripheral#attach(IComputerAccess)
* @see IPeripheral#detach(IComputerAccess)
*/
public final class AttachedComputerSet {
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final Set<IComputerAccess> computers = new HashSet<>(0);

/**
* Add a computer to this collection of computers. This should be called from
* {@link IPeripheral#attach(IComputerAccess)}.
*
* @param computer The computer to add.
*/
public void add(IComputerAccess computer) {
lock.writeLock().lock();
try {
computers.add(computer);
} finally {
lock.writeLock().unlock();
}
}

/**
* Remove a computer from this collection of computers. This should be called from
* {@link IPeripheral#detach(IComputerAccess)}.
*
* @param computer The computer to remove.
*/
public void remove(IComputerAccess computer) {
lock.writeLock().lock();
try {
computers.remove(computer);
} finally {
lock.writeLock().unlock();
}
}

/**
* Apply an action to each computer in this collection.
*
* @param action The action to apply.
*/
public void forEach(Consumer<? super IComputerAccess> action) {
lock.readLock().lock();
try {
computers.forEach(action);
} finally {
lock.readLock().unlock();
}
}

/**
* {@linkplain IComputerAccess#queueEvent(String, Object...) Queue an event} on all computers.
*
* @param event The name of the event to queue.
* @param arguments The arguments for this event.
* @see IComputerAccess#queueEvent(String, Object...)
*/
public void queueEvent(String event, @Nullable Object... arguments) {
forEach(c -> c.queueEvent(event, arguments));
}

/**
* Determine if this collection contains any computers.
* <p>
* This method is primarily intended for presentation purposes (such as rendering an icon in the UI if a computer
* is attached to your peripheral). Due to the multi-threaded nature of peripherals, it is not recommended to guard
* any logic behind this check.
* <p>
* For instance, {@code if(computers.hasComputers()) computers.queueEvent("foo");} contains a race condition, as
* there's no guarantee that any computers are still attached within the body of the if statement.
*
* @return Whether this collection is non-empty.
*/
public boolean hasComputers() {
lock.readLock().lock();
try {
return !computers.isEmpty();
} finally {
lock.readLock().unlock();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ default Set<String> getAdditionalTypes() {
* {@code peripheral.call()}. This method can be used to keep track of which computers are attached to the
* peripheral, or to take action when attachment occurs.
* <p>
* Be aware that will be called from both the server thread and ComputerCraft Lua thread, and so must be thread-safe
* and reentrant.
* Be aware that may be called from both the server thread and ComputerCraft Lua thread, and so must be thread-safe
* and reentrant. If you need to store a list of attached computers, it is recommended you use a
* {@link AttachedComputerSet}.
*
* @param computer The interface to the computer that is being attached. Remember that multiple computers can be
* attached to a peripheral at once.
Expand All @@ -68,8 +69,9 @@ default void attach(IComputerAccess computer) {
* This method can be used to keep track of which computers are attached to the peripheral, or to take action when
* detachment occurs.
* <p>
* Be aware that this will be called from both the server and ComputerCraft Lua thread, and must be thread-safe
* and reentrant.
* Be aware that this may be called from both the server and ComputerCraft Lua thread, and must be thread-safe
* and reentrant. If you need to store a list of attached computers, it is recommended you use a
* {@link AttachedComputerSet}.
*
* @param computer The interface to the computer that is being detached. Remember that multiple computers can be
* attached to a peripheral at once.
Expand Down

0 comments on commit e9aceca

Please sign in to comment.