foreign.scm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 foreign)
  19. #:use-module (fibers)
  20. #:use-module (fibers operations)
  21. #:use-module (fibers channels)
  22. #:use-module (fibers timers)
  23. #:use-module (ice-9 threads))
  24. (define failed? #f)
  25. (define-syntax-rule (assert-equal expected actual)
  26. (let ((x expected))
  27. (format #t "assert ~s equal to ~s: " 'actual x)
  28. (force-output)
  29. (let ((y actual))
  30. (cond
  31. ((equal? x y) (format #t "ok\n"))
  32. (else
  33. (format #t "no (got ~s)\n" y)
  34. (set! failed? #t))))))
  35. (define-syntax-rule (assert-terminates exp)
  36. (begin
  37. (format #t "assert ~s terminates: " 'exp)
  38. (force-output)
  39. exp
  40. (format #t "ok\n")))
  41. (define (receive-from-fiber x)
  42. (let* ((ch (make-channel))
  43. (t (call-with-new-thread
  44. (lambda ()
  45. (run-fibers (lambda () (put-message ch 42))))))
  46. (v (get-message ch)))
  47. (join-thread t)
  48. v))
  49. (define (send-to-fiber x)
  50. (let* ((ch (make-channel))
  51. (t (call-with-new-thread
  52. (lambda ()
  53. (run-fibers (lambda () (get-message ch)))))))
  54. (put-message ch x)
  55. (join-thread t)))
  56. (assert-equal #f #f)
  57. (assert-terminates #t)
  58. (assert-terminates (sleep 1))
  59. (assert-terminates (perform-operation (sleep-operation 1)))
  60. (assert-equal 42 (receive-from-fiber 42))
  61. (assert-equal 42 (send-to-fiber 42))
  62. (exit (if failed? 1 0))