condvar.scm 1.3 KB

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