1 ;;;; threads.test --- Tests for Guile threading. -*- scheme -*-
3 ;;;; Copyright 2003, 2006 Free Software Foundation, Inc.
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;;;; Boston, MA 02110-1301 USA
20 (use-modules (ice-9 threads)
23 (if (provided? 'threads)
26 (with-test-prefix "parallel"
62 (with-test-prefix "n-par-for-each"
64 (pass-if "0 in limit 10"
65 (n-par-for-each 10 noop '())
68 (pass-if "6 in limit 10"
69 (let ((v (make-vector 6 #f)))
70 (n-par-for-each 10 (lambda (n)
73 (equal? v '#(#t #t #t #t #t #t))))
75 (pass-if "6 in limit 1"
76 (let ((v (make-vector 6 #f)))
77 (n-par-for-each 1 (lambda (n)
80 (equal? v '#(#t #t #t #t #t #t))))
82 (pass-if "6 in limit 2"
83 (let ((v (make-vector 6 #f)))
84 (n-par-for-each 2 (lambda (n)
87 (equal? v '#(#t #t #t #t #t #t))))
89 (pass-if "6 in limit 3"
90 (let ((v (make-vector 6 #f)))
91 (n-par-for-each 3 (lambda (n)
94 (equal? v '#(#t #t #t #t #t #t)))))
100 (with-test-prefix "n-for-each-par-map"
102 (pass-if "0 in limit 10"
103 (n-for-each-par-map 10 noop noop '())
106 (pass-if "6 in limit 10"
108 (n-for-each-par-map 10
109 (lambda (n) (set! result (cons n result)))
112 (equal? result '(10 8 6 4 2 0))))
114 (pass-if "6 in limit 1"
116 (n-for-each-par-map 1
117 (lambda (n) (set! result (cons n result)))
120 (equal? result '(10 8 6 4 2 0))))
122 (pass-if "6 in limit 2"
124 (n-for-each-par-map 2
125 (lambda (n) (set! result (cons n result)))
128 (equal? result '(10 8 6 4 2 0))))
130 (pass-if "6 in limit 3"
132 (n-for-each-par-map 3
133 (lambda (n) (set! result (cons n result)))
136 (equal? result '(10 8 6 4 2 0)))))
142 (with-test-prefix "joining"
144 ;; scm_join_thread has a SCM_TICK in the middle of it, to
145 ;; allow asyncs to run (including signal delivery). We used
146 ;; to have a bug whereby if the joined thread terminated at
147 ;; the same time as the joining thread is in this SCM_TICK,
148 ;; scm_join_thread would not notice and would hang forever.
149 ;; So in this test we are setting up the following sequence of
151 ;; T=0 other thread is created and starts running
152 ;; T=2 main thread sets up an async that will sleep for 10 seconds
153 ;; T=2 main thread calls join-thread, which will...
154 ;; T=2 ...call the async, which starts sleeping
155 ;; T=5 other thread finishes its work and terminates
156 ;; T=7 async completes, main thread continues inside join-thread.
157 (pass-if "don't hang when joined thread terminates in SCM_TICK"
158 (let ((other-thread (make-thread sleep 5)))
159 (letrec ((delay-count 10)
161 (set! delay-count (- delay-count 1))
162 (if (zero? delay-count)
164 (system-async-mark aproc)))))
166 (system-async-mark aproc)
167 (join-thread other-thread)))