chickadee » synch

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

synch

Simple critical region helpers.

Documentation

Where MUTEX-FORM below the following forms are accepted:

MUTEX-OBJECT
mutex
(MUTEX-OBJECT [(LOCK-ARG...) [(UNLOCK-ARG...)]])
mutex w/ optional lock and unlock arguments

Synchronized Invocation - Continuation Safe

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

synch

(synch MUTEX-FORM [BODY ...]) -> *syntax

Execute BODY ... while MUTEX locked.

Returns the result of BODY ....

synch-with

(synch-with MUTEX-FORM VARIABLE [BODY ...]) -> *syntax

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

Returns the result of BODY ....

call-synch

(call-synch MUTEX-FORM PROCEDURE [ARGUMENTS ...]) -> *syntax

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

Returns the result of the PROCEDURE invocation.

call-synch-with

(call-synch-with MUTEX-FORM PROCEDURE [ARGUMENTS ...]) -> *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 MUTEX-FORM PROCEDURE [ARGUMENTS ...]) -> *syntax

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

Returns the result of the PROCEDURE application.

apply-synch-with

(apply-synch-with MUTEX-FORM PROCEDURE [ARGUMENTS ...]) -> *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 MUTEX-FORM [BODY ...]) -> *syntax

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

Returns the result of BODY ....

synch-unlock

(synch-unlock MUTEX-FORM [BODY ...]) -> *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-with

(let-synch-with BINDINGS [BODY ...]) -> *syntax

BINDINGS is a list of (VARIABLE MUTEX-FORM).

Expands into a nested (synch-with MUTEX-FORM 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-with

(set!-synch-with MUTEX-FORM VARIABLE [BODY ...]) -> *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-cut-with

<macro>(object-synch-cut-with MUTEX-FORM [BODY ...]) -> *<-macro>

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-OBJECT RECORD-SYMBOL [BODY ...]) -> *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-OBJECT RECORD-SYMBOL [BODY ...]) -> *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-OBJECT RECORD-SYMBOL [BODY ...]) -> *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 MUTEX-FORM PROCEDURE [ARGUMENTS ...]) -> *syntax

%call-synch-with

(%call-synch-with MUTEX-FORM PROCEDURE [ARGUMENTS ...]) -> *syntax

%apply-synch

(%apply-synch MUTEX-FORM PROCEDURE [ARGUMENTS ...]) -> *syntax

%apply-synch-with

(%apply-synch-with MUTEX-FORM PROCEDURE [ARGUMENTS ...]) -> *syntax

%synch

(%synch MUTEX-FORM [BODY ...]) -> *syntax

%synch-with

(%synch-with MUTEX-FORM VARIABLE [BODY ...]) -> *syntax

%synch-lock

(%synch-lock MUTEX-FORM [BODY ...]) -> *syntax

%synch-unlock

(%synch-unlock MUTEX-FORM [BODY ...]) -> *syntax

%let-synch

(%let-synch BINDINGS [BODY ...]) -> *syntax

%set!-synch-with

(%set!-synch-with MUTEX-FORM VARIABLE [BODY ...]) -> *syntax

%object-synch-cut-with

(%object-synch-cut-with MUTEX-FORM [BODY ...]) -> *syntax

%record-synch

(%record-synch RECORD-OBJECT RECORD-SYMBOL [BODY ...]) -> *syntax

%record-synch-lock

(%record-synch-lock RECORD-OBJECT RECORD-SYMBOL [BODY ...]) -> *syntax

%record-synch-unlock

(%record-synch-unlock RECORD-OBJECT RECORD-SYMBOL [BODY ...]) -> *syntax

Object Synchronization

make-synch-with-object

make-synch-with-object OBJECT #!optional NAMEprocedure

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.

synch-with-object?

synch-with-object? OBJECT #!optional PREDICATEprocedure

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-synch-with-object (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) (synch-with-object? 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)) ) )

define-operation-%synch

(define-operation-%synch OPERNAME)syntax

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

synchronized-procedure

synchronized-procedure PROCprocedure

Returns a synchronized version of PROC

Usage

(require-extension synch)

or

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

Notes

Bugs & Limitations

Author

Kon Lovett

Requirements

Unit srfi-18 check-errors

setup-helper

Version history

2.4.1
Fix lock failure handling.
2.4.0
2.3.0
Add critical-region, synchronized-procedure, record-synch, record-synch-lock, record-synch-unlock, call-synch, call-synch-with, apply-synch, apply-synch-with, let-synch-with, set!-synch-with, synch-lock, synch-unlock, object-synch-cut-with, make-synch-with-object, synch-with-object?, define-constructor-synch, define-predicate-synch, define-operation-synch.
2.2.2
2.2.1
Do not build format-synch.
2.2.0
Deprecate ../synch bindings. Add 'format-synch.scm'.
2.1.4
.
2.1.3
.
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-2018 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 »