1 ;;;; Copyright (C) 1996, 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 ;;;; This library is free software; you can redistribute it and/or
4 ;;;; modify it under the terms of the GNU Lesser General Public
5 ;;;; License as published by the Free Software Foundation; either
6 ;;;; version 2.1 of the License, or (at your option) any later version.
8 ;;;; This library is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;;; Lesser General Public License for more details.
13 ;;;; You should have received a copy of the GNU Lesser General Public
14 ;;;; License along with this library; if not, write to the Free Software
15 ;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 ;;;; ----------------------------------------------------------------
18 ;;;; threads.scm -- User-level interface to Guile's thread system
19 ;;;; 4 March 1996, Anthony Green <green@cygnus.com>
20 ;;;; Modified 5 October 1996, MDJ <djurfeldt@nada.kth.se>
21 ;;;; Modified 6 April 2001, ttn
22 ;;;; ----------------------------------------------------------------
27 ;; This module is documented in the Guile Reference Manual.
28 ;; Briefly, one procedure is exported: `%thread-handler';
29 ;; as well as four macros: `make-thread', `begin-thread',
30 ;; `with-mutex' and `monitor'.
34 (define-module (ice-9 threads)
39 :re-export (future-ref)
40 :export-syntax (begin-thread
46 :re-export-syntax (future))
50 (define ((par-mapper mapper) proc . arglists)
54 (future (apply proc args)))
57 (define par-map (par-mapper map))
58 (define par-for-each (par-mapper for-each))
60 (define (n-par-map n proc . arglists)
61 (let* ((m (make-mutex))
63 (results (make-list (length (car arglists))))
67 (for-each future-ref futures)
75 (let ((args (map car arglists))
77 (set! arglists (map cdr arglists))
78 (set! result (cdr result))
80 (set-car! my-result (apply proc args))
84 (define (n-par-for-each n proc . arglists)
85 (let ((m (make-mutex))
89 (for-each future-ref futures))
94 (if (null? (car arglists))
96 (let ((args (map car arglists)))
97 (set! arglists (map cdr arglists))
103 (define (thread-handler tag . args)
104 (fluid-set! the-last-stack #f)
105 (let ((n (length args))
106 (p (current-error-port)))
107 (display "In thread:" p)
119 (display "uncaught throw to " p)
126 ;;; Set system thread handler
127 (set! %thread-handler thread-handler)
129 ; --- MACROS -------------------------------------------------------
131 (define-macro (begin-thread . forms)
134 `(call-with-new-thread
139 (define-macro (parallel . forms)
140 (cond ((null? forms) '(begin))
141 ((null? (cdr forms)) (car forms))
143 (let ((vars (map (lambda (f)
147 (values ,@(map (lambda (v) `(future-ref ,v)) vars)))
148 ,@(map (lambda (form) `(future ,form)) forms))))))
150 (define-macro (letpar bindings . body)
151 (cond ((or (null? bindings) (null? (cdr bindings)))
152 `(let ,bindings ,@body))
154 (let ((vars (map car bindings)))
156 ((lambda ,vars ,@body)
157 ,@(map (lambda (v) `(future-ref ,v)) vars)))
158 ,@(map (lambda (b) `(future ,(cadr b))) bindings))))))
160 (define-macro (make-thread proc . args)
161 `(call-with-new-thread
166 (define-macro (with-mutex m . body)
168 (lambda () (lock-mutex ,m))
169 (lambda () (begin ,@body))
170 (lambda () (unlock-mutex ,m))))
172 (define-macro (monitor first . rest)
173 `(with-mutex ,(make-mutex)
177 ;;; threads.scm ends here