Wiki
Download
Manual
Eggs
API
Tests
Bugs
show
edit
history
You can edit this page using
wiki syntax
for markup.
Article contents:
[[tags: egg]] == 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 <enscript language=scheme> (import synch) </enscript> 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 <enscript language=scheme> (import synch-params) </enscript> ==== current-synch-exit-condition <parameter>(current-synch-exit-condition [OBJ])</parameter> 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])</parameter> 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])</parameter> 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)</procedure> 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 <enscript language=scheme> (import synch-dyn) </enscript> ==== synch <syntax>(synch MUTEX-FORM [BODY ...]) -> * ...</syntax> Execute {{BODY ...}} while {{MUTEX}} locked. Returns the result of {{BODY ...}}. ==== synch-with <syntax>(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 <syntax>(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 <syntax>(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 <syntax>(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 <syntax>(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 <syntax>(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 <syntax>(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 <syntax>(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 <syntax>(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 <syntax>(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 ...}}. <enscript language=scheme> (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))) </enscript> ==== record-synch-lock <syntax>(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 <syntax>(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 <procedure>(make-synch-with-object 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. ==== synch-with-object? <procedure>(synch-with-object? OBJECT [PREDICATE]) -> bool</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 <syntax>(define-constructor-synch CTORNAME [ID])</syntax> <enscript language=scheme> (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)) ) </enscript> ==== define-predicate-synch <syntax>(define-predicate-synch PREDNAME)</syntax> <enscript language=scheme> (define-predicate-synch hash-table?) ;=> similar (define (hash-table?-sync obj) (synch-with-object? obj hash-table?)) </enscript> ==== define-operation-synch <syntax>(define-operation-synch OPERNAME)</syntax> Note that the operand must be the first argument of {{OPERNAME}}. <enscript language=scheme> (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)) ) ) </enscript> ==== synchronized-procedure <procedure>(synchronized-procedure PROC) -> procedure</procedure> Returns a synchronized version of {{PROC}} === Synchronized Invocation (exception aware) More protection, but expensive. Provides the same API as {{synch-dyn)}}. ==== Usage <enscript language=scheme> (import synch-exn) </enscript> ==== 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 <enscript language=scheme> (import synch-dynexn) </enscript> ==== 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 <enscript language=scheme> (import synch-open) </enscript> ==== %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 <enscript language=scheme> (import critical-region) </enscript> ==== make-exchanger <procedure>(make-exchanger OBJ) -> *</procedure> 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 * The isolation is easily broken. See {{tests/synch-continuation-dyn-test.scm}} in the egg source for poor interaction with {{SRFI 154}} & {{SRFI 155}}. * {{define-*-synch}} is obtuse. {{define-*-%synch}} is worse. == Notes * Inspired by Thomas Chust. * Thanks to Jörg Wittenberger. == Author [[/users/kon-lovett|Kon Lovett]] == Repository This egg is hosted on the CHICKEN Subversion repository: [[https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/synch|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 [[/egg-svn-checkout|this page]]. == Requirements [[srfi-18]] [[check-errors]] [[test]] [[srfi-69]] == Version history ; 3.3.9 : Remove failing test since not instructive. ; 3.3.8 : . ; 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-2024 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.
Description of your changes:
I would like to authenticate
Authentication
Username:
Password:
Spam control
What do you get when you multiply 3 by 1?