`CQueue`: rework implementation
NicolasT opened this issue · comments
The current implementation of CQueue
is quite complicated. Reworking this, simplifying it, potentially making it fully STM-only, could be useful.
See: #7
Note, making this STM
-only, is potentially not a good idea, though we likely do want to make "most of it" run in STM, but potentially multiple transactions.
The rationale: a receive
should not impact any send
, to the max extent possible. If we run the whole receive
inside a single transaction, then either it will succeed but any send
s will need to be restarted, or a receive
transaction would be interrupted by any send
and need to retry. All this could lead to some livelock'y situations.
The way this could be implemented (though to be validated/discussed/...) is by running a first transaction in receive
which empties the message queue and puts all messages in some other TVar
value (akin to what the current CQueue
implementation does). Since there are no guarantees about handling of messages which arrive during a call to receive
, from that point on, send
s can put messages in the TQueue
again without being impacted by, or impacting, the receive
. In a subsequent transaction, messages stored in this TVar
can be consumed by receive
(as well as any other STM values): no send
will be impacted, nor will any send
impact this second transaction.
It's likely a good idea to model the implementation using DejaFu or IOSim and have tests in place to validate the properties we expect the implementation to adhere to. See also #17.