aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2021-01-31 19:50:38 +0100
committerTeddy Wing2021-01-31 19:50:38 +0100
commitdc2fc9f2cd69422c720daaa531d1bdb9a6bde0e5 (patch)
tree705853050ab666ca421fada33ce07c9ead14ea2c /src
downloadcl-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')
-rw-r--r--src/package.lisp9
-rw-r--r--src/wait-group.lisp27
2 files changed, 36 insertions, 0 deletions
diff --git a/src/package.lisp b/src/package.lisp
new file mode 100644
index 0000000..c868d02
--- /dev/null
+++ b/src/package.lisp
@@ -0,0 +1,9 @@
+(defpackage :wait-group
+ (:nicknames :wg)
+
+ (:use :cl)
+
+ (:export #:make-wait-group
+ #:add
+ #:done
+ #:wait))
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)))