Skip to content

Commit c5ea4e8

Browse files
committed
Allow binding a callback to configure worker environments
1 parent c3d1357 commit c5ea4e8

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

test/task-runner.lisp

+27
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,30 @@
9191
;; Ensure correct results are returned by multi-threaded task-map
9292
;; (in any order).
9393
(mapc (lambda (result) (is (member result results))) '(2 3 4))))
94+
95+
(defvar *in-worker*)
96+
97+
(deftest test-worker-env ()
98+
"Using `*worker-funcall*' should change the environment of tasks
99+
launched directly as well as tasks launched from the task thread."
100+
(is (not (boundp '*in-worker*)))
101+
(let* ((thread-count 4)
102+
(worker-fn
103+
(lambda (fn)
104+
(let ((*in-worker* t))
105+
(funcall fn))))
106+
(*worker-funcall* worker-fn)
107+
boundp)
108+
(task-map thread-count
109+
(lambda (x)
110+
(declare (ignore x))
111+
(synchronized ('boundp)
112+
(push (boundp '*in-worker*) boundp))
113+
(task-map thread-count
114+
(lambda (x)
115+
(declare (ignore x))
116+
(synchronized ('boundp)
117+
(push (boundp '*in-worker*) boundp)))
118+
(make-list thread-count)))
119+
(make-list thread-count))
120+
(is (every (eqls t) boundp))))

utility/task.lisp

+12-3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
(:export
115115
:task-runner
116116
:*task-runner*
117+
:*worker-funcall*
117118
:task-runner-jobs
118119
:task-runner-workers
119120
:task-runner-workers-count
@@ -164,6 +165,11 @@
164165
(defparameter *task-runner* nil
165166
"Bind *TASK-RUNNER* for worker threads")
166167

168+
(defparameter *worker-funcall* #'funcall
169+
"Function that invokes worker loop to run.
170+
When this value is bound when starting a task, the value is propagated
171+
through to any further tasks launched from the first task, and so on.")
172+
167173
(defclass task ()
168174
((object :initarg :object :accessor task-object))
169175
(:documentation "Base class for all task classes."))
@@ -274,10 +280,13 @@
274280
(defun task-runner-create-worker (runner)
275281
"Create a new worker thread."
276282
(let ((*default-special-bindings*
277-
(acons '*task-runner* runner
278-
*default-special-bindings*)))
283+
(list* (cons '*task-runner* runner)
284+
(cons '*worker-funcall* *worker-funcall*)
285+
*default-special-bindings*)))
279286
(with-lock-held ((task-runner-workers-lock runner))
280-
(push (make-thread 'start-worker
287+
(push (make-thread
288+
(lambda ()
289+
(funcall *worker-funcall* #'start-worker))
281290
:name (format nil "~A-~D" "software-mutator"
282291
(incf worker-id)))
283292
(task-runner-workers runner))))))

0 commit comments

Comments
 (0)