Skip to content

Commit

Permalink
completion: add optional debugging name
Browse files Browse the repository at this point in the history
  • Loading branch information
fare committed Mar 9, 2021
1 parent dbda747 commit 1653647
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions doc/reference/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -855,13 +855,17 @@ The following low level unsafe operations are also exported.
### make-completion
``` scheme
(make-completion) -> completion
(make-completion name) -> completion
```

Creates a new asynchronous completion, a synchronization construct which blocks
until a thread signals that its task either succeeded or failed via
`completion-post!` or `completion-error!`, respectively, notifying all waiting
threads about the result.

An optional `name` may be provided for debugging purposes:
if you deadlock, you'll be able to more easily identify which completion went wrong.

::: tip Examples:
``` scheme
> (import :gerbil/gambit/threads :std/iter :std/sugar)
Expand Down
2 changes: 1 addition & 1 deletion src/std/misc/completion-test.ss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
(def thr (spawn completion-post! c 'done))
(check (completion-wait! c) => 'done))
(test-case "test completion error"
(def c (make-completion))
(def c (make-completion 'error-test))
(def thr (spawn completion-error! c 'fail))
(check-exception (completion-wait! c) (cut eq? <> 'fail)))))
6 changes: 3 additions & 3 deletions src/std/misc/completion.ss
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
constructor: :init!)

(defmethod {:init! completion}
(lambda (self)
(lambda (self (name 'completion)) ;; name is for debugging
(struct-instance-init! self
(make-mutex 'completion)
(make-condition-variable 'completion))))
(make-mutex name)
(make-condition-variable name))))

(def (completion-wait! compl)
(with ((completion mx cv) compl)
Expand Down

0 comments on commit 1653647

Please sign in to comment.