cs & thread: repair for sync/timeout

If a `sync/timeout` on a semaphore (or simiilar asynchronous event)
succeeds simultaneously with the timeout, the success could get lost.
This commit is contained in:
Matthew Flatt 2019-09-28 11:34:25 -06:00
parent 11c6f1686c
commit a20a27f5b0
2 changed files with 17 additions and 1 deletions

View File

@ -1528,6 +1528,22 @@
(void (sync t))
(test 'ok values v))
;; ----------------------------------------
;; Try to make a semaphore-post succeed at exactly
;; the same time that a `sync/timeout` times out
(for ([i 10])
(define s (make-semaphore))
(define t (thread
(lambda ()
(sleep (- 0.1 (* 0.001 (random))))
(semaphore-post s))))
(define r (sync/timeout 0.1 s))
(unless r
;; This will get stuck if the success of time sync got lost
(sync s))
(thread-wait t))
;; ----------------------------------------
(report-errs)

View File

@ -121,13 +121,13 @@
timeout
(timeout-at . <= . (current-inexact-milliseconds)))
(start-atomic)
(syncing-done! s none-syncer)
(cond
[(syncing-selected s)
;; Selected after all:
(end-atomic)
(loop #f #f)]
[else
(syncing-done! s none-syncer)
(end-atomic)
;; Return result in a thunk:
(lambda () #f)])]