You are looking at historical revision 33001 of this page. It may differ significantly from its current revision.

hopefully

Simple composable transactional memory.

The API is similar to Clojure and STMX. However (currently) limited.

Overview

This egg provides two modules: hopefully and hopefully-current.

Module hopefully contains bindings recommended to use. These transactions are roughly as fast as normal locking (ca. 10% faster than locking).

Module hopefully-current adds support for the concept of an implicit current transaction, which is nice but and order of magnitude more expensive at runtime.

Modules

hopefully

[syntax] define-a-record

Like define-record, defines procedures according to the following pattern.

[procedure] (make-TYPE FIELDS ...) -> AREC
[procedure] (TYPE-FIELD-ref AREC TNX) => REF

Return a reference for use with CELL-REF (alias "@") and ALTER!.

Note: Try to use this accessor just once per object+field. Multiple calls (within a lightweight transaction) will produce independent references. (If these become inconsistent, the commit will fail nevertheless.) Only those references used to (set! (@ REF) val) will return the in-transaction value.

[procedure] (TYPE-FIELD AREC)

Return the FIELD value visible without transactions in effect.

[procedure] (TYPE-tag AREC)

Purely for debugging. Mayb e removed. Return the internal version tag of the slot.

[procedure] (cell-ref REF)

Retrieve the in-transaction value from the REFerence (and add it to the transactions dependencies).

[procedure] (@ REF)

Alias to cell-ref. With generalized setter.

[procedure] (alter! REF val)

Alter a REFerence (produced by the type-field-ref accessors to hold the new value val. This also adds the cell to the dependencies of the transaction associated with the REF.

[procedure] (call-with-transaction PROC)

Call PROC with one argument, a fresh (lightweight) transaction. PROC may be called multiple times in case of a conflict. (See hopefully-current for the difference to heavy transactions).

Returns whatever PROC returns.

Note: One should not pass the transaction argument around among threads or capture it. Most (if not all) resulting conditions should be handled. But it is no good idea.

hopefully-current

This module introduces the concept of a default current-transaction and heavy transactions.

When a reference is added to a heavy transaction, the transactions dependencies are searched and if a reference to the same object+slot is found, it is returned.

[syntax] define-ac-record

Like define-a-record.

define-ac-record is provided for maximum compatibility with define-record. Just changing the record definition should make code aware of the current transaction.

[procedure] (make-TYPE FIELDS..) -> ACREC

Accessors:

[procedure] (TYPE-FIELD-ref ACREC TNX) => REF

Return a reference to the in-transaction value of field in ACREC for use with CELL-REF (alias "@") and ALTER!. See sibling definition in hopefully.

[procedure] (TYPE-FIELD ACREC)

Return the value of field in ACREC. Returns the in-transaction value with respect to the current-transaction or the outside value if there is no current-transaction in the current-thread.

Note: this is roughly an order of magnitude slower than the corresponding accessor from define-a-record

[procedure] (TYPE-FIELD-set! ACREC val) -> undefined

Set the value of field to val. Changes the in-transaction value respect to the current-transaction or the outside value if there is no current-transaction in the current-thread.

Note: this is roughly an order of magnitude slower than the corresponding accessor from define-a-record

[procedure] (with-current-transaction THUNK)

Establish a new current-transaction and call THUNK. After thunk completed, commit the current transaction. If that failes, THUNK is called again.

Returns whatever THUNK returns.

Examples

(define-a-record gbox v)
(define b1 (make-gbox 0))
(define b2 (make-gbox 1))

(call-with-transaction
 (lambda (tnx)
   (let ((x (gbox-v-ref b1 tnx))
         (i (gbox-v-ref b2 tnx)))
     (let ((ni (@ i)))
       (alter! x (+ (@ x) ni))
       (alter! i (add1 ni))))))

See also tests/run.scm.

About this egg

Source

Authors

Jörg F. Wittenberger