Skip to content

Commit

Permalink
refactored `with-fut-resolve' to accept more use-cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbergmann committed Mar 25, 2024
1 parent f94ac50 commit 776cb38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/fcomputation.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ Example:
(with-fut-resolve
(bt:make-thread
(lambda ()
(fresolve (do-some-lengthy-calculation)))))
(let ((result (do-some-lengthy-calculation)))
(fresolve result)))))
```
"
`(macrolet ((fresolve (resolve-form)
`(make-future (lambda (resolve-fun)
(let ((resolved ,resolve-form))
(funcall resolve-fun resolved))))))
,@body))
`(make-future (lambda (resolve-fun)
(macrolet ((fresolve (resolve-value)
`(funcall resolve-fun ,resolve-value)))
,@body))))

(defun make-future (execute-fun)
"Creates a future. `execute-fun` is the lambda that is executed when the future is created.
Expand Down Expand Up @@ -101,14 +101,15 @@ Create a future with:
(defun %fcompleted (future completed-fun)
(with-slots (promise) future
(attach promise completed-fun))
nil)
future)

(defmacro fcompleted (future (result) &body body)
"Completion handler on the given `future`.
If the `future` is already complete then the `body` executed immediately.
`result` represents the future result.
`body` is executed when future completed.
Returns the future.
Example:
Expand Down
8 changes: 5 additions & 3 deletions src/timeutils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
"Waits until `cond-fun' is not `nil' or `max-time' elapsed.
This blocks the calling thread."
(loop
:with wait-acc = 0
:unless (or (funcall cond-fun) (> wait-acc max-time))
:for fun-result := (funcall cond-fun)
:with wait-acc := 0
:while (and (not fun-result) (< wait-acc max-time))
:do (progn
(sleep sleep-time)
(incf wait-acc sleep-time))))
(incf wait-acc sleep-time))
:finally (return fun-result)))

(define-condition ask-timeout (serious-condition)
((wait-time :initform nil
Expand Down

0 comments on commit 776cb38

Please sign in to comment.