Debian Codegraph @

test/include/srfi-230-tests.scm

raw
1 ;; SRFI-230 tests
2 ;;
3 ;; Gauche: gosh -Ilib srfi-230-tests.scm
4 ;; Chibi: chibi-scheme -I lib srfi-230-tests.scm
5 ;;
6
7 (import (scheme base)
8 (scheme list)
9 (srfi 64)
10 (srfi 230))
11
12 (cond-expand
13 ((library (srfi 18)) (import (srfi 18)))
14 (else))
15
16 (cond-expand
17 ((library (srfi 210)) (import (only (srfi 210) list/mv)))
18 (else
19 ;; not a complete replacement, but this suffices for our purpose.
20 (define-syntax list/mv
21 (syntax-rules ()
22 ((_ mv-expr) (call-with-values (lambda () mv-expr) list))))))
23
24 ;; To test collision situations, this needs to be not too small,
25 ;; though too many threads may slow down the tests.
26 (define *num-test-threads* 500)
27
28 (test-begin "SRFI 230 Atomic operations")
29
30 ;;;
31 ;;; Memory order
32 ;;;;
33
34 (test-group "Memory order"
35 (test-assert (memory-order? (memory-order relaxed)))
36 (test-assert (memory-order? (memory-order acquire)))
37 (test-assert (memory-order? (memory-order release)))
38 (test-assert (memory-order? (memory-order acquire-release)))
39 (test-assert (memory-order? (memory-order sequentially-consistent)))
40 (test-eqv #f (memory-order? 'invalid-memory-order))
41 )
42
43 ;;;
44 ;;; Atomic flags
45 ;;;
46
47 (test-group "Atomic flags"
48 (let ()
49 (define f (make-atomic-flag))
50 (test-assert (atomic-flag? f))
51 (test-eqv #f (atomic-flag-test-and-set! f))
52 (test-eqv #t (atomic-flag-test-and-set! f))
53 (atomic-flag-clear! f)
54 (test-eqv #f (atomic-flag-test-and-set! f))
55 )
56
57 (cond-expand
58 ((library (srfi 18))
59 (let ()
60 (define flag (make-atomic-flag))
61 (define counter 0)
62 (define (increment! delta)
63 (cond ((atomic-flag-test-and-set! flag)
64 ;; somebody's working
65 (thread-sleep! 0.000001)
66 (increment! delta)) ; retry
67 (else ; we got lock
68 (set! counter (+ counter delta))
69 (atomic-flag-clear! flag)
70 (atomic-fence (memory-order sequentially-consistent)))))
71 (define threads (map (lambda (x)
72 (make-thread
73 (lambda ()
74 (thread-sleep! 0.000001)
75 (increment! x))))
76 (iota *num-test-threads*)))
77 (for-each thread-start! threads)
78 (for-each thread-join! threads)
79 (test-eqv (apply + (iota *num-test-threads*)) counter)
80 ))
81 (else))
82 )
83
84 ;;;
85 ;;; Atomic box
86 ;;;
87
88 (test-group "Atomic box"
89 (let ()
90 (define b (make-atomic-box 'foo))
91 (test-assert (atomic-box? b))
92 (test-eq 'foo (atomic-box-ref b))
93 (atomic-box-set! b 'bar)
94 (test-eq 'bar (atomic-box-ref b))
95 (test-eq 'bar (atomic-box-swap! b 'baz))
96 (test-eq 'baz (atomic-box-ref b))
97 (test-eq 'baz (atomic-box-compare-and-swap! b 'foo 'boo))
98 (test-eq 'baz (atomic-box-ref b))
99 (test-eq 'baz (atomic-box-compare-and-swap! b 'baz 'boo))
100 (test-eq 'boo (atomic-box-ref b))
101 )
102
103 (cond-expand
104 ((library (srfi 18))
105 (let ()
106 (define box (make-atomic-box #f))
107 (define counter 0)
108 (define (increment! delta)
109 (cond ((atomic-box-compare-and-swap! box #f #t)
110 ;; somebody's working
111 (thread-sleep! 0.000001)
112 (increment! delta)) ; retry
113 (else ; we got lock
114 (set! counter (+ counter delta))
115 (atomic-box-set! box #f)
116 (atomic-fence (memory-order sequentially-consistent)))))
117 (define threads (map (lambda (x) (make-thread
118 (lambda ()
119 (thread-sleep! 0.000001)
120 (increment! x))))
121 (iota *num-test-threads*)))
122 (for-each thread-start! threads)
123 (for-each thread-join! threads)
124 (test-eqv (apply + (iota *num-test-threads*)) counter)
125 ))
126 (else))
127 )
128
129 ;;;
130 ;;; Atomic fxbox
131 ;;;
132
133 (test-group "Atomic fbox"
134 (let ()
135 (define x (make-atomic-fxbox 0))
136 (test-assert (atomic-fxbox? x))
137 (test-eqv 0 (atomic-fxbox-ref x))
138 (atomic-fxbox-set! x 1)
139 (test-eqv 1 (atomic-fxbox-ref x))
140 (test-eqv 1 (atomic-fxbox-swap! x 3))
141 (test-eqv 3 (atomic-fxbox-ref x))
142 (test-eqv 3 (atomic-fxbox-compare-and-swap! x 1 5))
143 (test-eqv 3 (atomic-fxbox-ref x))
144 (test-eqv 3 (atomic-fxbox-compare-and-swap! x 3 5))
145 (test-eqv 5 (atomic-fxbox-ref x))
146
147 (test-eqv 5 (atomic-fxbox+/fetch! x 3))
148 (test-eqv 8 (atomic-fxbox-ref x))
149 (test-eqv 8 (atomic-fxbox-/fetch! x 1))
150 (test-eqv 7 (atomic-fxbox-ref x))
151
152 (test-eqv 7 (atomic-fxbox-and/fetch! x #x55))
153 (test-eqv 5 (atomic-fxbox-ref x))
154 (test-eqv 5 (atomic-fxbox-ior/fetch! x #xaa))
155 (test-eqv #xaf (atomic-fxbox-ref x))
156 (test-eqv #xaf (atomic-fxbox-xor/fetch! x #x15))
157 (test-eqv #xba (atomic-fxbox-ref x))
158 )
159
160 (cond-expand
161 ((library (srfi 18))
162 (let ()
163 (define counter (make-atomic-fxbox 0))
164 (define (increment! delta)
165 (atomic-fxbox-/fetch! counter delta
166 (memory-order sequentially-consistent)))
167 (define threads (map (lambda (x) (make-thread
168 (lambda ()
169 (thread-sleep! 0.000001)
170 (increment! x))))
171 (iota *num-test-threads*)))
172 (for-each thread-start! threads)
173 (for-each thread-join! threads)
174 (test-eqv (- (apply + (iota *num-test-threads*)))
175 (atomic-fxbox-ref counter))
176 ))
177 (else))
178 )
179
180 ;;;
181 ;;; Atomic pair
182 ;;;
183
184 (test-group "Atomic pair"
185 (let ()
186 (define x (make-atomic-pair 'p 'q))
187 (test-assert (atomic-pair? x))
188 (test-equal '(p q) (list/mv (atomic-pair-ref x)))
189 (atomic-pair-set! x 'r 's)
190 (test-equal '(r s) (list/mv (atomic-pair-ref x)))
191 (test-equal '(r s) (list/mv (atomic-pair-swap! x 't 'u)))
192 (test-equal '(t u) (list/mv (atomic-pair-ref x)))
193 (test-equal '(t u) (list/mv (atomic-pair-compare-and-swap! x 'p 'q 'v 'w)))
194 (test-equal '(t u) (list/mv (atomic-pair-ref x)))
195 (test-equal '(t u) (list/mv (atomic-pair-compare-and-swap! x 't 'q 'v 'w)))
196 (test-equal '(t u) (list/mv (atomic-pair-ref x)))
197 (test-equal '(t u) (list/mv (atomic-pair-compare-and-swap! x 'p 'u 'v 'w)))
198 (test-equal '(t u) (list/mv (atomic-pair-ref x)))
199 (test-equal '(t u) (list/mv (atomic-pair-compare-and-swap! x 't 'u 'v 'w)))
200 (test-equal '(v w) (list/mv (atomic-pair-ref x)))
201 )
202
203 (cond-expand
204 ((library (srfi 18))
205 (let ()
206 (define accumulator (make-atomic-pair 0 0))
207 (define (accumulate! delta)
208 (define (try curr-sum curr-val)
209 (let-values (((prev-sum prev-val)
210 (atomic-pair-compare-and-swap! accumulator
211 curr-sum curr-val
212 (+ curr-sum curr-val) delta)))
213 (unless (and (eq? prev-sum curr-sum)
214 (eq? prev-val curr-val))
215 (try prev-sum prev-val))))
216 (call-with-values (lambda () (atomic-pair-ref accumulator)) try))
217 (define threads (map (lambda (x) (make-thread
218 (lambda ()
219 (thread-sleep! 0.001)
220 (accumulate! x))))
221 (iota *num-test-threads*)))
222 (for-each thread-start! threads)
223 (for-each thread-join! threads)
224 (test-eqv (apply + (iota *num-test-threads*))
225 (apply + (list/mv (atomic-pair-ref accumulator))))
226 ))
227 (else))
228 )
229
230 (test-end "SRFI 230 Atomic operations")