1 ;;;; Copyright (C) 1996, 1998, 2001, 2002, 2003, 2006 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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)
51 ;;; Macros first, so that the procedures expand correctly.
53 (define-syntax begin-thread
60 (define-syntax parallel
64 (with-syntax (((tmp0 ...) (generate-temporaries (syntax (e0 ...)))))
66 (let ((tmp0 (begin-thread e0))
68 (values (join-thread tmp0) ...))))))))
72 ((_ ((v e) ...) b0 b1 ...)
74 (lambda () (parallel e ...))
78 (define-syntax make-thread
82 (lambda () (proc arg ...))
85 (define-syntax with-mutex
90 (lambda () (lock-mutex x))
91 (lambda () (begin e0 e1 ...))
92 (lambda () (unlock-mutex x)))))))
94 (define-syntax monitor
97 (with-mutex (make-mutex)
100 (define (par-mapper mapper)
101 (lambda (proc . arglists)
105 (begin-thread (apply proc args)))
108 (define par-map (par-mapper map))
109 (define par-for-each (par-mapper for-each))
111 (define (n-par-map n proc . arglists)
112 (let* ((m (make-mutex))
114 (results (make-list (length (car arglists))))
118 (for-each join-thread threads)
126 (let ((args (map car arglists))
128 (set! arglists (map cdr arglists))
129 (set! result (cdr result))
131 (set-car! my-result (apply proc args))
135 (define (n-par-for-each n proc . arglists)
136 (let ((m (make-mutex))
140 (for-each join-thread threads))
145 (if (null? (car arglists))
147 (let ((args (map car arglists)))
148 (set! arglists (map cdr arglists))
154 ;;; The following procedure is motivated by the common and important
155 ;;; case where a lot of work should be done, (not too much) in parallel,
156 ;;; but the results need to be handled serially (for example when
157 ;;; writing them to a file).
159 (define (n-for-each-par-map n s-proc p-proc . arglists)
160 "Using N parallel processes, apply S-PROC in serial order on the results
161 of applying P-PROC on ARGLISTS."
162 (let* ((m (make-mutex))
164 (no-result '(no-value))
165 (results (make-list (length (car arglists)) no-result))
169 (for-each join-thread threads))
174 (cond ((null? results)
176 ((not (eq? (car results) no-result))
177 (let ((arg (car results)))
178 ;; stop others from choosing to process results
179 (set-car! results no-result)
183 (set! results (cdr results))
189 (let ((args (map car arglists))
191 (set! arglists (map cdr arglists))
192 (set! result (cdr result))
194 (set-car! my-result (apply p-proc args))
198 (define (thread-handler tag . args)
199 (fluid-set! the-last-stack #f)
200 (let ((n (length args))
201 (p (current-error-port)))
202 (display "In thread:" p)
214 (display "uncaught throw to " p)
221 ;;; Set system thread handler
222 (define %thread-handler thread-handler)
224 ;;; threads.scm ends here