Recover cancellation cleanup for AsyncEvent wait(). (#398)

* Recover cancellation cleanup for AsyncEvent wait().

* Address review comments.
This commit is contained in:
Eugene Kabanov 2023-06-01 18:09:49 +03:00 committed by GitHub
parent 5be223e92c
commit 315a27236c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

@ -223,12 +223,15 @@ proc wait*(event: AsyncEvent): Future[void] =
## If the internal flag is `true` on entry, return immediately. Otherwise,
## block until another task calls `fire()` to set the flag to `true`,
## then return.
var w = newFuture[void]("AsyncEvent.wait")
let retFuture = newFuture[void]("AsyncEvent.wait")
proc cancellation(udata: pointer) {.gcsafe, raises: [Defect].} =
event.waiters.keepItIf(it != retFuture)
if not(event.flag):
event.waiters.add(w)
retFuture.cancelCallback = cancellation
event.waiters.add(retFuture)
else:
w.complete()
w
retFuture.complete()
retFuture
proc fire*(event: AsyncEvent) =
## Set the internal flag of ``event`` to `true`. All tasks waiting for it