2025-01-13 12:22:27 +00:00
|
|
|
(use-modules (tests)
|
2025-11-19 13:18:39 +00:00
|
|
|
(ice-9 atomic)
|
2025-01-13 12:22:27 +00:00
|
|
|
(srfi srfi-71)
|
|
|
|
|
(fibers)
|
|
|
|
|
(unit-test)
|
2025-02-27 12:08:41 +00:00
|
|
|
(knots)
|
2025-01-13 12:22:27 +00:00
|
|
|
(knots thread-pool))
|
|
|
|
|
|
|
|
|
|
(let ((thread-pool
|
2025-05-16 11:48:41 +01:00
|
|
|
(make-fixed-size-thread-pool 2)))
|
|
|
|
|
|
|
|
|
|
(assert-equal
|
|
|
|
|
(call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
(lambda ()
|
|
|
|
|
4))
|
|
|
|
|
4))
|
|
|
|
|
|
|
|
|
|
(let ((thread-pool
|
|
|
|
|
(make-fixed-size-thread-pool
|
|
|
|
|
2
|
|
|
|
|
#:thread-initializer (const '(2)))))
|
|
|
|
|
|
|
|
|
|
(assert-equal
|
|
|
|
|
(call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
(lambda (num)
|
|
|
|
|
(* 2 num)))
|
|
|
|
|
4))
|
|
|
|
|
|
|
|
|
|
(let ((thread-pool
|
|
|
|
|
(make-fixed-size-thread-pool 2)))
|
|
|
|
|
|
|
|
|
|
(assert-equal
|
|
|
|
|
#t
|
|
|
|
|
(with-exception-handler
|
|
|
|
|
(lambda (exn)
|
|
|
|
|
(knots-exception? exn))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
(lambda ()
|
|
|
|
|
(+ 1 'a))))
|
|
|
|
|
#:unwind? #t)))
|
|
|
|
|
|
|
|
|
|
(run-fibers-for-tests
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let ((thread-pool
|
|
|
|
|
(make-thread-pool 2)))
|
2025-01-13 12:22:27 +00:00
|
|
|
|
|
|
|
|
(assert-equal
|
|
|
|
|
(call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
(lambda ()
|
|
|
|
|
4))
|
|
|
|
|
4))))
|
|
|
|
|
|
2025-05-16 11:48:41 +01:00
|
|
|
(run-fibers-for-tests
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let ((thread-pool
|
|
|
|
|
(make-thread-pool
|
|
|
|
|
2
|
|
|
|
|
#:thread-initializer (const '(2)))))
|
2025-01-13 12:22:27 +00:00
|
|
|
|
|
|
|
|
(assert-equal
|
|
|
|
|
(call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
(lambda (num)
|
|
|
|
|
(* 2 num)))
|
|
|
|
|
4))))
|
|
|
|
|
|
2025-05-16 11:48:41 +01:00
|
|
|
(run-fibers-for-tests
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let ((thread-pool
|
|
|
|
|
(make-thread-pool 2)))
|
2025-02-27 12:08:41 +00:00
|
|
|
|
|
|
|
|
(assert-equal
|
|
|
|
|
#t
|
|
|
|
|
(with-exception-handler
|
|
|
|
|
(lambda (exn)
|
|
|
|
|
(knots-exception? exn))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
(lambda ()
|
|
|
|
|
(+ 1 'a))))
|
|
|
|
|
#:unwind? #t)))))
|
|
|
|
|
|
2025-11-17 11:37:26 +00:00
|
|
|
(let ((thread-pool
|
2025-11-19 13:18:39 +00:00
|
|
|
(make-fixed-size-thread-pool
|
|
|
|
|
1
|
|
|
|
|
#:thread-lifetime 1
|
|
|
|
|
#:thread-initializer
|
|
|
|
|
(lambda ()
|
|
|
|
|
(list (make-atomic-box #t))))))
|
2025-11-17 11:37:26 +00:00
|
|
|
|
|
|
|
|
(for-each
|
|
|
|
|
(lambda _
|
|
|
|
|
(call-with-thread
|
|
|
|
|
thread-pool
|
2025-11-19 13:18:39 +00:00
|
|
|
(lambda (box)
|
|
|
|
|
(if (atomic-box-ref box)
|
|
|
|
|
(atomic-box-set! box #f)
|
|
|
|
|
(error (atomic-box-ref box))))))
|
2025-11-17 11:37:26 +00:00
|
|
|
(iota 10)))
|
|
|
|
|
|
|
|
|
|
(run-fibers-for-tests
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let ((thread-pool
|
|
|
|
|
(make-thread-pool 1 #:thread-lifetime 1)))
|
|
|
|
|
|
|
|
|
|
(for-each
|
|
|
|
|
(lambda _
|
|
|
|
|
(call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
(lambda () #f)))
|
|
|
|
|
(iota 10)))))
|
|
|
|
|
|
2025-11-19 13:18:39 +00:00
|
|
|
(let ((thread-pool
|
|
|
|
|
(make-fixed-size-thread-pool
|
|
|
|
|
1
|
|
|
|
|
#:thread-lifetime 2
|
|
|
|
|
#:thread-initializer
|
|
|
|
|
(lambda ()
|
|
|
|
|
(list (make-atomic-box 2))))))
|
|
|
|
|
|
|
|
|
|
(define (ref-and-decrement box)
|
|
|
|
|
(let ((val (atomic-box-ref box)))
|
|
|
|
|
(atomic-box-set! box (- val 1))
|
|
|
|
|
val))
|
|
|
|
|
|
|
|
|
|
(unless (= 2 (call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
ref-and-decrement))
|
|
|
|
|
(error))
|
|
|
|
|
(unless (= 1 (call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
ref-and-decrement))
|
|
|
|
|
(error))
|
|
|
|
|
(unless (= 2 (call-with-thread
|
|
|
|
|
thread-pool
|
|
|
|
|
ref-and-decrement))
|
|
|
|
|
(error)))
|
|
|
|
|
|
2025-01-13 12:22:27 +00:00
|
|
|
(display "thread-pool test finished successfully\n")
|