condvar.scm 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees
  3. ; Condition variables (cells for which readers block until the next write).
  4. ;
  5. ; (make-condvar [id]) -> condvar
  6. ; (condvar-has-value? condvar) -> boolean
  7. ; (condvar-value condvar) -> value
  8. ; (maybe-commit-and-wait-for-condvar condvar) -> boolean
  9. ; (condvar-set! condvar value)
  10. (define-synchronized-record-type condvar :condvar
  11. (really-make-condvar queue has-value? id)
  12. (has-value? value) ; synchronize on these
  13. condvar?
  14. (queue condvar-queue)
  15. (has-value? condvar-has-value? set-condvar-has-value?!)
  16. (value condvar-value set-condvar-value!)
  17. (id condvar-id))
  18. (define-record-discloser :condvar
  19. (lambda (condvar)
  20. (if (condvar-id condvar)
  21. (list 'condvar (condvar-id condvar))
  22. '(condvar))))
  23. (define (make-condvar . id-option)
  24. (really-make-condvar (make-queue)
  25. #f
  26. (if (null? id-option)
  27. #f
  28. (car id-option))))
  29. (define (maybe-commit-and-wait-for-condvar condvar . maybe-deadlock?)
  30. (apply maybe-commit-and-block-on-queue (condvar-queue condvar) maybe-deadlock?))
  31. (define (maybe-commit-and-set-condvar! condvar value)
  32. (set-condvar-value! condvar value)
  33. (set-condvar-has-value?! condvar #t)
  34. (maybe-commit-and-make-ready (condvar-queue condvar)))
  35. (define (condvar-has-waiters? condvar)
  36. (not (thread-queue-empty? (condvar-queue condvar))))