-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data flow instead of continuations.
- Loading branch information
Showing
18 changed files
with
235 additions
and
1,023 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
(in-package :cl-bodge.concurrency) | ||
|
||
|
||
(defmacro *> (invariant-n-opts condition-var &body body) | ||
(declare (ignore invariant-n-opts condition-var body)) | ||
(error "*> cannot be used outside of flow operator")) | ||
|
||
|
||
(defun nop (result error-p) | ||
(declare (ignore result error-p))) | ||
|
||
|
||
(declaim (ftype (function (* (or function null) * list function list) *) invariant-dispatch)) | ||
(defun invariant-dispatch (dispatcher result-callback invariant opts fn args) | ||
(labels ((return-error (e) | ||
(funcall result-callback (list e) t)) | ||
(dispatched () | ||
(handler-bind ((simple-error #'return-error)) | ||
(funcall result-callback | ||
(multiple-value-list (apply fn args)) nil)))) | ||
(apply #'dispatch dispatcher #'dispatched :invariant invariant opts))) | ||
|
||
|
||
(defmacro -> (invariant-n-opts lambda-list &body body) | ||
(destructuring-bind (invariant &rest opts) (ensure-list invariant-n-opts) | ||
(with-gensyms (dispatcher body-fn args result-callback) | ||
`(lambda (,dispatcher ,result-callback &rest ,args) | ||
(declare (ignorable ,args)) | ||
(flet ((,body-fn ,lambda-list | ||
,@body)) | ||
(invariant-dispatch ,dispatcher (or ,result-callback #'nop) ,invariant (list ,@opts) | ||
#',body-fn ,(when (not (null lambda-list)) args))))))) | ||
|
||
|
||
(defun dispatch-list-flow (list dispatcher result-callback args) | ||
(labels ((dispatch-list (fn-list args) | ||
(flet ((dispatch-next (result error-p) | ||
(if error-p | ||
(log:error "Error during serial flow dispatch: ~A" result) | ||
(dispatch-list (rest fn-list) result)))) | ||
(if (null fn-list) | ||
(funcall result-callback args nil) | ||
(let ((flow-element (first fn-list))) | ||
(if (listp flow-element) | ||
(dispatch-list-flow flow-element dispatcher #'dispatch-next args) | ||
(apply flow-element dispatcher #'dispatch-next args))))))) | ||
(dispatch-list list args))) | ||
|
||
|
||
(defun dispatch-parallel-flow (list dispatcher result-callback args) | ||
(let ((n 0) | ||
(lock (make-recursive-lock "~>")) | ||
(flow-result (copy-tree list))) | ||
(labels ((count-elements (root) | ||
(if (listp root) | ||
(loop for node in root summing (count-elements node)) | ||
1)) | ||
(resolve (callback-list) | ||
(unless (null callback-list) | ||
(let* ((element (car callback-list))) | ||
(if (listp element) | ||
(resolve element) | ||
(flet ((%cons-result-callback (result error-p) | ||
(when error-p | ||
(log:error "Error during parralel flow dispatch: ~A" | ||
result)) | ||
(setf (car callback-list) result) | ||
(with-recursive-lock-held (lock) (decf n)) | ||
(when (= n 0) | ||
(funcall result-callback flow-result nil)))) | ||
(resolve (cdr callback-list)) | ||
(apply element dispatcher #'%cons-result-callback args))))))) | ||
(setf n (count-elements list)) | ||
(resolve flow-result)))) | ||
|
||
|
||
(defmacro >> (&body flow) | ||
(with-gensyms (dispatcher result-callback args flow-tree) | ||
`(lambda (,dispatcher ,result-callback &rest ,args) | ||
(declare (type (or null (function (list t) *)) ,result-callback)) | ||
(let ((,flow-tree (list ,@flow))) | ||
(dispatch-list-flow ,flow-tree ,dispatcher (or ,result-callback #'nop) ,args))))) | ||
|
||
|
||
(defmacro define-flow (name (&rest lambda-list) &body body) | ||
`(defun ,name ,lambda-list | ||
(>> ,@body))) | ||
|
||
|
||
(defmacro ~> (&body body) | ||
(with-gensyms (dispatcher args result-callback flow) | ||
`(lambda (,dispatcher ,result-callback &rest ,args) | ||
(declare (type (or (function (list t) *) null) ,result-callback)) | ||
(let ((,flow (list ,@body))) | ||
(dispatch-parallel-flow ,flow ,dispatcher (or ,result-callback #'nop) ,args))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.