channels.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 channels)
  19. #:use-module ((ice-9 threads) #:select (current-processor-count))
  20. #:use-module (fibers)
  21. #:use-module (fibers channels))
  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)))
  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. (define-syntax-rule (do-times n exp)
  49. (let lp ((count n))
  50. (let ((count (1- count)))
  51. exp
  52. (unless (zero? count) (lp count)))))
  53. (define-syntax-rule (rpc exp)
  54. (let ((ch (make-channel)))
  55. (spawn-fiber (lambda () (put-message ch exp)))
  56. (get-message ch)))
  57. (assert-run-fibers-returns (1) (rpc 1))
  58. (define (rpc-fib n)
  59. (rpc (if (< n 2)
  60. 1
  61. (+ (rpc-fib (- n 1)) (rpc-fib (- n 2))))))
  62. (assert-run-fibers-returns (75025) (rpc-fib 24))
  63. (define (pingpong M N)
  64. (let ((request (make-channel)))
  65. (for-each (lambda (m)
  66. (spawn-fiber (lambda ()
  67. (let lp ((n 0))
  68. (when (< n N)
  69. (let ((reply (make-channel)))
  70. (put-message request reply)
  71. (get-message reply)
  72. (lp (1+ n))))))
  73. #:parallel? #t))
  74. (iota M))
  75. (let lp ((m 0))
  76. (when (< m M)
  77. (let lp ((n 0))
  78. (when (< n N)
  79. (put-message (get-message request) 'foo)
  80. (lp (1+ n))))
  81. (lp (1+ m))))))
  82. (assert-run-fibers-terminates (pingpong (current-processor-count) 1000))
  83. ;; timed channel wait
  84. ;; multi-channel wait
  85. ;; cross-thread calls
  86. (exit (if failed? 1 0))