ocaml-multicore / kcas

Software Transactional Memory for OCaml
https://ocaml-multicore.github.io/kcas/
ISC License
109 stars 11 forks source link

Nested transactions #70

Closed polytypic closed 1 year ago

polytypic commented 1 year ago

This PR adds low level support for nested (conditional) transactions.

The idea is that a snapshot of a transaction log can be taken and later the transaction log can be rolled back to discard any changes made after the snapshot was taken. This allows transactions to record tentative changes to locations and then later perform a rollback. This also allows transaction implementations to scope such rollbacks as desired.

Some other STM implementations make such rollbacks implicitly. The reasoning for not making rollbacks implicit is based on the following:

  1. In many cases rollback is unnecessary. For example, Queue.Xt.take_blocking does not logically mutate the queue in case it signals retry and that applies to many similar operations.
  2. In some cases rollback is not only unnecessary, but might also be undesirable as a transaction might record benign changes that "move things forward" without changing the logical state of abstractions. In other words, the scoping of ideal rollback depends on the internals of transactions.
  3. Performing a rollback is potentially linear time O(n) in the number of accessed locations and relatively expensive.

The disadvantage of not making rollbacks implicit is, of course, that it potentially makes the programming model more difficult.