~ chicken-core (chicken-5) /continuation.scm
Trap1;;;; continuation.scm - A better API for continuations
2;
3; Copyright (c) 2008-2022, The CHICKEN Team
4; Copyright (c) 2007, Felix L. Winkelmann
5; All rights reserved.
6;
7; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
8; conditions are met:
9;
10; Redistributions of source code must retain the above copyright notice, this list of conditions and the following
11; disclaimer.
12; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
13; disclaimer in the documentation and/or other materials provided with the distribution.
14; Neither the name of the author nor the names of its contributors may be used to endorse or promote
15; products derived from this software without specific prior written permission.
16;
17; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
18; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
20; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25; POSSIBILITY OF SUCH DAMAGE.
26
27(declare
28 (unit continuation)
29 (disable-interrupts))
30
31(foreign-declare "#define C_direct_continuation(dummy) t1")
32
33(module chicken.continuation
34 (continuation?
35 continuation-capture
36 continuation-graft
37 continuation-return)
38
39(import scheme chicken.base chicken.fixnum)
40
41(include "common-declarations.scm")
42
43(define (continuation-capture proc)
44 (let ((winds ##sys#dynamic-winds)
45 (k (##core#inline "C_direct_continuation" #f)))
46 (proc (##sys#make-structure 'continuation k winds))))
47
48(define (continuation? x)
49 (##sys#structure? x 'continuation))
50
51(define (continuation-graft k thunk)
52 (##sys#check-structure k 'continuation 'continuation-graft)
53 (let ([winds (##sys#slot k 2)])
54 (unless (eq? ##sys#dynamic-winds winds)
55 (##sys#dynamic-unwind winds (fx- (length ##sys#dynamic-winds) (length winds))))
56 ((##core#primitive "C_continuation_graft") k thunk)))
57
58(define continuation-return
59 (lambda (k . vals)
60 (##sys#check-structure k 'continuation 'continuation-return)
61 ((##core#primitive "C_continuation_graft") k (lambda () (apply values vals))))))