1 | ;;;-*- Mode: Lisp; Package: CCL -*- |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2009 Clozure Associates |
---|
4 | ;;; Copyright (C) 1994-2001 Digitool, Inc |
---|
5 | ;;; This file is part of Clozure CL. |
---|
6 | ;;; |
---|
7 | ;;; Clozure CL is licensed under the terms of the Lisp Lesser GNU Public |
---|
8 | ;;; License , known as the LLGPL and distributed with Clozure CL as the |
---|
9 | ;;; file "LICENSE". The LLGPL consists of a preamble and the LGPL, |
---|
10 | ;;; which is distributed with Clozure CL as the file "LGPL". Where these |
---|
11 | ;;; conflict, the preamble takes precedence. |
---|
12 | ;;; |
---|
13 | ;;; Clozure CL is referenced in the preamble as the "LIBRARY." |
---|
14 | ;;; |
---|
15 | ;;; The LLGPL is also available online at |
---|
16 | ;;; http://opensource.franz.com/preamble.html |
---|
17 | |
---|
18 | (in-package "CCL") |
---|
19 | |
---|
20 | |
---|
21 | ;;; Bootstrapping for futexes |
---|
22 | #+(and linux-target (or x86-target arm-target)) |
---|
23 | (eval-when (:compile-toplevel :execute) |
---|
24 | (pushnew :futex *features*)) |
---|
25 | |
---|
26 | #+futex |
---|
27 | (eval-when (:compile-toplevel :execute) |
---|
28 | ;; We only need a few constants from <linux/futex.h>, which may |
---|
29 | ;; not have been included in the :libc .cdb files. |
---|
30 | (defconstant FUTEX-WAIT 0) |
---|
31 | (defconstant FUTEX-WAKE 1) |
---|
32 | (defconstant futex-avail 0) |
---|
33 | (defconstant futex-locked 1) |
---|
34 | (defconstant futex-contended 2) |
---|
35 | (declaim (inline %lock-futex %unlock-futex))) |
---|
36 | |
---|
37 | ;;; Miscellany. |
---|
38 | |
---|
39 | (defun memq (item list) |
---|
40 | (do* ((tail list (%cdr tail))) |
---|
41 | ((null tail)) |
---|
42 | (if (eq item (car tail)) |
---|
43 | (return tail)))) |
---|
44 | |
---|
45 | (defun %copy-u8-to-string (u8-vector source-idx string dest-idx n) |
---|
46 | (declare (optimize (speed 3) (safety 0)) |
---|
47 | (fixnum source-idx dest-idx n) |
---|
48 | (type (simple-array (unsigned-byte 8) (*)) u8-vector) |
---|
49 | (simple-base-string string)) |
---|
50 | (do* ((i 0 (1+ i))) |
---|
51 | ((= i n) string) |
---|
52 | (declare (fixnum i)) |
---|
53 | (setf (%scharcode string dest-idx) (aref u8-vector source-idx)) |
---|
54 | (incf source-idx) |
---|
55 | (incf dest-idx))) |
---|
56 | |
---|
57 | (defun %copy-string-to-u8 (string source-idx u8-vector dest-idx n) |
---|
58 | (declare (optimize (speed 3) (safety 0)) |
---|
59 | (fixnum source-idx dest-idx n) |
---|
60 | (type (simple-array (unsigned-byte 8) (*)) u8-vector) |
---|
61 | (simple-base-string string)) |
---|
62 | (do* ((i 0 (1+ i))) |
---|
63 | ((= i n) u8-vector) |
---|
64 | (declare (fixnum i)) |
---|
65 | (let* ((code (%scharcode string source-idx))) |
---|
66 | (declare (type (mod #x11000) code)) |
---|
67 | (if (> code #xff) |
---|
68 | (setq code (char-code #\Sub))) |
---|
69 | (setf (aref u8-vector dest-idx) code) |
---|
70 | (incf source-idx) |
---|
71 | (incf dest-idx)))) |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | (defun append-2 (y z) |
---|
77 | (if (null y) |
---|
78 | z |
---|
79 | (let* ((new (cons (car y) nil)) |
---|
80 | (tail new)) |
---|
81 | (declare (list new tail)) |
---|
82 | (dolist (head (cdr y)) |
---|
83 | (setq tail (cdr (rplacd tail (cons head nil))))) |
---|
84 | (rplacd tail z) |
---|
85 | new))) |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | (defun dbg (&optional arg) |
---|
96 | (dbg arg)) |
---|
97 | |
---|
98 | |
---|
99 | ; This takes a simple-base-string and passes a C string into |
---|
100 | ; the kernel "Bug" routine. Not too fancy, but neither is #_DebugStr, |
---|
101 | ; and there's a better chance that users would see this message. |
---|
102 | (defun bug (arg) |
---|
103 | (if (typep arg 'simple-base-string) |
---|
104 | #+x86-target |
---|
105 | (debug-trap-with-string arg) |
---|
106 | #-x86-target |
---|
107 | (let* ((len (length arg))) |
---|
108 | (%stack-block ((buf (1+ len))) |
---|
109 | (%cstr-pointer arg buf) |
---|
110 | (ff-call |
---|
111 | (%kernel-import target::kernel-import-lisp-bug) |
---|
112 | :address buf |
---|
113 | :void))) |
---|
114 | (bug "Bug called with non-simple-base-string."))) |
---|
115 | |
---|
116 | (defun total-bytes-allocated () |
---|
117 | (%heap-bytes-allocated) |
---|
118 | #+not-any-more |
---|
119 | (+ (unsignedwide->integer *total-bytes-freed*) |
---|
120 | (%heap-bytes-allocated))) |
---|
121 | |
---|
122 | (defun %freebytes () |
---|
123 | (with-macptrs (p) |
---|
124 | (%setf-macptr-to-object p |
---|
125 | (%fixnum-ref (%get-kernel-global 'all-areas) |
---|
126 | target::area.succ)) |
---|
127 | (- (%get-natural p target::area.high) |
---|
128 | (%get-natural p target::area.active)))) |
---|
129 | |
---|
130 | (defun %reservedbytes () |
---|
131 | (with-macptrs (p) |
---|
132 | (%setf-macptr-to-object p (%get-kernel-global 'all-areas)) |
---|
133 | (- #+32-bit-target |
---|
134 | (%get-unsigned-long p target::area.high) |
---|
135 | #+64-bit-target |
---|
136 | (%%get-unsigned-longlong p target::area.high) |
---|
137 | #+32-bit-target |
---|
138 | (%get-unsigned-long p target::area.low) |
---|
139 | #+64-bit-target |
---|
140 | (%%get-unsigned-longlong p target::area.low)))) |
---|
141 | |
---|
142 | (defun object-in-application-heap-p (address) |
---|
143 | (declare (ignore address)) |
---|
144 | t) |
---|
145 | |
---|
146 | (defun frozen-space-dnodes () |
---|
147 | "Returns the current size of the frozen area." |
---|
148 | (%fixnum-ref-natural (%get-kernel-global 'tenured-area) |
---|
149 | target::area.static-dnodes)) |
---|
150 | (defun %usedbytes () |
---|
151 | (with-lock-grabbed (*kernel-exception-lock*) |
---|
152 | (with-lock-grabbed (*kernel-tcr-area-lock*) |
---|
153 | (%normalize-areas) |
---|
154 | (let ((static 0) |
---|
155 | (dynamic 0) |
---|
156 | (library 0)) |
---|
157 | (do-consing-areas (area) |
---|
158 | (let* ((active (%fixnum-ref area target::area.active)) |
---|
159 | (bytes (ash (- active |
---|
160 | (%fixnum-ref area target::area.low)) |
---|
161 | target::fixnumshift)) |
---|
162 | (code (%fixnum-ref area target::area.code))) |
---|
163 | (when (object-in-application-heap-p active) |
---|
164 | (if (eql code area-dynamic) |
---|
165 | (incf dynamic bytes) |
---|
166 | (if (eql code area-managed-static) |
---|
167 | (incf library bytes) |
---|
168 | (incf static bytes)))))) |
---|
169 | (let* ((frozen-size (ash (frozen-space-dnodes) target::dnode-shift))) |
---|
170 | (decf dynamic frozen-size) |
---|
171 | (values dynamic static library frozen-size)))))) |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | (defun %stack-space () |
---|
176 | (%normalize-areas) |
---|
177 | (let ((free 0) |
---|
178 | (used 0)) |
---|
179 | (with-macptrs (p) |
---|
180 | (do-gc-areas (area) |
---|
181 | (when (member (%fixnum-ref area target::area.code) |
---|
182 | '(#.area-vstack |
---|
183 | #.area-cstack |
---|
184 | #.area-tstack)) |
---|
185 | (%setf-macptr-to-object p area) |
---|
186 | (let ((active |
---|
187 | #+32-bit-target |
---|
188 | (%get-unsigned-long p target::area.active) |
---|
189 | #+64-bit-target |
---|
190 | (%%get-unsigned-longlong p target::area.active)) |
---|
191 | (high |
---|
192 | #+32-bit-target |
---|
193 | (%get-unsigned-long p target::area.high) |
---|
194 | #+64-bit-target |
---|
195 | (%%get-unsigned-longlong p target::area.high)) |
---|
196 | (low |
---|
197 | #+32-bit-target |
---|
198 | (%get-unsigned-long p target::area.low) |
---|
199 | #+64-bit-target |
---|
200 | (%%get-unsigned-longlong p target::area.low))) |
---|
201 | (incf used (- high active)) |
---|
202 | (incf free (- active low)))))) |
---|
203 | (values (+ free used) used free))) |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | ; Returns an alist of the form: |
---|
208 | ; ((thread cstack-free cstack-used vstack-free vstack-used tstack-free tstack-used) |
---|
209 | ; ...) |
---|
210 | (defun %stack-space-by-lisp-thread () |
---|
211 | (let* ((res nil)) |
---|
212 | (without-interrupts |
---|
213 | (dolist (p (all-processes)) |
---|
214 | (let* ((thread (process-thread p))) |
---|
215 | (when thread |
---|
216 | (push (cons thread (multiple-value-list (%thread-stack-space thread))) res))))) |
---|
217 | res)) |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | ;;; Returns six values on most platforms, 4 on ARM. |
---|
222 | ;;; sp free |
---|
223 | ;;; sp used |
---|
224 | ;;; vsp free |
---|
225 | ;;; vsp used |
---|
226 | ;;; tsp free (not on ARM) |
---|
227 | ;;; tsp used (not on ARM) |
---|
228 | (defun %thread-stack-space (&optional (thread *current-lisp-thread*)) |
---|
229 | (when (eq thread *current-lisp-thread*) |
---|
230 | (%normalize-areas)) |
---|
231 | (labels ((free-and-used (area) |
---|
232 | (with-macptrs (p) |
---|
233 | (%setf-macptr-to-object p area) |
---|
234 | (let* ((low |
---|
235 | #+32-bit-target |
---|
236 | (%get-unsigned-long p target::area.low) |
---|
237 | #+64-bit-target |
---|
238 | (%%get-unsigned-longlong p target::area.low)) |
---|
239 | (high |
---|
240 | #+32-bit-target |
---|
241 | (%get-unsigned-long p target::area.high) |
---|
242 | #+64-bit-target |
---|
243 | (%%get-unsigned-longlong p target::area.high)) |
---|
244 | (active |
---|
245 | #+32-bit-target |
---|
246 | (%get-unsigned-long p target::area.active) |
---|
247 | #+64-bit-target |
---|
248 | (%%get-unsigned-longlong p target::area.active)) |
---|
249 | (free (- active low)) |
---|
250 | (used (- high active))) |
---|
251 | (loop |
---|
252 | (setq area (%fixnum-ref area target::area.older)) |
---|
253 | (when (eql area 0) (return)) |
---|
254 | (%setf-macptr-to-object p area) |
---|
255 | (let ((low |
---|
256 | #+32-bit-target |
---|
257 | (%get-unsigned-long p target::area.low) |
---|
258 | #+64-bit-target |
---|
259 | (%%get-unsigned-longlong p target::area.low)) |
---|
260 | (high |
---|
261 | #+32-bit-target |
---|
262 | (%get-unsigned-long p target::area.high) |
---|
263 | #+64-bit-target |
---|
264 | (%%get-unsigned-longlong p target::area.high))) |
---|
265 | (declare (fixnum low high)) |
---|
266 | (incf used (- high low)))) |
---|
267 | (values free used))))) |
---|
268 | (let* ((tcr (lisp-thread.tcr thread))) |
---|
269 | (if (or (null tcr) |
---|
270 | (zerop (%fixnum-ref (%fixnum-ref tcr target::tcr.cs-area)))) |
---|
271 | (values 0 0 0 0 0 0) |
---|
272 | (multiple-value-bind (cf cu) (free-and-used (%fixnum-ref tcr target::tcr.cs-area)) |
---|
273 | (multiple-value-bind (vf vu) (free-and-used (%fixnum-ref tcr target::tcr.vs-area)) |
---|
274 | #+arm-target |
---|
275 | (values cf cu vf vu) |
---|
276 | #-arm-target |
---|
277 | (multiple-value-bind (tf tu) (free-and-used (%fixnum-ref tcr target::tcr.ts-area )) |
---|
278 | (values cf cu vf vu tf tu)))))))) |
---|
279 | |
---|
280 | |
---|
281 | (defun room (&optional (verbose :default)) |
---|
282 | "Print to *STANDARD-OUTPUT* information about the state of internal |
---|
283 | storage and its management. The optional argument controls the |
---|
284 | verbosity of output. If it is T, ROOM prints out a maximal amount of |
---|
285 | information. If it is NIL, ROOM prints out a minimal amount of |
---|
286 | information. If it is :DEFAULT or it is not supplied, ROOM prints out |
---|
287 | an intermediate amount of information." |
---|
288 | (let* ((freebytes nil) |
---|
289 | (usedbytes nil) |
---|
290 | (static-used nil) |
---|
291 | (staticlib-used nil) |
---|
292 | (frozen-space-size nil) |
---|
293 | (lispheap nil) |
---|
294 | (reserved nil) |
---|
295 | (static nil) |
---|
296 | (stack-total) |
---|
297 | (stack-used) |
---|
298 | (stack-free) |
---|
299 | (static-cons-reserved nil) |
---|
300 | (stack-used-by-thread nil)) |
---|
301 | (progn |
---|
302 | (progn |
---|
303 | (setq freebytes (%freebytes)) |
---|
304 | (when verbose |
---|
305 | (multiple-value-setq (usedbytes static-used staticlib-used frozen-space-size) |
---|
306 | (%usedbytes)) |
---|
307 | (setq lispheap (+ freebytes usedbytes) |
---|
308 | reserved (%reservedbytes) |
---|
309 | static (+ static-used staticlib-used frozen-space-size)) |
---|
310 | (multiple-value-setq (stack-total stack-used stack-free) |
---|
311 | (%stack-space)) |
---|
312 | (unless (eq verbose :default) |
---|
313 | (setq stack-used-by-thread (%stack-space-by-lisp-thread)))))) |
---|
314 | (format t "~&Approximately ~:D bytes of memory can be allocated ~%before the next full GC is triggered. ~%" freebytes) |
---|
315 | (when verbose |
---|
316 | (flet ((k (n) (round n 1024))) |
---|
317 | (princ " |
---|
318 | Total Size Free Used") |
---|
319 | (format t "~&Lisp Heap:~15t~10D (~DK)~35t~10D (~DK)~55t~10D (~DK)" |
---|
320 | lispheap (k lispheap) |
---|
321 | freebytes (k freebytes) |
---|
322 | usedbytes (k usedbytes)) |
---|
323 | (format t "~&Stacks:~15t~10D (~DK)~35t~10D (~DK)~55t~10D (~DK)" |
---|
324 | stack-total (k stack-total) |
---|
325 | stack-free (k stack-free) |
---|
326 | stack-used (k stack-used)) |
---|
327 | (format t "~&Static:~15t~10D (~DK)~35t~10D (~DK)~55t~10D (~DK)" |
---|
328 | static (k static) |
---|
329 | 0 0 |
---|
330 | static (k static)) |
---|
331 | (when (and frozen-space-size (not (zerop frozen-space-size))) |
---|
332 | (setq static-cons-reserved (ash (reserved-static-conses) target::dnode-shift) |
---|
333 | frozen-space-size (- frozen-space-size static-cons-reserved)) |
---|
334 | (unless (zerop static-cons-reserved) |
---|
335 | (format t "~&~,3f MB of reserved static conses (~d free, ~d reserved)" |
---|
336 | (/ static-cons-reserved (float (ash 1 20))) |
---|
337 | (free-static-conses) |
---|
338 | (reserved-static-conses))) |
---|
339 | |
---|
340 | (unless (zerop frozen-space-size) |
---|
341 | (format t "~&~,3f MB of static memory is \"frozen\" dynamic memory" |
---|
342 | (/ frozen-space-size (float (ash 1 20)))))) |
---|
343 | (format t "~&~,3f MB reserved for heap expansion." |
---|
344 | (/ reserved (float (ash 1 20)))) |
---|
345 | (unless (eq verbose :default) |
---|
346 | (terpri) |
---|
347 | (let* ((processes (all-processes))) |
---|
348 | (dolist (thread-info stack-used-by-thread) |
---|
349 | (destructuring-bind (thread sp-free sp-used vsp-free vsp-used #-arm-target tsp-free #-arm-target tsp-used) |
---|
350 | thread-info |
---|
351 | (let* ((process (dolist (p processes) |
---|
352 | (when (eq (process-thread p) thread) |
---|
353 | (return p))))) |
---|
354 | (when process |
---|
355 | (let ((sp-total (+ sp-used sp-free)) |
---|
356 | (vsp-total (+ vsp-used vsp-free)) |
---|
357 | #-arm-target |
---|
358 | (tsp-total (+ tsp-used tsp-free))) |
---|
359 | (format t "~%~a(~d)~% cstack:~12T~10D (~DK) ~33T~10D (~DK) ~54T~10D (~DK)~ |
---|
360 | ~% vstack:~12T~10D (~DK) ~33T~10D (~DK) ~54T~10D (~DK)" |
---|
361 | (process-name process) |
---|
362 | (process-serial-number process) |
---|
363 | sp-total (k sp-total) sp-free (k sp-free) sp-used (k sp-used) |
---|
364 | vsp-total (k vsp-total) vsp-free (k vsp-free) vsp-used (k vsp-used)) |
---|
365 | #-arm-target |
---|
366 | (format t |
---|
367 | "~% tstack:~12T~10D (~DK) ~33T~10D (~DK) ~54T~10D (~DK)" |
---|
368 | |
---|
369 | tsp-total (k tsp-total) tsp-free (k tsp-free) tsp-used (k tsp-used))))))))))))) |
---|
370 | |
---|
371 | |
---|
372 | (defun list-length (l) |
---|
373 | "Return the length of the given LIST, or NIL if the LIST is circular." |
---|
374 | (do* ((n 0 (+ n 2)) |
---|
375 | (fast l (cddr fast)) |
---|
376 | (slow l (cdr slow))) |
---|
377 | ((null fast) n) |
---|
378 | (declare (fixnum n)) |
---|
379 | (if (null (cdr fast)) |
---|
380 | (return (the fixnum (1+ n))) |
---|
381 | (if (and (eq fast slow) |
---|
382 | (> n 0)) |
---|
383 | (return nil))))) |
---|
384 | |
---|
385 | (defun proper-list-p (l) |
---|
386 | (and (typep l 'list) |
---|
387 | (do* ((n 0 (+ n 2)) |
---|
388 | (fast l (if (and (listp fast) (listp (cdr fast))) |
---|
389 | (cddr fast) |
---|
390 | (return-from proper-list-p nil))) |
---|
391 | (slow l (cdr slow))) |
---|
392 | ((null fast) n) |
---|
393 | (declare (fixnum n)) |
---|
394 | (if (atom fast) |
---|
395 | (return nil) |
---|
396 | (if (null (cdr fast)) |
---|
397 | (return t) |
---|
398 | (if (and (eq fast slow) |
---|
399 | (> n 0)) |
---|
400 | (return nil))))))) |
---|
401 | |
---|
402 | (defun proper-sequence-p (x) |
---|
403 | (cond ((typep x 'vector)) |
---|
404 | ((typep x 'list) (not (null (list-length x)))))) |
---|
405 | |
---|
406 | |
---|
407 | (defun length (seq) |
---|
408 | "Return an integer that is the length of SEQUENCE." |
---|
409 | (seq-dispatch |
---|
410 | seq |
---|
411 | (or (list-length seq) |
---|
412 | (%err-disp $XIMPROPERLIST seq)) |
---|
413 | (if (= (the fixnum (typecode seq)) target::subtag-vectorH) |
---|
414 | (%svref seq target::vectorH.logsize-cell) |
---|
415 | (uvsize seq)))) |
---|
416 | |
---|
417 | (defun %str-from-ptr (pointer len &optional (dest (make-string len))) |
---|
418 | (declare (fixnum len) |
---|
419 | (optimize (speed 3) (safety 0))) |
---|
420 | (dotimes (i len dest) |
---|
421 | (setf (%scharcode dest i) (%get-unsigned-byte pointer i)))) |
---|
422 | |
---|
423 | (defun %get-cstring (pointer) |
---|
424 | (do* ((end 0 (1+ end))) |
---|
425 | ((zerop (the (unsigned-byte 8) (%get-unsigned-byte pointer end))) |
---|
426 | (%str-from-ptr pointer end)) |
---|
427 | (declare (fixnum end)))) |
---|
428 | |
---|
429 | (defun %get-utf-8-cstring (pointer) |
---|
430 | (do* ((end 0 (1+ end))) |
---|
431 | ((zerop (the (unsigned-byte 8) (%get-unsigned-byte pointer end))) |
---|
432 | (let* ((len (utf-8-length-of-memory-encoding pointer end 0)) |
---|
433 | (string (make-string len))) |
---|
434 | (utf-8-memory-decode pointer end 0 string) |
---|
435 | string)) |
---|
436 | (declare (fixnum end)))) |
---|
437 | |
---|
438 | ;;; Assumes that pointer is terminated by a 0-valued 16-bit word |
---|
439 | ;;; and that it points to a valid utf-16 string with native endianness. |
---|
440 | (defun %get-native-utf-16-cstring (pointer) |
---|
441 | (do* ((nchars 0 (1+ nchars)) |
---|
442 | (i 0 (+ i 2)) |
---|
443 | (code (%get-unsigned-word pointer i) (%get-unsigned-word pointer i))) |
---|
444 | ((zerop code) |
---|
445 | (do* ((string (make-string nchars)) |
---|
446 | (out 0 (1+ out)) |
---|
447 | (i 0 (+ i 2))) |
---|
448 | ((= out nchars) string) |
---|
449 | (declare (fixnum i out)) |
---|
450 | (let* ((code (%get-unsigned-word pointer i))) |
---|
451 | (declare (type (unsigned-byte 16) code)) |
---|
452 | (when (and (>= code #xd800) |
---|
453 | (< code #xdc00)) |
---|
454 | (incf i 2) |
---|
455 | (let* ((code2 (%get-unsigned-word pointer i))) |
---|
456 | (declare (type (unsigned-byte 16) code2)) |
---|
457 | (setq code (utf-16-combine-surrogate-pairs code code2)))) |
---|
458 | (setf (schar string out) (code-char code))))) |
---|
459 | (when (and (>= code #xd800) (< code #xdc00)) |
---|
460 | (incf i 2)))) |
---|
461 | |
---|
462 | |
---|
463 | ;;; This is mostly here so we can bootstrap shared libs without |
---|
464 | ;;; having to bootstrap #_strcmp. |
---|
465 | ;;; Return true if the cstrings are equal, false otherwise. |
---|
466 | (defun %cstrcmp (x y) |
---|
467 | (do* ((i 0 (1+ i)) |
---|
468 | (bx (%get-byte x i) (%get-byte x i)) |
---|
469 | (by (%get-byte y i) (%get-byte y i))) |
---|
470 | ((not (= bx by))) |
---|
471 | (declare (fixnum i bx by)) |
---|
472 | (when (zerop bx) |
---|
473 | (return t)))) |
---|
474 | |
---|
475 | (defun %cnstrcmp (x y n) |
---|
476 | (declare (fixnum n)) |
---|
477 | (do* ((i 0 (1+ i)) |
---|
478 | (bx (%get-byte x i) (%get-byte x i)) |
---|
479 | (by (%get-byte y i) (%get-byte y i))) |
---|
480 | ((= i n) t) |
---|
481 | (declare (fixnum i bx by)) |
---|
482 | (unless (= bx by) |
---|
483 | (return)))) |
---|
484 | |
---|
485 | (defvar %documentation nil) |
---|
486 | |
---|
487 | (defvar %documentation-lock% nil) |
---|
488 | |
---|
489 | (setq %documentation |
---|
490 | (make-hash-table :weak t :size 100 :test 'eq :rehash-threshold .95) |
---|
491 | %documentation-lock% (make-lock)) |
---|
492 | |
---|
493 | (defun %put-documentation (thing doc-id doc) |
---|
494 | (with-lock-grabbed (%documentation-lock%) |
---|
495 | (let* ((info (gethash thing %documentation)) |
---|
496 | (pair (assoc doc-id info))) |
---|
497 | (if doc |
---|
498 | (progn |
---|
499 | (unless (typep doc 'string) |
---|
500 | (report-bad-arg doc 'string)) |
---|
501 | (if pair |
---|
502 | (setf (cdr pair) doc) |
---|
503 | (setf (gethash thing %documentation) (cons (cons doc-id doc) info)))) |
---|
504 | (when pair |
---|
505 | (if (setq info (nremove pair info)) |
---|
506 | (setf (gethash thing %documentation) info) |
---|
507 | (remhash thing %documentation)))))) |
---|
508 | doc) |
---|
509 | |
---|
510 | (defun %get-documentation (object doc-id) |
---|
511 | (cdr (assoc doc-id (gethash object %documentation)))) |
---|
512 | |
---|
513 | ;;; This pretends to be (SETF DOCUMENTATION), until that generic function |
---|
514 | ;;; is defined. It handles a few common cases. |
---|
515 | (defun %set-documentation (thing doc-id doc-string) |
---|
516 | (case doc-id |
---|
517 | (function |
---|
518 | (if (typep thing 'function) |
---|
519 | (%put-documentation thing t doc-string) |
---|
520 | (if (typep thing 'symbol) |
---|
521 | (let* ((def (fboundp thing))) |
---|
522 | (if def |
---|
523 | (%put-documentation def t doc-string))) |
---|
524 | (if (setf-function-name-p thing) |
---|
525 | (%set-documentation |
---|
526 | (setf-function-name thing) doc-id doc-string))))) |
---|
527 | (variable |
---|
528 | (if (typep thing 'symbol) |
---|
529 | (%put-documentation thing doc-id doc-string))) |
---|
530 | (t (%put-documentation thing doc-id doc-string))) |
---|
531 | doc-string) |
---|
532 | |
---|
533 | |
---|
534 | (%fhave 'set-documentation #'%set-documentation) |
---|
535 | |
---|
536 | |
---|
537 | |
---|
538 | ;;; This is intended for use by debugging tools. It's a horrible thing |
---|
539 | ;;; to do otherwise. The caller really needs to hold the heap-segment |
---|
540 | ;;; lock; this grabs the tcr queue lock as well. |
---|
541 | |
---|
542 | |
---|
543 | (defparameter *spin-lock-tries* 1) |
---|
544 | (defparameter *spin-lock-timeouts* 0) |
---|
545 | |
---|
546 | #+(and (not futex) (not x86-target)) |
---|
547 | (defun %get-spin-lock (p) |
---|
548 | (let* ((self (%current-tcr)) |
---|
549 | (n *spin-lock-tries*)) |
---|
550 | (declare (fixnum n)) |
---|
551 | (loop |
---|
552 | (dotimes (i n) |
---|
553 | (when (eql 0 (%ptr-store-fixnum-conditional p 0 self)) |
---|
554 | (return-from %get-spin-lock t))) |
---|
555 | (%atomic-incf-node 1 '*spin-lock-timeouts* target::symbol.vcell) |
---|
556 | (yield)))) |
---|
557 | |
---|
558 | (eval-when (:compile-toplevel :execute) |
---|
559 | (declaim (inline note-lock-wait note-lock-held note-lock-released))) |
---|
560 | |
---|
561 | |
---|
562 | |
---|
563 | |
---|
564 | |
---|
565 | #-futex |
---|
566 | (defun %lock-recursive-lock-object (lock &optional flag) |
---|
567 | (let* ((ptr (recursive-lock-ptr lock))) |
---|
568 | (with-macptrs ((p) |
---|
569 | (owner (%get-ptr ptr target::lockptr.owner)) |
---|
570 | (signal (%get-ptr ptr target::lockptr.signal)) |
---|
571 | (spin (%inc-ptr ptr target::lockptr.spinlock))) |
---|
572 | (%setf-macptr-to-object p (%current-tcr)) |
---|
573 | (if (istruct-typep flag 'lock-acquisition) |
---|
574 | (setf (lock-acquisition.status flag) nil) |
---|
575 | (if flag (report-bad-arg flag 'lock-acquisition))) |
---|
576 | (loop |
---|
577 | (without-interrupts |
---|
578 | (when (eql p owner) |
---|
579 | (incf (%get-natural ptr target::lockptr.count)) |
---|
580 | (when flag |
---|
581 | (setf (lock-acquisition.status flag) t)) |
---|
582 | (return t)) |
---|
583 | (%get-spin-lock spin) |
---|
584 | (when (eql 1 (incf (%get-natural ptr target::lockptr.avail))) |
---|
585 | (setf (%get-ptr ptr target::lockptr.owner) p |
---|
586 | (%get-natural ptr target::lockptr.count) 1) |
---|
587 | (setf (%get-natural spin 0) 0) |
---|
588 | (if flag |
---|
589 | (setf (lock-acquisition.status flag) t)) |
---|
590 | (return t)) |
---|
591 | (setf (%get-natural spin 0) 0)) |
---|
592 | (%process-wait-on-semaphore-ptr signal 1 0 (recursive-lock-whostate lock)))))) |
---|
593 | |
---|
594 | |
---|
595 | |
---|
596 | #+futex |
---|
597 | (progn |
---|
598 | #-monitor-futex-wait |
---|
599 | (defun futex-wait (p val whostate) |
---|
600 | (with-process-whostate (whostate) |
---|
601 | (int-errno-ffcall |
---|
602 | (%kernel-import target::kernel-import-lisp-futex) |
---|
603 | :address p :int FUTEX-WAIT :int val :address (%null-ptr) :address (%null-ptr) :int 0 :int))) |
---|
604 | #+monitor-futex-wait |
---|
605 | (progn |
---|
606 | (defparameter *total-futex-wait-calls* 0) |
---|
607 | (defparameter *total-futex-wait-times* 0) |
---|
608 | (defun futex-wait (p val whostate) |
---|
609 | (with-process-whostate (whostate) |
---|
610 | (let* ((start (get-internal-real-time))) |
---|
611 | (incf *total-futex-wait-calls*) |
---|
612 | (int-errno-ffcall |
---|
613 | (%kernel-import target::kernel-import-lisp-futex) |
---|
614 | :address p :int FUTEX-WAIT :int val :address (%null-ptr) :address (%null-ptr) :int 0 :int) |
---|
615 | (incf *total-futex-wait-times* (- (get-internal-real-time) start))))))) |
---|
616 | |
---|
617 | |
---|
618 | |
---|
619 | |
---|
620 | #+futex |
---|
621 | (defun futex-wake (p n) |
---|
622 | (int-errno-ffcall (%kernel-import target::kernel-import-lisp-futex) |
---|
623 | :address p :int FUTEX-WAKE :int n :address (%null-ptr) :address (%null-ptr) :int 0 :int)) |
---|
624 | |
---|
625 | #+futex |
---|
626 | (defun %lock-futex (p wait-level lock fwhostate) |
---|
627 | (let* ((val (%ptr-store-conditional p futex-avail futex-locked))) |
---|
628 | (declare (fixnum val)) |
---|
629 | (or (eql val futex-avail) |
---|
630 | (loop |
---|
631 | (if (eql val futex-contended) |
---|
632 | (let* ((*interrupt-level* wait-level)) |
---|
633 | (futex-wait p val (if fwhostate (funcall fwhostate lock) "futex wait"))) |
---|
634 | (setq val futex-contended)) |
---|
635 | (when (eql futex-avail (xchgl val p)) |
---|
636 | (return t)))))) |
---|
637 | |
---|
638 | #+futex |
---|
639 | (defun %unlock-futex (p) |
---|
640 | (unless (eql futex-avail (%atomic-decf-ptr p)) |
---|
641 | (setf (%get-natural p target::lockptr.avail) futex-avail) |
---|
642 | (futex-wake p #$INT_MAX))) |
---|
643 | |
---|
644 | |
---|
645 | |
---|
646 | |
---|
647 | #+futex |
---|
648 | (defun %lock-recursive-lock-object (lock &optional flag) |
---|
649 | (if (istruct-typep flag 'lock-acquisition) |
---|
650 | (setf (lock-acquisition.status flag) nil) |
---|
651 | (if flag (report-bad-arg flag 'lock-acquisition))) |
---|
652 | (let* ((self (%current-tcr)) |
---|
653 | (level *interrupt-level*) |
---|
654 | (ptr (recursive-lock-ptr lock))) |
---|
655 | (declare (fixnum self)) |
---|
656 | (without-interrupts |
---|
657 | (cond ((eql self (%get-object ptr target::lockptr.owner)) |
---|
658 | (incf (%get-natural ptr target::lockptr.count))) |
---|
659 | (t (%lock-futex ptr level lock #'recursive-lock-whostate) |
---|
660 | (%set-object ptr target::lockptr.owner self) |
---|
661 | (setf (%get-natural ptr target::lockptr.count) 1))) |
---|
662 | (when flag |
---|
663 | (setf (lock-acquisition.status flag) t)) |
---|
664 | t))) |
---|
665 | |
---|
666 | |
---|
667 | |
---|
668 | |
---|
669 | |
---|
670 | |
---|
671 | #-futex |
---|
672 | (defun %try-recursive-lock-object (lock &optional flag) |
---|
673 | (let* ((ptr (recursive-lock-ptr lock))) |
---|
674 | (with-macptrs ((p) |
---|
675 | (owner (%get-ptr ptr target::lockptr.owner)) |
---|
676 | (spin (%inc-ptr ptr target::lockptr.spinlock))) |
---|
677 | (%setf-macptr-to-object p (%current-tcr)) |
---|
678 | (if flag |
---|
679 | (if (istruct-typep flag 'lock-acquisition) |
---|
680 | (setf (lock-acquisition.status flag) nil) |
---|
681 | (report-bad-arg flag 'lock-acquisition))) |
---|
682 | (without-interrupts |
---|
683 | (cond ((eql p owner) |
---|
684 | (incf (%get-natural ptr target::lockptr.count)) |
---|
685 | (if flag (setf (lock-acquisition.status flag) t)) |
---|
686 | t) |
---|
687 | (t |
---|
688 | (let* ((win nil)) |
---|
689 | (%get-spin-lock spin) |
---|
690 | (when (setq win (eql 1 (incf (%get-natural ptr target::lockptr.avail)))) |
---|
691 | (setf (%get-ptr ptr target::lockptr.owner) p |
---|
692 | (%get-natural ptr target::lockptr.count) 1) |
---|
693 | (if flag (setf (lock-acquisition.status flag) t))) |
---|
694 | (setf (%get-ptr spin) (%null-ptr)) |
---|
695 | win))))))) |
---|
696 | |
---|
697 | |
---|
698 | |
---|
699 | #+futex |
---|
700 | (defun %try-recursive-lock-object (lock &optional flag) |
---|
701 | (let* ((self (%current-tcr)) |
---|
702 | (ptr (recursive-lock-ptr lock))) |
---|
703 | (declare (fixnum self)) |
---|
704 | (if flag |
---|
705 | (if (istruct-typep flag 'lock-acquisition) |
---|
706 | (setf (lock-acquisition.status flag) nil) |
---|
707 | (report-bad-arg flag 'lock-acquisition))) |
---|
708 | (without-interrupts |
---|
709 | (cond ((eql (%get-object ptr target::lockptr.owner) self) |
---|
710 | (incf (%get-natural ptr target::lockptr.count)) |
---|
711 | (if flag (setf (lock-acquisition.status flag) t)) |
---|
712 | t) |
---|
713 | (t |
---|
714 | (when (eql 0 (%ptr-store-conditional ptr futex-avail futex-locked)) |
---|
715 | (%set-object ptr target::lockptr.owner self) |
---|
716 | (setf (%get-natural ptr target::lockptr.count) 1) |
---|
717 | (if flag (setf (lock-acquisition.status flag) t)) |
---|
718 | t)))))) |
---|
719 | |
---|
720 | |
---|
721 | |
---|
722 | |
---|
723 | |
---|
724 | #-futex |
---|
725 | (defun %unlock-recursive-lock-object (lock) |
---|
726 | (let* ((ptr (%svref lock target::lock._value-cell))) |
---|
727 | (with-macptrs ((signal (%get-ptr ptr target::lockptr.signal)) |
---|
728 | (spin (%inc-ptr ptr target::lockptr.spinlock))) |
---|
729 | (unless (eql (%get-object ptr target::lockptr.owner) (%current-tcr)) |
---|
730 | (error 'not-lock-owner :lock lock)) |
---|
731 | (without-interrupts |
---|
732 | (when (eql 0 (decf (the fixnum |
---|
733 | (%get-natural ptr target::lockptr.count)))) |
---|
734 | (%get-spin-lock spin) |
---|
735 | (setf (%get-ptr ptr target::lockptr.owner) (%null-ptr)) |
---|
736 | (let* ((pending (+ (the fixnum |
---|
737 | (1- (the fixnum (%get-fixnum ptr target::lockptr.avail)))) |
---|
738 | (the fixnum (%get-fixnum ptr target::lockptr.waiting))))) |
---|
739 | (declare (fixnum pending)) |
---|
740 | (setf (%get-natural ptr target::lockptr.avail) 0 |
---|
741 | (%get-natural ptr target::lockptr.waiting) 0) |
---|
742 | (decf pending) |
---|
743 | (if (> pending 0) |
---|
744 | (setf (%get-natural ptr target::lockptr.waiting) pending)) |
---|
745 | (setf (%get-ptr spin) (%null-ptr)) |
---|
746 | (if (>= pending 0) |
---|
747 | (%signal-semaphore-ptr signal))))))) |
---|
748 | nil) |
---|
749 | |
---|
750 | |
---|
751 | |
---|
752 | #+futex |
---|
753 | (defun %unlock-recursive-lock-object (lock) |
---|
754 | (let* ((ptr (%svref lock target::lock._value-cell))) |
---|
755 | (unless (eql (%get-object ptr target::lockptr.owner) (%current-tcr)) |
---|
756 | (error 'not-lock-owner :lock lock)) |
---|
757 | (without-interrupts |
---|
758 | (when (eql 0 (decf (the fixnum |
---|
759 | (%get-natural ptr target::lockptr.count)))) |
---|
760 | (setf (%get-natural ptr target::lockptr.owner) 0) |
---|
761 | (%unlock-futex ptr)))) |
---|
762 | nil) |
---|
763 | |
---|
764 | |
---|
765 | |
---|
766 | |
---|
767 | (defun %%lock-owner (lock) |
---|
768 | "Intended for debugging only; ownership may change while this code |
---|
769 | is running." |
---|
770 | (let* ((tcr (%get-object (recursive-lock-ptr lock) target::lockptr.owner))) |
---|
771 | (unless (zerop tcr) |
---|
772 | (tcr->process tcr)))) |
---|
773 | |
---|
774 | |
---|
775 | |
---|
776 | |
---|
777 | |
---|
778 | |
---|
779 | |
---|
780 | (defun %rplaca-conditional (cons-cell old new) |
---|
781 | (%store-node-conditional target::cons.car cons-cell old new)) |
---|
782 | |
---|
783 | (defun %rplacd-conditional (cons-cell old new) |
---|
784 | (%store-node-conditional target::cons.cdr cons-cell old new)) |
---|
785 | |
---|
786 | ;;; Atomically push NEW onto the list in the I'th cell of uvector V. |
---|
787 | |
---|
788 | (defun atomic-push-uvector-cell (v i new) |
---|
789 | (let* ((cell (cons new nil)) |
---|
790 | (offset (+ target::misc-data-offset (ash i target::word-shift)))) |
---|
791 | (loop |
---|
792 | (let* ((old (%svref v i))) |
---|
793 | (rplacd cell old) |
---|
794 | (when (%store-node-conditional offset v old cell) |
---|
795 | (return cell)))))) |
---|
796 | |
---|
797 | (defun atomic-pop-uvector-cell (v i) |
---|
798 | (let* ((offset (+ target::misc-data-offset (ash i target::word-shift)))) |
---|
799 | (loop |
---|
800 | (let* ((old (%svref v i))) |
---|
801 | (if (null old) |
---|
802 | (return (values nil nil)) |
---|
803 | (let* ((tail (cdr old))) |
---|
804 | (when (%store-node-conditional offset v old tail) |
---|
805 | (return (values (car old) t))))))))) |
---|
806 | |
---|
807 | |
---|
808 | (defun store-gvector-conditional (index gvector old new) |
---|
809 | (%store-node-conditional (+ target::misc-data-offset |
---|
810 | (ash index target::word-shift)) |
---|
811 | gvector |
---|
812 | old |
---|
813 | new)) |
---|
814 | |
---|
815 | (defun %atomic-incf-car (cell &optional (by 1)) |
---|
816 | (%atomic-incf-node (require-type by 'fixnum) |
---|
817 | (require-type cell 'cons) |
---|
818 | target::cons.car)) |
---|
819 | |
---|
820 | (defun %atomic-incf-cdr (cell &optional (by 1)) |
---|
821 | (%atomic-incf-node (require-type by 'fixnum) |
---|
822 | (require-type cell 'cons) |
---|
823 | target::cons.cdr)) |
---|
824 | |
---|
825 | (defun %atomic-incf-gvector (v i &optional (by 1)) |
---|
826 | (setq v (require-type v 'gvector)) |
---|
827 | (setq i (require-type i 'fixnum)) |
---|
828 | (%atomic-incf-node by v (+ target::misc-data-offset (ash i target::word-shift)))) |
---|
829 | |
---|
830 | (defun %atomic-incf-symbol-value (s &optional (by 1)) |
---|
831 | (setq s (require-type s 'symbol)) |
---|
832 | (multiple-value-bind (base offset) (%symbol-binding-address s) |
---|
833 | (%atomic-incf-node by base offset))) |
---|
834 | |
---|
835 | ;;; What happens if there are some pending readers and another writer, |
---|
836 | ;;; and we abort out of the semaphore wait ? If the writer semaphore is |
---|
837 | ;;; signaled before we abandon interest in it |
---|
838 | #-futex |
---|
839 | (defun %write-lock-rwlock-ptr (ptr lock &optional flag) |
---|
840 | (with-macptrs ((write-signal (%get-ptr ptr target::rwlock.writer-signal)) ) |
---|
841 | (if (istruct-typep flag 'lock-acquisition) |
---|
842 | (setf (lock-acquisition.status flag) nil) |
---|
843 | (if flag (report-bad-arg flag 'lock-acquisition))) |
---|
844 | (let* ((level *interrupt-level*) |
---|
845 | (tcr (%current-tcr))) |
---|
846 | (declare (fixnum tcr)) |
---|
847 | (without-interrupts |
---|
848 | (%get-spin-lock ptr) ;(%get-spin-lock (%inc-ptr ptr target::rwlock.spin)) |
---|
849 | (if (eq (%get-object ptr target::rwlock.writer) tcr) |
---|
850 | (progn |
---|
851 | (incf (%get-signed-natural ptr target::rwlock.state)) |
---|
852 | (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
853 | (if flag |
---|
854 | (setf (lock-acquisition.status flag) t)) |
---|
855 | t) |
---|
856 | (do* () |
---|
857 | ((eql 0 (%get-signed-natural ptr target::rwlock.state)) |
---|
858 | ;; That wasn't so bad, was it ? We have the spinlock now. |
---|
859 | (setf (%get-signed-natural ptr target::rwlock.state) 1 |
---|
860 | (%get-natural ptr target::rwlock.spin) 0) |
---|
861 | (%set-object ptr target::rwlock.writer tcr) |
---|
862 | (if flag |
---|
863 | (setf (lock-acquisition.status flag) t)) |
---|
864 | t) |
---|
865 | (incf (%get-natural ptr target::rwlock.blocked-writers)) |
---|
866 | (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
867 | (let* ((*interrupt-level* level)) |
---|
868 | (%process-wait-on-semaphore-ptr write-signal 1 0 (rwlock-write-whostate lock))) |
---|
869 | (%get-spin-lock ptr))))))) |
---|
870 | #+futex |
---|
871 | (defun %write-lock-rwlock-ptr (ptr lock &optional flag) |
---|
872 | (with-macptrs ((write-signal (%INC-ptr ptr target::rwlock.writer-signal)) ) |
---|
873 | (if (istruct-typep flag 'lock-acquisition) |
---|
874 | (setf (lock-acquisition.status flag) nil) |
---|
875 | (if flag (report-bad-arg flag 'lock-acquisition))) |
---|
876 | (let* ((level *interrupt-level*) |
---|
877 | (tcr (%current-tcr))) |
---|
878 | (declare (fixnum tcr)) |
---|
879 | (without-interrupts |
---|
880 | (%lock-futex ptr level lock nil) |
---|
881 | (if (eq (%get-object ptr target::rwlock.writer) tcr) |
---|
882 | (progn |
---|
883 | (incf (%get-signed-natural ptr target::rwlock.state)) |
---|
884 | (%unlock-futex ptr) |
---|
885 | (if flag |
---|
886 | (setf (lock-acquisition.status flag) t)) |
---|
887 | t) |
---|
888 | (do* () |
---|
889 | ((eql 0 (%get-signed-natural ptr target::rwlock.state)) |
---|
890 | ;; That wasn't so bad, was it ? We have the spinlock now. |
---|
891 | (setf (%get-signed-natural ptr target::rwlock.state) 1) |
---|
892 | (setf (%get-signed-long write-signal) -1) |
---|
893 | (%unlock-futex ptr) |
---|
894 | (%set-object ptr target::rwlock.writer tcr) |
---|
895 | (if flag |
---|
896 | (setf (lock-acquisition.status flag) t)) |
---|
897 | t) |
---|
898 | (incf (%get-natural ptr target::rwlock.blocked-writers)) |
---|
899 | (let* ((waitval -1)) |
---|
900 | (%unlock-futex ptr) |
---|
901 | (with-process-whostate ((rwlock-write-whostate lock)) |
---|
902 | (let* ((*interrupt-level* level)) |
---|
903 | (futex-wait write-signal waitval (rwlock-write-whostate lock))))) |
---|
904 | (%lock-futex ptr level lock nil) |
---|
905 | (decf (%get-natural ptr target::rwlock.blocked-writers)))))))) |
---|
906 | |
---|
907 | |
---|
908 | |
---|
909 | (defun write-lock-rwlock (lock &optional flag) |
---|
910 | (%write-lock-rwlock-ptr (read-write-lock-ptr lock) lock flag)) |
---|
911 | |
---|
912 | #-futex |
---|
913 | (defun %read-lock-rwlock-ptr (ptr lock &optional flag) |
---|
914 | (with-macptrs ((read-signal (%get-ptr ptr target::rwlock.reader-signal))) |
---|
915 | (if (istruct-typep flag 'lock-acquisition) |
---|
916 | (setf (lock-acquisition.status flag) nil) |
---|
917 | (if flag (report-bad-arg flag 'lock-acquisition))) |
---|
918 | (let* ((level *interrupt-level*) |
---|
919 | (tcr (%current-tcr))) |
---|
920 | (declare (fixnum tcr)) |
---|
921 | (without-interrupts |
---|
922 | (%get-spin-lock ptr) ;(%get-spin-lock (%inc-ptr ptr target::rwlock.spin)) |
---|
923 | (if (eq (%get-object ptr target::rwlock.writer) tcr) |
---|
924 | (progn |
---|
925 | (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
926 | (error 'deadlock :lock lock)) |
---|
927 | (do* ((state |
---|
928 | (%get-signed-natural ptr target::rwlock.state) |
---|
929 | (%get-signed-natural ptr target::rwlock.state))) |
---|
930 | ((<= state 0) |
---|
931 | ;; That wasn't so bad, was it ? We have the spinlock now. |
---|
932 | (setf (%get-signed-natural ptr target::rwlock.state) |
---|
933 | (the fixnum (1- state)) |
---|
934 | (%get-natural ptr target::rwlock.spin) 0) |
---|
935 | (if flag |
---|
936 | (setf (lock-acquisition.status flag) t)) |
---|
937 | t) |
---|
938 | (declare (fixnum state)) |
---|
939 | (incf (%get-natural ptr target::rwlock.blocked-readers)) |
---|
940 | (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
941 | (let* ((*interrupt-level* level)) |
---|
942 | (%process-wait-on-semaphore-ptr read-signal 1 0 (rwlock-read-whostate lock))) |
---|
943 | (%get-spin-lock ptr))))))) |
---|
944 | |
---|
945 | #+futex |
---|
946 | (defun %read-lock-rwlock-ptr (ptr lock &optional flag) |
---|
947 | (with-macptrs ((reader-signal (%INC-ptr ptr target::rwlock.reader-signal))) |
---|
948 | (if (istruct-typep flag 'lock-acquisition) |
---|
949 | (setf (lock-acquisition.status flag) nil) |
---|
950 | (if flag (report-bad-arg flag 'lock-acquisition))) |
---|
951 | (let* ((level *interrupt-level*) |
---|
952 | (tcr (%current-tcr))) |
---|
953 | (declare (fixnum tcr)) |
---|
954 | (without-interrupts |
---|
955 | (%lock-futex ptr level lock nil) |
---|
956 | (if (eq (%get-object ptr target::rwlock.writer) tcr) |
---|
957 | (progn |
---|
958 | (%unlock-futex ptr) |
---|
959 | (error 'deadlock :lock lock)) |
---|
960 | (do* ((state |
---|
961 | (%get-signed-natural ptr target::rwlock.state) |
---|
962 | (%get-signed-natural ptr target::rwlock.state))) |
---|
963 | ((<= state 0) |
---|
964 | ;; That wasn't so bad, was it ? We have the spinlock now. |
---|
965 | (setf (%get-signed-natural ptr target::rwlock.state) |
---|
966 | (the fixnum (1- state))) |
---|
967 | (setf (%get-signed-long reader-signal) -1) ; can happen multiple times, but that's harmless |
---|
968 | (%unlock-futex ptr) |
---|
969 | (if flag |
---|
970 | (setf (lock-acquisition.status flag) t)) |
---|
971 | t) |
---|
972 | (declare (fixnum state)) |
---|
973 | (incf (%get-natural ptr target::rwlock.blocked-readers)) |
---|
974 | (let* ((waitval -1)) |
---|
975 | (%unlock-futex ptr) |
---|
976 | (let* ((*interrupt-level* level)) |
---|
977 | (futex-wait reader-signal waitval (rwlock-read-whostate lock)))) |
---|
978 | (%lock-futex ptr level lock nil) |
---|
979 | (decf (%get-natural ptr target::rwlock.blocked-readers)))))))) |
---|
980 | |
---|
981 | |
---|
982 | |
---|
983 | (defun read-lock-rwlock (lock &optional flag) |
---|
984 | (%read-lock-rwlock-ptr (read-write-lock-ptr lock) lock flag)) |
---|
985 | |
---|
986 | |
---|
987 | |
---|
988 | #-futex |
---|
989 | (defun %unlock-rwlock-ptr (ptr lock) |
---|
990 | (with-macptrs ((reader-signal (%get-ptr ptr target::rwlock.reader-signal)) |
---|
991 | (writer-signal (%get-ptr ptr target::rwlock.writer-signal))) |
---|
992 | (without-interrupts |
---|
993 | (%get-spin-lock ptr) |
---|
994 | (let* ((state (%get-signed-natural ptr target::rwlock.state)) |
---|
995 | (tcr (%current-tcr))) |
---|
996 | (declare (fixnum state tcr)) |
---|
997 | (cond ((> state 0) |
---|
998 | (unless (eql tcr (%get-object ptr target::rwlock.writer)) |
---|
999 | (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
1000 | (error 'not-lock-owner :lock lock)) |
---|
1001 | (decf state)) |
---|
1002 | ((< state 0) (incf state)) |
---|
1003 | (t (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
1004 | (error 'not-locked :lock lock))) |
---|
1005 | (setf (%get-signed-natural ptr target::rwlock.state) state) |
---|
1006 | (when (zerop state) |
---|
1007 | ;; We want any thread waiting for a lock semaphore to |
---|
1008 | ;; be able to wait interruptibly. When a thread waits, |
---|
1009 | ;; it increments either the "blocked-readers" or "blocked-writers" |
---|
1010 | ;; field, but since it may get interrupted before obtaining |
---|
1011 | ;; the semaphore that's more of "an expression of interest" |
---|
1012 | ;; in taking the lock than it is "a firm commitment to take it." |
---|
1013 | ;; It's generally (much) better to signal the semaphore(s) |
---|
1014 | ;; too often than it would be to not signal them often |
---|
1015 | ;; enough; spurious wakeups are better than deadlock. |
---|
1016 | ;; So: if there are blocked writers, the writer-signal |
---|
1017 | ;; is raised once for each apparent blocked writer. (At most |
---|
1018 | ;; one writer will actually succeed in taking the lock.) |
---|
1019 | ;; If there are blocked readers, the reader-signal is raised |
---|
1020 | ;; once for each of them. (It's possible for both the |
---|
1021 | ;; reader and writer semaphores to be raised on the same |
---|
1022 | ;; unlock; the writer semaphore is raised first, so in that |
---|
1023 | ;; sense, writers still have priority but it's not guaranteed.) |
---|
1024 | ;; Both the "blocked-writers" and "blocked-readers" fields |
---|
1025 | ;; are cleared here (they can't be changed from another thread |
---|
1026 | ;; until this thread releases the spinlock.) |
---|
1027 | (setf (%get-signed-natural ptr target::rwlock.writer) 0) |
---|
1028 | (let* ((nwriters (%get-natural ptr target::rwlock.blocked-writers)) |
---|
1029 | (nreaders (%get-natural ptr target::rwlock.blocked-readers))) |
---|
1030 | (declare (fixnum nreaders nwriters)) |
---|
1031 | (when (> nwriters 0) |
---|
1032 | (setf (%get-natural ptr target::rwlock.blocked-writers) 0) |
---|
1033 | (dotimes (i nwriters) |
---|
1034 | (%signal-semaphore-ptr writer-signal))) |
---|
1035 | (when (> nreaders 0) |
---|
1036 | (setf (%get-natural ptr target::rwlock.blocked-readers) 0) |
---|
1037 | (dotimes (i nreaders) |
---|
1038 | (%signal-semaphore-ptr reader-signal))))) |
---|
1039 | (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
1040 | t)))) |
---|
1041 | |
---|
1042 | #+futex |
---|
1043 | (defun %unlock-rwlock-ptr (ptr lock) |
---|
1044 | (with-macptrs ((reader-signal (%INC-ptr ptr target::rwlock.reader-signal)) |
---|
1045 | (writer-signal (%INC-ptr ptr target::rwlock.writer-signal))) |
---|
1046 | (let* ((signal nil) |
---|
1047 | (wakeup 0)) |
---|
1048 | (without-interrupts |
---|
1049 | (%lock-futex ptr -1 lock nil) |
---|
1050 | (let* ((state (%get-signed-natural ptr target::rwlock.state)) |
---|
1051 | (tcr (%current-tcr))) |
---|
1052 | (declare (fixnum state tcr)) |
---|
1053 | (cond ((> state 0) |
---|
1054 | (unless (eql tcr (%get-object ptr target::rwlock.writer)) |
---|
1055 | (%unlock-futex ptr) |
---|
1056 | (error 'not-lock-owner :lock lock)) |
---|
1057 | (decf state)) |
---|
1058 | ((< state 0) (incf state)) |
---|
1059 | (t (%unlock-futex ptr) |
---|
1060 | (error 'not-locked :lock lock))) |
---|
1061 | (setf (%get-signed-natural ptr target::rwlock.state) state) |
---|
1062 | (when (zerop state) |
---|
1063 | (setf (%get-signed-natural ptr target::rwlock.writer) 0) |
---|
1064 | (let* ((nwriters (%get-natural ptr target::rwlock.blocked-writers)) |
---|
1065 | (nreaders (%get-natural ptr target::rwlock.blocked-readers))) |
---|
1066 | (declare (fixnum nreaders nwriters)) |
---|
1067 | (if (> nwriters 0) |
---|
1068 | (setq signal writer-signal wakeup 1) |
---|
1069 | (if (> nreaders 0) |
---|
1070 | (setq signal reader-signal wakeup #$INT_MAX))))) |
---|
1071 | (when signal (setf (%get-signed-long signal) 0)) |
---|
1072 | (%unlock-futex ptr) |
---|
1073 | (when signal (futex-wake signal wakeup)) |
---|
1074 | t))))) |
---|
1075 | |
---|
1076 | |
---|
1077 | (defun unlock-rwlock (lock) |
---|
1078 | (%unlock-rwlock-ptr (read-write-lock-ptr lock) lock)) |
---|
1079 | |
---|
1080 | ;;; There are all kinds of ways to lose here. |
---|
1081 | ;;; The caller must have read access to the lock exactly once, |
---|
1082 | ;;; or have write access. |
---|
1083 | ;;; there's currently no way to detect whether the caller has |
---|
1084 | ;;; read access at all. |
---|
1085 | ;;; If we have to block and get interrupted, cleanup code may |
---|
1086 | ;;; try to unlock a lock that we don't hold. (It might be possible |
---|
1087 | ;;; to circumvent that if we use the same notifcation object here |
---|
1088 | ;;; that controls that cleanup process.) |
---|
1089 | |
---|
1090 | (defun %promote-rwlock (lock &optional flag) |
---|
1091 | (let* ((ptr (read-write-lock-ptr lock))) |
---|
1092 | (if (istruct-typep flag 'lock-acquisition) |
---|
1093 | (setf (lock-acquisition.status flag) nil) |
---|
1094 | (if flag (report-bad-arg flag 'lock-acquisition))) |
---|
1095 | (let* ((level *interrupt-level*) |
---|
1096 | (tcr (%current-tcr))) |
---|
1097 | (without-interrupts |
---|
1098 | #+futex |
---|
1099 | (%lock-futex ptr level lock nil) |
---|
1100 | #-futex |
---|
1101 | (%get-spin-lock ptr) |
---|
1102 | (let* ((state (%get-signed-natural ptr target::rwlock.state))) |
---|
1103 | (declare (fixnum state)) |
---|
1104 | (cond ((> state 0) |
---|
1105 | (unless (eql (%get-object ptr target::rwlock.writer) tcr) |
---|
1106 | #+futex |
---|
1107 | (%unlock-futex ptr) |
---|
1108 | #-futex |
---|
1109 | (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
1110 | (error :not-lock-owner :lock lock))) |
---|
1111 | ((= state 0) |
---|
1112 | #+futex (%unlock-futex ptr) |
---|
1113 | #-futex (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
1114 | (error :not-locked :lock lock)) |
---|
1115 | (t |
---|
1116 | (if (= state -1) |
---|
1117 | (progn |
---|
1118 | (setf (%get-signed-natural ptr target::rwlock.state) 1) |
---|
1119 | (%set-object ptr target::rwlock.writer tcr) |
---|
1120 | #+futex |
---|
1121 | (%unlock-futex ptr) |
---|
1122 | #-futex |
---|
1123 | (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
1124 | (if flag |
---|
1125 | (setf (lock-acquisition.status flag) t)) |
---|
1126 | t) |
---|
1127 | (progn |
---|
1128 | #+futex |
---|
1129 | (%unlock-futex ptr) |
---|
1130 | #-futex |
---|
1131 | (setf (%get-natural ptr target::rwlock.spin) 0) |
---|
1132 | (%unlock-rwlock-ptr ptr lock) |
---|
1133 | (let* ((*interrupt-level* level)) |
---|
1134 | (%write-lock-rwlock-ptr ptr lock flag))))))))))) |
---|
1135 | |
---|
1136 | |
---|
1137 | |
---|
1138 | (defun safe-get-ptr (p &optional dest) |
---|
1139 | (if (null dest) |
---|
1140 | (setq dest (%null-ptr)) |
---|
1141 | (unless (typep dest 'macptr) |
---|
1142 | (check-type dest macptr))) |
---|
1143 | (without-interrupts ;reentrancy |
---|
1144 | (%safe-get-ptr p dest))) |
---|
1145 | |
---|
1146 | |
---|