[cl-stm-devel] Higher order transactions

Hoan Ton-That hoan at ton-that.org
Sat Aug 19 12:31:51 UTC 2006


Its now possible to write transactions that take
transactions as parameters.  Here are two short
examples.  The first one is `nob' which takes a
transaction and turns it into a non-blocking one.
It returns T on success or NIL on failure.

(deftransaction nob ((tx transaction))
  (try (progn tx t) nil))

And with it we can define lots and lots of new
operators, like `try-put'.

(deftransaction try-put ((cell cell) value)
  (nob (put cell value)))

STM> (defvar *cell* (new 'cell))
*CELL*
STM> (perform (try-put *cell* 'hello) :wait? t)
T
STM> (perform (try-put *cell* 'hello) :wait? t)
NIL

In the source code, there is a better version of
`nob' which returns T and the values of the
transaction.

The next example is `repeat' which executes
a transaction n times.

(deftransaction repeat ((tx transaction) n)
  (if (= n 1)
      tx
      (progn tx (repeat tx (- n 1)))))

STM> (defvar *counter* (new 'counter :count 1))
*COUNTER*
STM> (perform (repeat (increment *counter* 3) 3) :wait? t)
10

Pretty cool huh?

Hoan



More information about the Cl-stm-devel mailing list