local-ping-pong.scm 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. (let lp ((n 0))
  14. (when (< n message-count)
  15. (put-message ch n)
  16. (get-message ch)
  17. (lp (1+ n))))))
  18. (define (test pair-count message-count)
  19. (let ((done (make-channel)))
  20. (for-each (lambda (_)
  21. (spawn-fiber (lambda ()
  22. (run-ping-pong message-count)
  23. (put-message done 'done))
  24. #:parallel? #t))
  25. (iota pair-count))
  26. (for-each (lambda (_) (get-message done))
  27. (iota pair-count))))
  28. (define (main args)
  29. (match args
  30. ((_ pair-count message-count)
  31. (let ((pair-count (string->number pair-count))
  32. (message-count (string->number message-count)))
  33. (run-fibers (lambda () (test pair-count message-count)))))))
  34. (when (batch-mode?) (main (program-arguments)))