chickadee » synch

synch

Simple critical region helpers.

Documentation

Critical Regions

Where MUTEXFORM below the following forms are accepted:

MUTEX
just a mutex
(MUTEX [(LOCK-ARG ...) [(UNLOCK-ARG ...)]])
mutex with optional lock and unlock arguments

Synchronized Invocation - Continuation Safe

These forms have the mutex lock/unlock wrapped in dynamic-wind.

synch
(synch MUTEXFORM [BODY ...]) => OBJECT syntax

Execute BODY ... while MUTEX locked.

Returns the result of BODY ....

synch-with
(synch-with MUTEXFORM VARIABLE [BODY ...]) => OBJECT syntax

Execute BODY ... while MUTEX locked and the mutex-specific of MUTEX bound to VARIABLE.

Returns the result of BODY ....

call/synch
(call/synch MUTEXFORM PROCEDURE [ARGUMENTS ...]) => OBJECT syntax

Invoke PROCEDURE on the argument list ARGUMENTS ... while MUTEX locked.

Returns the result of the PROCEDURE invocation.

call-with/synch
(call-with/synch MUTEXFORM PROCEDURE [ARGUMENTS ...]) => OBJECT syntax

Invoke PROCEDURE on the mutex-specific of MUTEX and the ARGUMENTS ... while MUTEX locked.

Returns the result of the PROCEDURE invocation.

apply/synch
(apply/synch MUTEXFORM PROCEDURE [ARGUMENTS ...]) => OBJECT syntax

Apply PROCEDURE to the argument list ARGUMENTS ... while MUTEX locked.

Returns the result of the PROCEDURE application.

apply-with/synch
(apply-with/synch MUTEXFORM PROCEDURE [ARGUMENTS ...]) => OBJECT syntax

Apply PROCEDURE to the mutex-specific of MUTEX) and the ARGUMENTS ... while MUTEX locked.

Returns the result of the PROCEDURE application.

synch/lock
(synch/lock MUTEXFORM [BODY ...]) => OBJECT syntax

Execute BODY ... while MUTEX locked, and leave mutex locked.

Returns the result of BODY ....

synch/unlock
(synch/unlock MUTEXFORM [BODY ...]) => OBJECT syntax

Execute BODY ... while MUTEX locked, and leave mutex unlocked.

Returns the result of BODY ....

Should the mutex be unlocked a warning is issued and the mutex is locked before executing the BODY.

let/synch
(let/synch BINDINGS [BODY ...]) => OBJECT syntax

BINDINGS is a list of (VARIABLE MUTEXFORM).

