synch

Simple critical region helpers.

  1. synch
  2. Documentation
    1. Usage
    2. Parameters
      1. Usage
      2. current-synch-exit-condition
      3. current-synch-raise
      4. current-synch-abandon?
      5. synch-raise-warning
    3. Synchronized Invocation (exit continuation aware)
      1. Usage
      2. synch
      3. synch-with
      4. call-synch
      5. call-synch-with
      6. apply-synch
      7. apply-synch-with
      8. synch-lock
      9. synch-unlock
      10. let-synch-with
      11. set!-synch-with
      12. object-synch-cut-with
      13. record-synch
      14. record-synch-lock
      15. record-synch-unlock
    4. Object Synchronization
      1. make-synch-with-object
      2. synch-with-object?
      3. define-constructor-synch
      4. define-predicate-synch
      5. define-operation-synch
      6. synchronized-procedure
    5. Synchronized Invocation (exception aware)
      1. Usage
      2. synch
      3. synch-with
      4. call-synch
      5. call-synch-with
      6. apply-synch
      7. apply-synch-with
      8. synch-lock
      9. synch-unlock
      10. let-synch-with
      11. set!-synch-with
      12. object-synch-cut-with
      13. record-synch
      14. record-synch-lock
      15. record-synch-unlock
      16. synch-with-object?
      17. define-constructor-synch
      18. define-predicate-synch
      19. define-operation-synch
      20. synchronized-procedure
    6. Synchronized Invocation (exit continuation & exception aware)
      1. Usage
      2. synch
      3. synch-with
      4. call-synch
      5. call-synch-with
      6. apply-synch
      7. apply-synch-with
      8. synch-lock
      9. synch-unlock
      10. let-synch-with
      11. set!-synch-with
      12. object-synch-cut-with
      13. record-synch
      14. record-synch-lock
      15. record-synch-unlock
      16. synch-with-object?
      17. define-constructor-synch
      18. define-predicate-synch
      19. define-operation-synch
      20. synchronized-procedure
    7. Synchronized Invocation (exit continuation unaware)
      1. Usage
      2. %synch
      3. %synch-with
      4. %call-synch
      5. %call-synch-with
      6. %apply-synch
      7. %apply-synch-with
      8. %synch-lock
      9. %synch-unlock
      10. %let-synch-with
      11. %set!-synch-with
      12. %object-synch-cut-with
      13. %record-synch
      14. %record-synch-lock
      15. %record-synch-unlock
      16. define-constructor-%synch
      17. define-predicate-%synch
      18. define-operation-%synch
      19. %synchronized-procedure
    8. Critical Region
      1. Usage
      2. make-exchanger
  3. Bugs & Limitations
  4. Notes
  5. Author
  6. Repository
  7. Requirements
  8. Version history
  9. License

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

[parameter] (current-synch-exit-condition [OBJ])

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

Default is 'synch-exit.

current-synch-raise

[parameter] (current-synch-raise [PROC/1])

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

Default is synch-raise-warning.

current-synch-abandon?

[parameter] (current-synch-abandon? [BOOL])

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

[procedure] (current-synch-raise OBJ)

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

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

Execute BODY ... while MUTEX locked.

Returns the result of BODY ....

synch-with

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

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

Returns the result of BODY ....

call-synch

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

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

Returns the result of the PROCEDURE invocation.

call-synch-with

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

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

Returns the result of the PROCEDURE invocation.

apply-synch

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

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

Returns the result of the PROCEDURE application.

apply-synch-with

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

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

Returns the result of the PROCEDURE application.

synch-lock

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

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

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

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

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

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

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

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 ...]) -> * ...

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

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

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

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

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

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

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

[procedure] (make-synch-with-object OBJECT [NAME]) -> mutex

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?

[procedure] (synch-with-object? OBJECT [PREDICATE]) -> bool

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

[syntax] (define-constructor-synch CTORNAME [ID])
(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

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

define-operation-synch

[syntax] (define-operation-synch OPERNAME)

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

[procedure] (synchronized-procedure PROC) -> procedure

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

(import synch-open)

%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

define-constructor-%synch

define-predicate-%synch

define-operation-%synch

%synchronized-procedure

Critical Region

Usage

(import critical-region)

make-exchanger

[procedure] (make-exchanger OBJ) -> *

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

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/synch

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Requirements

srfi-18 check-errors

test srfi-69

Version history

3.3.7
Fix multiple values.
3.3.6
.
3.3.5
Add %synchronized-procedure, define-constructor-%synch, define-predicate-%synch.
3.3.4
.
3.3.3
.
3.3.2
.
3.3.1
.
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-2021 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.