-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkernel.lisp
70 lines (59 loc) · 1.77 KB
/
kernel.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(uiop:define-package :overlord/kernel
(:use #:cl #:alexandria #:serapeum)
(:import-from #:overlord/message #:message)
(:import-from #:overlord/specials #:use-threads-p)
(:import-from #:lparallel
#:*kernel*
#:make-kernel
#:end-kernel
#:pmap
#:task-handler-bind
#:invoke-transfer-error)
(:import-from #:uiop
#:register-image-dump-hook)
(:export
#:with-meta-kernel
#:end-meta-kernel
#:nproc))
(in-package :overlord/kernel)
(defconst thread-count-cap 20)
(defun nproc ()
(assure (integer 1 *)
(count-cpus :online nil)))
(def nproc
(nproc))
(def meta-kernel-size
(min thread-count-cap
(* 2 nproc)))
(defvar *meta-kernel* nil
"Lparallel kernel for fetching target metadata.")
(defun call/meta-kernel (thunk)
(if (use-threads-p)
(let ((*kernel* (ensure-meta-kernel)))
(task-handler-bind ((error #'invoke-transfer-error))
(funcall thunk)))
(funcall thunk)))
(defmacro with-meta-kernel ((&key) &body body)
(with-thunk (body)
`(call/meta-kernel ,body)))
(defun ensure-meta-kernel ()
(start-meta-kernel)
*meta-kernel*)
(defun start-meta-kernel ()
(unless *meta-kernel*
(synchronized ('*meta-kernel*)
(unless *meta-kernel*
(message "Initializing metadata thread pool for session")
(setf *meta-kernel*
(make-kernel meta-kernel-size
:name "Overlord metadata fetcher"))))))
(defun end-meta-kernel ()
"Terminate the Overlord kernel."
(when *meta-kernel*
(synchronized ('*meta-kernel*)
(when-let (kernel *meta-kernel*)
(setf *meta-kernel* nil)
(message "Terminating Overlord metadata thread pool")
(let ((*kernel* kernel))
(end-kernel :wait t))))))
(register-image-dump-hook 'end-meta-kernel)