Debian Codegraph @

lib/control/future.scm

raw
1 ;;;
2 ;;; control.future - future implementation
3 ;;;
4 ;;; Copyright (c) 2020-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; This is a provisional implementation; we'll use implicit thread pool
35 ;; to avoid thread creation overhead eventually.
36
37 ;; Guile and Racket uses 'touch' to retrive the result of a future, but
38 ;; that name seems too generic. We adopt 'future-get'.
39
40 ;; If a future already finished computation, 'future-get' returns immediately
41 ;; with the result value. Otherwise, it blocks until the result is available,
42 ;; unless timeout is specified. Subsequent 'future-get' returns the same
43 ;; result.
44 ;;
45 ;; If the concurrent computation raises an exception, it is caught, and
46 ;; re-raised when first 'future-get'. It is undefined if future-get is
47 ;; called again on such a future---it depends on the behavior of thread-join!,
48 ;; but the behavior of calling thread-join! again on exceptionally terminated
49 ;; thread isn't defined either. Currently, the second call of future-get
50 ;; won't raise an exception and returns #<undef>, but do not count on
51 ;; the behavior.
52
53 (define-module control.future
54 (use gauche.threads)
55 (export <future> future? future make-future future-done? future-get))
56 (select-module control.future)
57
58 (define-class <future> ()
59 ;; all slots must be private
60 ((%thread :init-keyword :thread)))
61
62 (define-syntax future
63 (syntax-rules ()
64 [(_ expr)
65 (make <future>
66 :thread (thread-start! (make-thread (lambda () (values->list expr)))))]))
67
68 (define (make-future thunk)
69 (future (thunk)))
70
71 (define (future? obj) (is-a? obj <future>))
72
73 (define (future-done? future)
74 (assume-type future <future>)
75 (eq? (thread-state (~ future'%thread)) 'terminated))
76
77 (define (future-get future :optional (timeout #f) (timeout-val #f))
78 (assume-type future <future>)
79 (guard (e [(<uncaught-exception> e) (raise (~ e'reason))])
80 (apply values (thread-join! (~ future'%thread) timeout timeout-val))))