ping-pong.scm 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env guile
  2. # -*- scheme -*-
  3. !#
  4. (use-modules (ice-9 match)
  5. (fibers)
  6. (fibers channels))
  7. (define (run-ping-pong message-count)
  8. (let ((ch (make-channel)))
  9. (spawn-fiber (lambda ()
  10. (let lp ()
  11. (put-message ch (get-message ch))
  12. (lp)))
  13. #:parallel? #t)
  14. (let lp ((n 0))
  15. (when (< n message-count)
  16. (put-message ch n)
  17. (get-message ch)
  18. (lp (1+ n))))))
  19. (define (test pair-count message-count)
  20. (let ((done (make-channel)))
  21. (for-each (lambda (_)
  22. (spawn-fiber (lambda ()
  23. (run-ping-pong message-count)
  24. (put-message done 'done))
  25. #:parallel? #t))
  26. (iota pair-count))
  27. (for-each (lambda (_) (get-message done))
  28. (iota pair-count))))
  29. (define (main args)
  30. (match args
  31. ((_ pair-count message-count)
  32. (let ((pair-count (string->number pair-count))
  33. (message-count (string->number message-count)))
  34. (run-fibers (lambda () (test pair-count message-count)))))))
  35. (when (batch-mode?) (main (program-arguments)))