diff options
author | Teddy Wing | 2021-01-31 19:50:38 +0100 |
---|---|---|
committer | Teddy Wing | 2021-01-31 19:50:38 +0100 |
commit | dc2fc9f2cd69422c720daaa531d1bdb9a6bde0e5 (patch) | |
tree | 705853050ab666ca421fada33ce07c9ead14ea2c /src/wait-group.lisp | |
download | cl-wait-group-dc2fc9f2cd69422c720daaa531d1bdb9a6bde0e5.tar.bz2 |
Define a library for Go-style wait groups
Define a data structure and methods to represent a Go-style wait group,
similar to `sync.WaitGroup` (except single-threaded).
Implements a similar interface. Calling `wait` blocks until the wait
group's internal counter gets to 0.
Diffstat (limited to 'src/wait-group.lisp')
-rw-r--r-- | src/wait-group.lisp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/wait-group.lisp b/src/wait-group.lisp new file mode 100644 index 0000000..35bb982 --- /dev/null +++ b/src/wait-group.lisp @@ -0,0 +1,27 @@ +(in-package :wait-group) + +(defclass wait-group () + ((counter + :initform 0 + :accessor counter))) + +(defun make-wait-group () + (make-instance 'wait-group)) + +(defgeneric add (wait-group &optional amount) + (:documentation "")) + +(defmethod add ((wait-group wait-group) &optional (amount 1)) + (incf (counter wait-group) amount)) + +(defgeneric done (wait-group) + (:documentation "")) + +(defmethod done ((wait-group wait-group)) + (decf (counter wait-group))) + +(defgeneric wait (wait-group) + (:documentation "")) + +(defmethod wait ((wait-group wait-group)) + (loop until (= (counter wait-group) 0))) |