preemption.scm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ;; Fibers: cooperative, event-driven user-space threads.
  2. ;;;; Copyright (C) 2016 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (tests parameters)
  19. #:use-module (ice-9 atomic)
  20. #:use-module (fibers)
  21. #:use-module (fibers scheduler))
  22. (define failed? #f)
  23. (define-syntax-rule (assert-equal expected actual)
  24. (let ((x expected))
  25. (format #t "assert ~s equal to ~s: " 'actual x)
  26. (force-output)
  27. (let ((y actual))
  28. (cond
  29. ((equal? x y) (format #t "ok\n"))
  30. (else
  31. (format #t "no (got ~s)\n" y)
  32. (set! failed? #t))))))
  33. (define-syntax-rule (assert-run-fibers-terminates exp)
  34. (begin
  35. (format #t "assert run-fibers on ~s terminates: " 'exp)
  36. (force-output)
  37. (let ((start (get-internal-real-time)))
  38. (call-with-values (lambda () (run-fibers (lambda () exp) #:hz 1000))
  39. (lambda vals
  40. (format #t "ok (~a s)\n" (/ (- (get-internal-real-time) start)
  41. 1.0 internal-time-units-per-second))
  42. (apply values vals))))))
  43. (define-syntax-rule (assert-run-fibers-returns (expected ...) exp)
  44. (begin
  45. (call-with-values (lambda () (assert-run-fibers-terminates exp))
  46. (lambda run-fiber-return-vals
  47. (assert-equal '(expected ...) run-fiber-return-vals)))))
  48. (assert-run-fibers-terminates
  49. (let lp ((n 0))
  50. (when (< n #e1e8) (lp (1+ n)))))
  51. (define (race-until n)
  52. (let ((box (make-atomic-box 0)))
  53. (spawn-fiber (lambda ()
  54. (let lp ()
  55. (let ((x (atomic-box-ref box)))
  56. (unless (= x n)
  57. (when (even? x)
  58. (atomic-box-set! box (1+ x)))
  59. (lp))))))
  60. (spawn-fiber (lambda ()
  61. (let lp ()
  62. (let ((x (atomic-box-ref box)))
  63. (unless (= x n)
  64. (when (odd? x)
  65. (atomic-box-set! box (1+ x)))
  66. (lp))))))
  67. (let lp ()
  68. (if (equal? (atomic-box-ref box) n)
  69. n
  70. (lp)))))
  71. (assert-run-fibers-returns (100) (race-until 100))
  72. (exit (if failed? 1 0))