1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-28 16:00:22 +02:00

Replaced 'futures' with threads.

This commit is contained in:
Marius Vollmer 2006-01-29 19:44:45 +00:00
parent 99e370f645
commit a64d058951
4 changed files with 35 additions and 29 deletions

View file

@ -1,3 +1,7 @@
2006-01-29 Marius Vollmer <mvo@zagadka.de>
* threads.scm: Replaced 'futures' with threads.
2006-01-13 Neil Jerram <neil@ossau.uklinux.net>
* boot-9.scm (repl-reader): Use value of current-reader fluid to

View file

@ -37,22 +37,20 @@
n-par-map
n-par-for-each
n-for-each-par-map)
:re-export (future-ref)
:export-syntax (begin-thread
parallel
letpar
make-thread
with-mutex
monitor)
:re-export-syntax (future))
monitor))
(define ((par-mapper mapper) proc . arglists)
(mapper future-ref
(mapper thread-join
(apply map
(lambda args
(future (apply proc args)))
(begin-thread (apply proc args)))
arglists)))
(define par-map (par-mapper map))
@ -60,15 +58,15 @@
(define (n-par-map n proc . arglists)
(let* ((m (make-mutex))
(futures '())
(threads '())
(results (make-list (length (car arglists))))
(result results))
(do ((i 0 (+ 1 i)))
((= i n)
(for-each future-ref futures)
(for-each thread-join threads)
results)
(set! futures
(cons (future
(set! threads
(cons (begin-thread
(let loop ()
(lock-mutex m)
(if (null? result)
@ -80,16 +78,16 @@
(unlock-mutex m)
(set-car! my-result (apply proc args))
(loop)))))
futures)))))
threads)))))
(define (n-par-for-each n proc . arglists)
(let ((m (make-mutex))
(futures '()))
(threads '()))
(do ((i 0 (+ 1 i)))
((= i n)
(for-each future-ref futures))
(set! futures
(cons (future
(for-each thread-join futures))
(set! threads
(cons (begin-thread
(let loop ()
(lock-mutex m)
(if (null? (car arglists))
@ -99,7 +97,7 @@
(unlock-mutex m)
(apply proc args)
(loop)))))
futures)))))
threads)))))
;;; The following procedure is motivated by the common and important
;;; case where a lot of work should be done, (not too much) in parallel,
@ -110,15 +108,15 @@
"Using N parallel processes, apply S-PROC in serial order on the results
of applying P-PROC on ARGLISTS."
(let* ((m (make-mutex))
(futures '())
(threads '())
(no-result '(no-value))
(results (make-list (length (car arglists)) no-result))
(result results))
(do ((i 0 (+ 1 i)))
((= i n)
(for-each future-ref futures))
(set! futures
(cons (future
(for-each thread-join futures))
(set! threads
(cons (begin-thread
(let loop ()
(lock-mutex m)
(cond ((null? results)
@ -143,7 +141,7 @@ of applying P-PROC on ARGLISTS."
(unlock-mutex m)
(set-car! my-result (apply p-proc args))
(loop))))))
futures)))))
threads)))))
(define (thread-handler tag . args)
(fluid-set! the-last-stack #f)
@ -169,7 +167,7 @@ of applying P-PROC on ARGLISTS."
#f))
;;; Set system thread handler
(set! %thread-handler thread-handler)
(define %thread-handler thread-handler)
; --- MACROS -------------------------------------------------------
@ -189,8 +187,8 @@ of applying P-PROC on ARGLISTS."
(make-symbol "f"))
forms)))
`((lambda ,vars
(values ,@(map (lambda (v) `(future-ref ,v)) vars)))
,@(map (lambda (form) `(future ,form)) forms))))))
(values ,@(map (lambda (v) `(thread-join ,v)) vars)))
,@(map (lambda (form) `(begin-thread ,form)) forms))))))
(define-macro (letpar bindings . body)
(cond ((or (null? bindings) (null? (cdr bindings)))
@ -199,8 +197,8 @@ of applying P-PROC on ARGLISTS."
(let ((vars (map car bindings)))
`((lambda ,vars
((lambda ,vars ,@body)
,@(map (lambda (v) `(future-ref ,v)) vars)))
,@(map (lambda (b) `(future ,(cadr b))) bindings))))))
,@(map (lambda (v) `(thread-join ,v)) vars)))
,@(map (lambda (b) `(begin-thread ,(cadr b))) bindings))))))
(define-macro (make-thread proc . args)
`(call-with-new-thread

View file

@ -1,3 +1,7 @@
2006-01-29 Marius Vollmer <mvo@zagadka.de>
* tests/time.test: Replaced 'futures' with threads.
2005-11-30 Kevin Ryde <user42@zip.com.au>
* tests/srfi-13.test (string-append/shared): New tests.

View file

@ -40,9 +40,9 @@
(alarm 5)
(false-if-exception (gmtime t))
(future-ref (future (catch 'out-of-range
(lambda () (gmtime t))
(lambda args #f))))
(thread-join (begin-thread (catch 'out-of-range
(lambda () (gmtime t))
(lambda args #f))))
(alarm 0)
#t))
@ -118,6 +118,6 @@
(alarm 5)
(false-if-exception
(strptime "%a" "nosuchday"))
(future-ref (future (strptime "%d" "1")))
(thread-join (begin-thread (strptime "%d" "1")))
(alarm 0)
#t))