Skip to content

Commit 4813b17

Browse files
committed
Add utility to ensure that a kernel with arbitrary workers exist.
1 parent 9c11f40 commit 4813b17

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/kernel/core.lisp

+11-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ A kernel will not be garbage collected until `end-kernel' is called."
260260
(fill-workers workers kernel)
261261
kernel))
262262

263-
(defun check-kernel ()
263+
(defun check-kernel (&key worker-count)
264264
"Ensures the value of `*kernel*' is a kernel instance. Provides the
265265
MAKE-KERNEL and STORE-VALUE restarts. Returns `*kernel*'."
266266
(or *kernel*
@@ -275,6 +275,16 @@ MAKE-KERNEL and STORE-VALUE restarts. Returns `*kernel*'."
275275
(check-type value kernel)
276276
(setf *kernel* value)))))
277277

278+
(defun ensure-kernel (worker-count &rest other-keys)
279+
"Return `*kernel*', a kernel instance featuring WORKER-COUNT."
280+
;; Notice that when *kernel* exists with 2 workers, then calling:
281+
;; (ensure-kernel N :name "a-different-name-from-*kernel*")
282+
;; has no effect since the number of workers match.
283+
(if (and *kernel*
284+
(eq (length (workers *kernel*)) worker-count))
285+
*kernel*
286+
(setf *kernel* (apply #'make-kernel worker-count other-keys))))
287+
278288
(defmacro define-worker-info-reader (name slot &optional (result slot))
279289
`(defun ,name ()
280290
,(format nil "Return the ~a passed to `make-kernel'."

src/kernel/package.lisp

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#+lparallel.with-stealing-scheduler #:lparallel.spin-queue)
4343
(:export #:make-kernel
4444
#:check-kernel
45+
#:ensure-kernel
4546
#:end-kernel
4647
#:kernel-worker-count
4748
#:kernel-worker-index

0 commit comments

Comments
 (0)