Expands into a nested (synch-with MUTEXFORM VARIABLE ... form, a synch-with for each binding pair in BINDINGS. The leftmost binding pair is the outermost.

Returns the result of BODY ....

set!/synch
(set!/synch (VARIABLE MUTEXFORM) [BODY ...]) => OBJECT syntax

While the MUTEX is locked, evaluates BODY ... with the VARIABLE bound to the mutex-specific of MUTEX. Sets the mutex-specific of MUTEX to the result of the evaluation.

Returns the new mutex-specific of MUTEX.

object/synch
(object/synch MUTEXFORM [BODY ...]) => OBJECT syntax

Execute BODY ... while MUTEX locked.

The top-level forms of BODY ... are parsed for occurrences of ><. All such occurrences are replaced by the mutex-specific of MUTEX.

Returns the result of BODY ....

record/synch
(record/synch RECORD-SYMBOL RECORD-OBJECT [BODY ...]) => OBJECT syntax

Execute BODY ... while the RECORD-OBJECT mutex is locked. The mutex is a field of the record named RECORD-SYMBOL-mutex.

Returns the result of BODY ....

(define-record-type foo
  (make-foo a b mtx)
  foo?
  (a foo-a)
  (b foo-b)
  (mtx foo-mutex) )

(define f1 (make-foo 1 2 (make-mutex 'foo)))

(record/synch foo f1 (+ (foo-a f1) (foo-b f1)))
record-synch/lock
(record-synch/lock RECORD-SYMBOL RECORD-OBJECT [BODY ...]) => OBJECT syntax

Execute BODY ... while the RECORD-OBJECT mutex is locked, and leave the mutex locked.

Returns the result of BODY ....

RECORD-SYMBOL and RECORD-OBJECT are per record/synch.

record-synch/unlock
(record-synch/unlock RECORD-SYMBOL RECORD-OBJECT [BODY ...]) => OBJECT syntax

Execute BODY ... while the RECORD-OBJECT mutex is locked, and leave the mutex unlocked.

Returns the result of BODY ....

RECORD-SYMBOL and RECORD-OBJECT are per record/synch.

Should the mutex be unlocked a warning is issued and the mutex is locked before executing the BODY.

Synchronized Invocation - Continuation Unsafe

These forms do not have the mutex lock/unlock wrapped in dynamic-wind, otherwise the same behavior.

%call/synch
(%call/synch MUTEXFORM PROCEDURE [ARGUMENTS ...]) => OBJECT syntax
%call-with/synch
(%call-with/synch MUTEXFORM PROCEDURE [ARGUMENTS ...]) => OBJECT syntax
%apply/synch
(%apply/synch MUTEXFORM PROCEDURE [ARGUMENTS ...]) => OBJECT syntax
%apply-with/synch
(%apply-with/synch MUTEXFORM PROCEDURE [ARGUMENTS ...]) => OBJECT syntax
%synch
(%synch MUTEXFORM [BODY ...]) => OBJECT syntax
%synch-with
(%synch-with MUTEXFORM VARIABLE [BODY ...]) => OBJECT syntax
%synch/lock
(%synch/lock MUTEXFORM [BODY ...]) => OBJECT syntax
%synch/unlock
(%synch/unlock MUTEXFORM [BODY ...]) => OBJECT syntax
%let/synch
(%let/synch BINDINGS [BODY ...]) => OBJECT syntax
%set!/synch
(%set!/synch (VARIABLE MUTEXFORM) [BODY ...]) => OBJECT syntax
%object/synch
(%object/synch MUTEXFORM [BODY ...]) => OBJECT syntax
%record/synch
(%record/synch RECORD-SYMBOL RECORD-OBJECT [BODY ...]) => OBJECT syntax
%record-synch/lock
(%record-synch/lock RECORD-SYMBOL RECORD-OBJECT [BODY ...]) => OBJECT syntax
%record-synch/unlock
(%record-synch/unlock RECORD-SYMBOL RECORD-OBJECT [BODY ...]) => OBJECT syntax

Object Synchronization

make-object/synch

(make-object/synch OBJECT [NAME]) => MUTEX procedure

Returns a mutex with a mutex-specific value of OBJECT, and optional mutex NAME.

NAME is either a symbol or a one element list of symbol. When a list the first element is used as the basis for a generated symbol. When a symbol it is used literally as the mutex name.

When NAME is missing a generated symbol with the prefix synchobj is provided.

object?/synch

(object?/synch OBJECT [PREDICATE]) => BOOLEAN procedure

Is the OBJECT a synchronized object - a mutex with a non-void mutex specific?

The optional PREDICATE is used to verify the type of the mutex-specific binding. Otherwise any object is accepted.

define-constructor/synch

(define-constructor/synch CTORNAME [ID]) syntax
(define-constructor/synch make-hash-table hash-table/synch:)
;=> similar
(define (make-hash-table/sync . args)
  (make-object/synch (apply make-hash-table args) '(hash-table/synch:)) )

define-predicate/synch

(define-predicate/synch PREDNAME) syntax
(define-predicate/synch hash-table?)
;=> similar
(define (hash-table?/sync obj) (object?/synch obj hash-table?))

define-operation/synch

(define-operation/synch OPERNAME) syntax

Note that the operand must be the first argument of OPERNAME.

(define-operation/synch hash-table-set!)
;=> similar
(define (hash-table-set!/sync mtx+obj . args)
  (let ((mtx (if (pair? mtx+obj) (car mtx+obj) mtx+obj)))
    (check-mutex+object 'hash-table-set!/synch mtx 'object/synch)
    (synch-with mtx+obj obj (apply hash-table-set! obj args)) ) )

Usage

(require-extension synch)

or

(require-library synch)
...
(import synch)

Examples

Notes

Inspired by Thomas Chust.

Bugs and Limitations

Author

Kon Lovett

Requirements

Unit srfi-18 check-errors

Version history

2.1.2
Ensure all local vars in operation macros hygenic.
2.1.1
Fix %record/synch body expansion. Fix make-object/synch default name.
2.1.0
Support for mutex lock/unlock arguments. Operation wrapper macros.
2.0.0
Port to hygienic Chicken. Removed 'set-object!/synch'.

License

Copyright (C) 2009 Kon Lovett. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contents »