mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-09 08:50:29 +00:00
e9aceca1de
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. |
||
---|---|---|
.. | ||
common | ||
common-api | ||
core | ||
core-api | ||
fabric | ||
fabric-api | ||
forge | ||
forge-api | ||
lints | ||
standalone | ||
web | ||
ARCHITECTURE.md |