chickadee » synch

synch

Simple critical region helpers.

TOC »

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

Usage

(import synch)

Synch wraps the user code with some form of isolation. The implementation forms are split into 4 modules:

synch-open
no attempted isolation
synch-dyn
dynamic-wind
synch-exn
handle-exceptions
synch-dynexn
both

The default module exports synch-open, synch-params, & synch-dyn.

Parameters

Used by synch-dyn, synch-exn, & synch-dynexn.

Usage

(import synch-params)

current-synch-exit-condition

current-synch-exit-condition #!optional OBJparameter

Object to designate premature exit of synchronized body due to exit continuation.

Default is 'synch-exit.

current-synch-raise

current-synch-raise #!optional PROC/1parameter

Procedure of one argument to call when premature exit of synchronized body due to exception.

Default is synch-raise-warning.

current-synch-abandon?

current-synch-abandon? #!optional BOOLparameter

Premature exit of synchronized body leaves mutex abandoned?

Default is #f.

Conceptually how closely bound is the synchronizing mutex to what is being synchronized; is the mutex state part of the error state? The presumption is not, but there are other scenarios.

synch-raise-warning

current-synch-raise OBJprocedure

Uses warning to announce receipt of OBJ, but does not raise the exception.

Synchronized Invocation (exit continuation aware)

Some protection. Assumes no exception raised by the synchronized expression.

Usage

(import synch-dyn)

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

Executes BODY ... while MUTEX locked, and leaves mutex locked, assuming the BODY ... does not invoke an exit continuation.

Returns the result of BODY ....

synch-unlock

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

Executes BODY ... while MUTEX locked, and leaves mutex unlocked.

Returns the result of BODY ....

Should the mutex be unlocked an error is issued.

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

<syntax>(object-synch-cut-with MUTEX-FORM [BODY ...]) -> *<-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-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 an error is issued.

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)) ) )

synchronized-procedure

synchronized-procedure PROCprocedure

Returns a synchronized version of PROC

Synchronized Invocation (exception aware)

More protection, but expensive. Provides the same API as synch-dyn).

Usage

(import synch-exn)

synch

synch-with

call-synch

call-synch-with

apply-synch

apply-synch-with

synch-lock

synch-unlock

let-synch-with

set!-synch-with

object-synch-cut-with

record-synch

record-synch-lock

record-synch-unlock

synch-with-object?

define-constructor-synch

define-predicate-synch

define-operation-synch

synchronized-procedure

Synchronized Invocation (exit continuation & exception aware)

More protection, but expensive. Provides the same API as synch-dyn).

Usage

(import synch-dynexn)

synch

synch-with

call-synch

call-synch-with

apply-synch

apply-synch-with

synch-lock

synch-unlock

let-synch-with

set!-synch-with

object-synch-cut-with

record-synch

record-synch-lock

record-synch-unlock

synch-with-object?

define-constructor-synch

define-predicate-synch

define-operation-synch

synchronized-procedure

Synchronized Invocation (exit continuation unaware)

No protection. Assumes no exit continuation called or exception raised by the synchronized expression.

Usage

Also available as:

(import synch-open)

%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

define-operation-%synch

(define-operation-%synch OPERNAME)syntax

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

Bug: part of Object Synchronization & useless without the rest, available only thru one of the other synch-* modules.

Critical Region

Usage

(import critical-region)

make-exchanger

make-exchanger OBJprocedure

Returns a new exchanger with the argument OBJ as its initial content.

An exchanger is a procedure of one argument regulating mutually exclusive access to a resource. When a exchanger is called, its current content is returned, while being replaced by its argument in an atomic operation.

From SRFI-96 Mutual Exclusion; see https://srfi.schemers.org/s/srfi-96.html.

Bugs & Limitations

Notes

Author

Kon Lovett

Requirements

srfi-18 check-errors

Version history

3.3.0
Add make-exchanger.
3.2.2
Fix synch-open identifier generation.
3.2.1
Fix syntax variable references.
3.2.0
Fix lock failure handling. Add modules synch-dyn, synch-exn, synch-dynexn, synch-open.
3.1.0
3.0.0
CHICKEN 5 release.
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-2020 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 »