synch

Simple critical region helpers.

  1. synch
  2. Documentation
    1. Synchronized Invocation - Continuation Safe
      1. synch
      2. synch-with
      3. call-synch
      4. call-synch-with
      5. apply-synch
      6. apply-synch-with
      7. synch-lock
      8. synch-unlock
      9. let-synch-with
      10. set!-synch-with
      11. object-synch-cut-with
      12. record-synch
      13. record-synch-lock
      14. record-synch-unlock
    2. Synchronized Invocation - Continuation Unsafe
      1. %call-synch
      2. %call-synch-with
      3. %apply-synch
      4. %apply-synch-with
      5. %synch
      6. %synch-with
      7. %synch-lock
      8. %synch-unlock
      9. %let-synch
      10. %set!-synch-with
      11. %object-synch-cut-with
      12. %record-synch
      13. %record-synch-lock
      14. %record-synch-unlock
    3. Object Synchronization
      1. make-synch-with-object
      2. synch-with-object?
      3. define-constructor-synch
      4. define-predicate-synch
      5. define-operation-synch
      6. define-operation-%synch
      7. synchronized-procedure
    4. Critical Regions
      1. Usage
      2. critical-region
      3. critical-region-apply
  3. Usage
  4. Notes
  5. Bugs & Limitations
  6. Author
  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

Synchronized Invocation - Continuation Safe

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

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

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

Returns the result of BODY ....

synch-unlock

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

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

[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

<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

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

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

%call-synch-with

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

%apply-synch

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

%apply-synch-with

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

%synch

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

%synch-with

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

%synch-lock

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

%synch-unlock

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

%let-synch

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

%set!-synch-with

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

%object-synch-cut-with

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

%record-synch

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

%record-synch-lock

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

%record-synch-unlock

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

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

define-operation-%synch

[syntax] (define-operation-%synch OPERNAME)

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

synchronized-procedure

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

Returns a synchronized version of PROC

Critical Regions

Usage

(require-extension critical-region)

critical-region

[syntax] (critical-region EXPR ...)

Executes EXPR ... with (disable-interrupts).

critical-region-apply

[procedure] (critical-region-apply PROC [ARG ...]) -> *

Executes (apply PROC [ARG ...]) with (disable-interrupts).

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