-
Notifications
You must be signed in to change notification settings - Fork 2
/
clorb-mt.lisp
184 lines (141 loc) · 5.74 KB
/
clorb-mt.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
;;;; clorb-mt.lisp -- System independent multi-threading and synchronization
(in-package :clorb)
(defgeneric lock (obj)
(:documentation
"Return the synchronization lock for object"))
(defgeneric waitqueue (obj)
(:documentation
"Return the waitqueue/semaphore/condition etc. for the object."))
(defclass synchronized ()
((lock :initform (make-lock "syn") :reader lock)
(waitqueue :initform (make-waitqueue) :reader waitqueue)))
(defclass synchronized-lazy (synchronized)
((waitqueue :initform nil )))
(defmethod waitqueue ((obj synchronized-lazy))
(or (slot-value obj 'waitqueue)
(setf (slot-value obj 'waitqueue)
(make-waitqueue))))
(defmacro with-synchronization (obj &body body)
"Execute body with the synchronized object's lock held."
`(with-lock (lock ,obj)
,@body))
(defun synch-locked-wait (obj)
"Synchronize wait on locked object.
Object should have (lock _) and (waitqueue _)"
(wq-locked-wait (waitqueue obj) (lock obj)))
(defun synch-notify (obj)
"Notify object's waitqueue, wake at least one process waiting on the waitqueue.
Should me called with object lock held."
(wq-notify (waitqueue obj)))
(defun synch-wait-on-condition (obj wait-func &rest wait-args)
(with-synchronization obj
(unless (apply wait-func wait-args)
(apply #'wq-locked-wait-on-condition
(waitqueue obj) (lock obj)
wait-func wait-args))))
;;;; Shared Queue
(defclass shared-queue (synchronized)
((queue :initform nil :accessor queue)))
(defmethod enqueue ((q shared-queue) obj)
(with-synchronization q
(enqf (queue q) obj)
(wq-notify (waitqueue q)))
t)
(defmethod dequeue ((q shared-queue) &optional non-blocking)
(with-synchronization q
(if non-blocking
(deqf (queue q))
(loop
(if (queue-empty-p (queue q))
(synch-locked-wait q)
(return (deqf (queue q))))))))
;;;; Execution Queue
(defclass execution-queue (synchronized)
((executor :initarg :executor :initform 'eq-exec :accessor executor)
(name-template :initarg :name-template :initform "eq" :accessor name-template)
(max-processes :initarg :max-processes :initform 10 :accessor max-processes)
(max-handler :initarg :max-handler :initform nil :accessor max-handler)
(max-idle :initarg :max-idle :initform nil :accessor max-idle)
(queue :initform nil :accessor eq-queue)
(idle-count :initform 0 :accessor idle-count)
(process-count :initform 0 :accessor process-count)
(process-table :initform nil :accessor process-table)
(name-count :initform 0 :accessor name-count)))
(defmacro with-execution-queue (q &body body)
`(with-accessors ((max-processes max-processes)
(max-handler max-handler)
(max-idle max-idle)
(queue eq-queue) (idle-count idle-count)
(process-table process-table) (process-count process-count)
(executor executor) (name-template name-template)
(name-count name-count))
,q
,@body))
(defmethod next-process-name ((q execution-queue))
(with-execution-queue q
(format nil "~A ~D" name-template (incf name-count))))
(defun eq-main (q obj entry)
(with-execution-queue q
(let* ((process (current-process)))
(setf (car entry) process)
(loop
(labels
((exit ()
(decf process-count)
(deletef entry process-table)
(return))
(set-status (status &optional obj)
(let ((x (cdr entry)))
(setf (car x) status)
(setf (cdr x) obj)))
(idle ()
(incf idle-count)
(set-status :idle)
(do () ((not (queue-empty-p queue)))
(synch-locked-wait q))
(decf idle-count))
(broken (condition)
(warn "Thread fails: ~A thr ~A" condition (current-process))
(with-synchronization q (exit))))
(set-status :working obj)
(handler-case (funcall executor obj)
(serious-condition (condition) (broken condition)))
(with-synchronization q
(when (and (queue-empty-p queue)
(or (null max-idle) (< idle-count max-idle)))
(idle))
(if (queue-empty-p queue)
(exit)
(setq obj (deqf queue)))))))))
(defun eq-exec (obj)
(etypecase obj
(function (funcall obj))
(cons (apply (car obj) (cdr obj)))))
(defmethod enqueue ((q execution-queue) obj)
(with-execution-queue q
(with-synchronization q
(flet ((ok-make-more-processes ()
(or (< process-count max-processes)
(if max-handler
(funcall max-handler q obj)))))
(cond ((and (zerop idle-count) (ok-make-more-processes))
(incf process-count)
(let ((entry (list nil nil)))
(push entry process-table)
(start-process (next-process-name q) #'eq-main q obj entry)))
(t
(enqf queue obj)
(synch-notify q)
nil))))))
(defun garb-threads (q)
(with-execution-queue q
(with-synchronization q
(setq process-table
(remove-if (lambda (entry)
(let ((p (car entry)))
(unless (process-running-p p)
(decf process-count)
(when (eql (cadr entry) :idle)
(decf idle-count))
t)))
process-table)))))