Eliminate style-warning about undefined type GLOBAL-VAR
[sbcl.git] / src / code / sysmacs.lisp
blob97c25be1747e461b691993f9313571db9fc7cd57
1 ;;;; miscellaneous system hacking macros
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; these are initialized in cold init
16 (!defvar *in-without-gcing* nil)
17 (!defvar *gc-inhibit* t)
19 ;;; When the dynamic usage increases beyond this amount, the system
20 ;;; notes that a garbage collection needs to occur by setting
21 ;;; *GC-PENDING* to T. It starts out as NIL meaning nobody has figured
22 ;;; out what it should be yet.
23 (!defvar *gc-pending* nil)
25 #!+sb-thread
26 (!defvar *stop-for-gc-pending* nil)
28 ;;; This one is initialized by the runtime, at thread creation. On
29 ;;; non-x86oid gencgc targets, this is a per-thread list of objects
30 ;;; which must not be moved during GC. It is frobbed by the code for
31 ;;; with-pinned-objects in src/compiler/target/macros.lisp.
32 #!+(and gencgc (not (or x86 x86-64)))
33 (defvar sb!vm::*pinned-objects*)
35 (defmacro without-gcing (&body body)
36 #!+sb-doc
37 "Executes the forms in the body without doing a garbage collection. It
38 inhibits both automatically and explicitly triggered collections. Finally,
39 upon leaving the BODY if gc is not inhibited it runs the pending gc.
40 Similarly, if gc is triggered in another thread then it waits until gc is
41 enabled in this thread.
43 Implies SB-SYS:WITHOUT-INTERRUPTS for BODY, and causes any nested
44 SB-SYS:WITH-INTERRUPTS to signal a warning during execution of the BODY.
46 Should be used with great care, and not at all in multithreaded application
47 code: Any locks that are ever acquired while GC is inhibited need to be always
48 held with GC inhibited to prevent deadlocks: if T1 holds the lock and is
49 stopped for GC while T2 is waiting for the lock inside WITHOUT-GCING the
50 system will be deadlocked. Since SBCL does not currently document its internal
51 locks, application code can never be certain that this invariant is
52 maintained."
53 (with-unique-names (without-gcing-body)
54 `(dx-flet ((,without-gcing-body ()
55 ,@body))
56 (if *gc-inhibit*
57 (,without-gcing-body)
58 ;; We need to disable interrupts before disabling GC, so
59 ;; that signal handlers using locks don't accidentally try
60 ;; to grab them with GC inhibited.
61 (let ((*in-without-gcing* t))
62 (unwind-protect
63 (let* ((*allow-with-interrupts* nil)
64 (*interrupts-enabled* nil)
65 (*gc-inhibit* t))
66 (,without-gcing-body))
67 ;; This is not racy becuase maybe_defer_handler
68 ;; defers signals if *GC-INHIBIT* is NIL but there
69 ;; is a pending gc or stop-for-gc.
70 (when (or *interrupt-pending*
71 *gc-pending*
72 #!+sb-thread *stop-for-gc-pending*)
73 (sb!unix::receive-pending-interrupt))))))))
75 ;;; EOF-OR-LOSE is a useful macro that handles EOF.
76 (defmacro eof-or-lose (stream eof-error-p eof-value)
77 `(if ,eof-error-p
78 (error 'end-of-file :stream ,stream)
79 ,eof-value))
81 ;;; These macros handle the special cases of T and NIL for input and
82 ;;; output streams.
83 ;;; It is unfortunate that the names connote synonym-streams being involved.
84 ;;; Perhaps {IN,OUT}-STREAM-FROM-DESIGNATOR would have been better.
85 ;;; And shouldn't the high-security feature check that if either NIL or T
86 ;;; is given, the designated stream has the right directionality?
87 ;;; Nothing prevents *TERMINAL-IO* from being bound to an output-only stream.
88 ;;;
89 ;;; FIXME: Shouldn't these be functions instead of macros?
90 (defmacro in-synonym-of (stream)
91 (let ((svar (gensym)))
92 `(let ((,svar ,stream))
93 (cond ((null ,svar) *standard-input*)
94 ((eq ,svar t) *terminal-io*)
96 #!+high-security
97 (unless (input-stream-p ,svar)
98 (error 'simple-type-error
99 :datum ,svar
100 :expected-type '(satisfies input-stream-p)
101 :format-control "~S isn't an input stream"
102 :format-arguments (list ,svar)))
103 ,svar)))))
104 ;; As noted above, this code is a tad wasteful for probably not a huge
105 ;; performance gain. On the other hand, where STREAM is known to be of type
106 ;; STREAM, it produces shorter code. But we could shorten the general case:
108 (lambda (x)
109 (block nil
110 (symbol-value
111 (case x ((nil) '*standard-output*)
112 ((t) '*terminal-io*)
113 (t (return x))))))
115 (defmacro out-synonym-of (stream)
116 (let ((svar (gensym)))
117 `(let ((,svar ,stream))
118 (cond ((null ,svar) *standard-output*)
119 ((eq ,svar t) *terminal-io*)
121 #!+high-security
122 (unless (output-stream-p ,svar)
123 (error 'simple-type-error
124 :datum ,svar
125 :expected-type '(satisfies output-stream-p)
126 :format-control "~S isn't an output stream."
127 :format-arguments (list ,svar)))
128 ,svar)))))
130 ;;; WITH-mumble-STREAM calls the function in the given SLOT of the
131 ;;; STREAM with the ARGS for ANSI-STREAMs, or the FUNCTION with the
132 ;;; ARGS for FUNDAMENTAL-STREAMs.
133 (defmacro with-in-stream (stream (slot &rest args) &optional stream-dispatch)
134 `(let ((stream (in-synonym-of ,stream)))
135 ,(if stream-dispatch
136 `(if (ansi-stream-p stream)
137 (funcall (,slot stream) stream ,@args)
138 ,@(when stream-dispatch
139 `(,(destructuring-bind (function &rest args) stream-dispatch
140 `(,function stream ,@args)))))
141 `(funcall (,slot stream) stream ,@args))))
143 (defmacro with-out-stream/no-synonym (stream (slot &rest args) &optional stream-dispatch)
144 `(let ((stream ,stream))
145 ,(if stream-dispatch
146 `(if (ansi-stream-p stream)
147 (funcall (,slot stream) stream ,@args)
148 ,@(when stream-dispatch
149 `(,(destructuring-bind (function &rest args) stream-dispatch
150 `(,function stream ,@args)))))
151 `(funcall (,slot stream) stream ,@args))))
153 (defmacro with-out-stream (stream (slot &rest args) &optional stream-dispatch)
154 `(with-out-stream/no-synonym (out-synonym-of ,stream)
155 (,slot ,@args) ,stream-dispatch))
158 ;;;; These are hacks to make the reader win.
160 ;;; This macro sets up some local vars for use by the
161 ;;; FAST-READ-CHAR macro within the enclosed lexical scope. The stream
162 ;;; is assumed to be a ANSI-STREAM.
164 ;;; KLUDGE: Some functions (e.g. ANSI-STREAM-READ-LINE) use these variables
165 ;;; directly, instead of indirecting through FAST-READ-CHAR.
166 ;;; When ANSI-STREAM-INPUT-CHAR-POS is non-null, we take care to update it,
167 ;;; but not for each character of input.
168 (defmacro prepare-for-fast-read-char (stream &body forms)
169 `(let* ((%frc-stream% ,stream)
170 (%frc-method% (ansi-stream-in %frc-stream%))
171 (%frc-buffer% (ansi-stream-cin-buffer %frc-stream%))
172 (%frc-index% (ansi-stream-in-index %frc-stream%)))
173 (declare (type (mod ,(1+ +ansi-stream-in-buffer-length+)) %frc-index%)
174 (type ansi-stream %frc-stream%))
175 ,@forms))
177 ;;; This macro must be called after one is done with FAST-READ-CHAR
178 ;;; inside its scope to decache the ANSI-STREAM-IN-INDEX.
179 ;;; To keep the amount of code injected by FAST-READ-CHAR as small as possible,
180 ;;; we avoid bumping the absolute stream position counter at each character.
181 ;;; When finished looping, one extra function call takes care of that.
182 ;;; If buffer refills occurred within FAST-READ-CHAR, the refill logic
183 ;;; similarly scans the cin-buffer before placing anything new into it.
184 (defmacro done-with-fast-read-char ()
185 `(progn
186 (when (ansi-stream-input-char-pos %frc-stream%)
187 (update-input-char-pos %frc-stream% %frc-index%))
188 (setf (ansi-stream-in-index %frc-stream%) %frc-index%)))
190 ;;; a macro with the same calling convention as READ-CHAR, to be used
191 ;;; within the scope of a PREPARE-FOR-FAST-READ-CHAR.
192 ;;; If EOF-ERROR-P is statically T (not any random expression evaluating
193 ;;; to T) then wrap the whole thing in (TRULY-THE CHARACTER ...)
194 ;;; because it's either going to yield a character or signal EOF.
195 (defmacro fast-read-char (&optional (eof-error-p t) (eof-value ()))
196 (let ((result
197 `(if (not %frc-buffer%)
198 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value)
199 (block nil
200 (when (= %frc-index% +ansi-stream-in-buffer-length+)
201 (let ((index-or-nil
202 (fast-read-char-refill %frc-stream% ,eof-error-p)))
203 ,@(unless (eq eof-error-p 't)
204 `((when (null index-or-nil)
205 (return ,eof-value))))
206 (setq %frc-index%
207 (truly-the (mod ,+ansi-stream-in-buffer-length+)
208 index-or-nil))))
209 (prog1 (aref %frc-buffer%
210 (truly-the (mod ,+ansi-stream-in-buffer-length+)
211 %frc-index%))
212 (incf %frc-index%))))))
213 (cond ((eq eof-error-p 't)
214 `(truly-the character ,result))
215 ((and (symbolp eof-value) (constantp eof-value)
216 ;; use an EQL specifier only if the const is EQL-comparable
217 (typep (symbol-value eof-value) '(or symbol fixnum)))
218 `(truly-the (or (eql ,(symbol-value eof-value)) character) ,result))
220 result))))
222 ;;;; And these for the fasloader...
224 ;;; Just like PREPARE-FOR-FAST-READ-CHAR except that we get the BIN
225 ;;; method. The stream is assumed to be a ANSI-STREAM.
227 ;;; FIXME: Refactor PREPARE-FOR-FAST-READ-CHAR into similar shape.
228 (defmacro with-fast-read-byte ((type stream &optional (eof-error-p t) eof-value)
229 &body body)
230 (aver (or (eq t eof-error-p) (eq t type)))
231 (with-unique-names (f-stream f-method f-buffer f-index eof-p eof-val)
232 `(let* ((,f-stream ,stream)
233 (,eof-p ,eof-error-p)
234 (,eof-val ,eof-value)
235 (,f-method (ansi-stream-bin ,f-stream))
236 (,f-buffer (ansi-stream-in-buffer ,f-stream))
237 (,f-index (ansi-stream-in-index ,f-stream)))
238 (declare (type ansi-stream ,f-stream)
239 (type index ,f-index))
240 (declare (disable-package-locks fast-read-byte))
241 (flet ((fast-read-byte ()
242 (,@(cond ((equal '(unsigned-byte 8) type)
243 ;; KLUDGE: For some reason I haven't tracked down
244 ;; this makes a difference even in given the TRULY-THE.
245 `(logand #xff))
247 `(identity)))
248 (truly-the ,type
249 (cond
250 ((not ,f-buffer)
251 (funcall ,f-method ,f-stream ,eof-p ,eof-val))
252 ((= ,f-index +ansi-stream-in-buffer-length+)
253 (prog1 (fast-read-byte-refill ,f-stream ,eof-p ,eof-val)
254 (setq ,f-index (ansi-stream-in-index ,f-stream))))
256 (prog1 (aref ,f-buffer ,f-index)
257 (incf ,f-index))))))))
258 (declare (inline fast-read-byte))
259 (declare (enable-package-locks fast-read-byte))
260 (unwind-protect
261 (locally ,@body)
262 (setf (ansi-stream-in-index ,f-stream) ,f-index))))))
264 ;; This is an internal-use-only macro.
265 (defmacro do-rest-arg (((var &optional index-var) rest-var
266 &optional (start 0) result)
267 &body body)
268 ;; If the &REST arg never needs to be reified, this is slightly quicker
269 ;; than using a DX list.
270 (let ((index (sb!xc:gensym "INDEX")))
271 `(let ((,index ,start))
272 (loop
273 (cond ((< (truly-the index ,index) (length ,rest-var))
274 (let ((,var (fast-&rest-nth ,index ,rest-var))
275 ,@(if index-var `((,index-var ,index))))
276 ,@body)
277 (incf ,index))
279 (return ,result)))))))