conditions.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ;; Conditions
  2. ;;;; Copyright (C) 2017 Andy Wingo <wingo@pobox.com>
  3. ;;;; Copyright (C) 2017 Christopher Allan Webber <cwebber@dustycloud.org>
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Condition variable (cvar) implementation following the 2009 ICFP
  19. ;;; paper "Parallel Concurrent ML" by John Reppy, Claudio V. Russo,
  20. ;;; and Yingqui Xiao. See channels.scm for additional commentary.
  21. ;;;
  22. ;;; Besides the general ways in which this implementation differs from
  23. ;;; the paper, this channel implementation avoids locks entirely.
  24. ;;; Still, we should disable interrupts while any operation is in a
  25. ;;; "claimed" state to avoid excess latency due to pre-emption. It
  26. ;;; would be great if we could verify our protocol though; the
  27. ;;; parallel channel operations are still gnarly.
  28. (define-module (fibers conditions)
  29. #:use-module (srfi srfi-9)
  30. #:use-module (ice-9 atomic)
  31. #:use-module (ice-9 match)
  32. #:use-module (fibers stack)
  33. #:use-module (fibers counter)
  34. #:use-module (fibers operations)
  35. #:export (make-condition
  36. condition?
  37. signal-condition!
  38. wait-operation
  39. wait))
  40. (define-record-type <condition>
  41. (%make-condition signalled? waiters gc-step)
  42. condition?
  43. ;; atomic box of bool
  44. (signalled? condition-signalled?)
  45. ;; stack of flag+resume pairs
  46. (waiters channel-waiters)
  47. ;; count until garbage collection
  48. (gc-step channel-gc-step))
  49. (define (make-condition)
  50. "Make a fresh condition variable."
  51. (%make-condition (make-atomic-box #f) (make-empty-stack) (make-counter)))
  52. (define (resume-waiters! waiters)
  53. (define (resume-one flag resume)
  54. (match (atomic-box-compare-and-swap! flag 'W 'S)
  55. ('W (resume values))
  56. ('C (resume-one flag resume))
  57. ('S #f)))
  58. ;; Non-tail-recursion to resume waiters in the order they were added
  59. ;; to the waiters stack.
  60. (let lp ((waiters (stack-pop-all! waiters)))
  61. (match waiters
  62. (() #f)
  63. (((flag . resume) . waiters)
  64. (lp waiters)
  65. (resume-one flag resume)))))
  66. (define (signal-condition! cvar)
  67. "Mark @var{cvar} as having been signalled. Resume any fiber or
  68. thread waiting for @var{cvar}. If @var{cvar} is already signalled,
  69. calling @code{signal-condition!} does nothing and returns @code{#f};
  70. returns @code{#t} otherwise."
  71. (match cvar
  72. (($ <condition> signalled? waiters)
  73. (match (atomic-box-compare-and-swap! signalled? #f #t)
  74. (#f ;; We signalled the cvar.
  75. (resume-waiters! waiters)
  76. #t)
  77. (#t ;; Cvar already signalled.
  78. #f)))))
  79. (define (wait-operation cvar)
  80. "Make an operation that will complete when @var{cvar} is signalled."
  81. (match cvar
  82. (($ <condition> signalled? waiters gc-step)
  83. (define (try-fn) (and (atomic-box-ref signalled?) values))
  84. (define (block-fn flag sched resume)
  85. ;; Decrement the garbage collection counter.
  86. ;; If we've surpassed the number of steps until garbage collection,
  87. ;; prune out waiters that have already succeeded.
  88. ;;
  89. ;; Note that it's possible that this number will go negative,
  90. ;; but stack-filter! should handle this without errors (though
  91. ;; possibly extra spin), and testing against zero rather than
  92. ;; less than zero will prevent multiple threads from repeating
  93. ;; this work.
  94. (when (= (counter-decrement! gc-step) 0)
  95. (stack-filter! waiters
  96. (match-lambda
  97. ((flag . resume)
  98. (not (eq? (atomic-box-ref flag) 'S)))))
  99. (counter-reset! gc-step))
  100. ;; We have suspended the current fiber or thread; arrange for
  101. ;; signal-condition! to call resume-get by adding the flag and
  102. ;; resume callback to the cvar's waiters stack.
  103. (stack-push! waiters (cons flag resume))
  104. ;; It could be that the cvar was actually signalled in between
  105. ;; the calls to try-fn and block-fn. In that case it could be
  106. ;; that resume-waiters! was called before our push above. In
  107. ;; that case, call resume-waiters! to resolve the race.
  108. (when (atomic-box-ref signalled?)
  109. (resume-waiters! waiters))
  110. (values))
  111. (make-base-operation #f try-fn block-fn))))
  112. (define (wait cvar)
  113. "Wait until @var{cvar} has been signalled."
  114. (perform-operation (wait-operation cvar)))