chain.scm 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env guile
  2. # -*- scheme -*-
  3. !#
  4. (use-modules (ice-9 match)
  5. (fibers)
  6. (fibers channels))
  7. (define (make-chain link-count make-head make-link make-tail)
  8. (let lp ((link-count link-count) (ch (make-head)))
  9. (if (zero? link-count)
  10. (make-tail ch)
  11. (lp (1- link-count) (make-link ch)))))
  12. (define (test link-count message-count)
  13. (get-message
  14. (make-chain
  15. link-count
  16. (lambda ()
  17. (let ((out (make-channel)))
  18. (spawn-fiber (lambda ()
  19. (let lp ((n 0))
  20. (put-message out n)
  21. (lp (1+ n))))
  22. #:parallel? #t)
  23. out))
  24. (lambda (in)
  25. (let ((out (make-channel)))
  26. (spawn-fiber (lambda ()
  27. (let lp ()
  28. (put-message out (get-message in))
  29. (lp)))
  30. #:parallel? #t)
  31. out))
  32. (lambda (in)
  33. (let ((out (make-channel)))
  34. (spawn-fiber (lambda ()
  35. (let lp ()
  36. (if (< (get-message in) message-count)
  37. (lp)
  38. (put-message out 'done))))
  39. #:parallel? #t)
  40. out)))))
  41. (define (main args)
  42. (match args
  43. ((_ link-count message-count)
  44. (let ((link-count (string->number link-count))
  45. (message-count (string->number message-count)))
  46. (run-fibers (lambda () (test link-count message-count)))))))
  47. (when (batch-mode?) (main (program-arguments)))