1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-09 08:50:29 +00:00
CC-Tweaked/projects
Jonathan Coates e9aceca1de
Add a helper class for working with attached computers
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.
2024-09-22 13:51:11 +01:00
..
common Add a helper class for working with attached computers 2024-09-22 13:51:11 +01:00
common-api Fix several repeated words 2024-08-17 11:39:14 +01:00
core Bump CC:T to 1.113.1 2024-09-11 18:27:13 +01:00
core-api Add a helper class for working with attached computers 2024-09-22 13:51:11 +01:00
fabric Use ARGB32 to store palette colours 2024-09-11 10:13:39 +01:00
fabric-api Rewrite turtle upgrade modeller registration API 2024-01-16 23:00:49 +00:00
forge Shadow netty rather than jar-in-jaring 2024-09-11 18:00:12 +01:00
forge-api Rewrite turtle upgrade modeller registration API 2024-01-16 23:00:49 +00:00
lints Fix disk drives not setting/clearing removed flag 2024-08-15 09:03:33 +01:00
standalone Small bits of cleanup 2024-06-26 18:07:57 +01:00
web Publish docs via an artifact instead 2024-06-19 09:57:56 +01:00
ARCHITECTURE.md Add a standalone CC:T UI 2023-10-28 17:58:11 +01:00