1.0.5.2: non-racy WITH-SPINLOCK-AND-WITHOUT-GCING
[sbcl.git] / src / code / sysmacs.lisp
1 ;;;; miscellaneous system hacking macros
2
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.
11
12 (in-package "SB!IMPL")
13
14 (defmacro atomic-incf/symbol (symbol-name &optional (delta 1))
15   #!-sb-thread
16   `(incf ,symbol-name ,delta)
17   #!+sb-thread
18   `(locally
19     (declare (optimize (safety 0) (speed 3)))
20     (sb!vm::locked-symbol-global-value-add ',symbol-name ,delta)))
21
22 (defvar *gc-inhibit*) ; initialized in cold init
23
24 ;;; When the dynamic usage increases beyond this amount, the system
25 ;;; notes that a garbage collection needs to occur by setting
26 ;;; *GC-PENDING* to T. It starts out as NIL meaning nobody has figured
27 ;;; out what it should be yet.
28 (defvar *gc-pending* nil)
29
30 #!+sb-thread
31 (defvar *stop-for-gc-pending* nil)
32
33 (defmacro without-gcing (&body body)
34   #!+sb-doc
35   "Executes the forms in the body without doing a garbage collection. It
36 inhibits both automatically and explicitly triggered collections. Finally,
37 upon leaving the BODY if gc is not inhibited it runs the pending gc.
38 Similarly, if gc is triggered in another thread then it waits until gc is
39 enabled in this thread.
40
41 Implies SB-SYS:WITHOUT-INTERRUPTS for BODY, and causes any nested
42 SB-SYS:WITH-INTERRUPTS to signal a warning during execution of the BODY.
43
44 Should be used with great care, and not at all in multithreaded application
45 code: Any locks that are ever acquired while GC is inhibited need to be always
46 held with GC inhibited to prevent deadlocks: if T1 holds the lock and is
47 stopped for GC while T2 is waiting for the lock inside WITHOUT-GCING the
48 system will be deadlocked. Since SBCL does not currently document its internal
49 locks, application code can never be certain that this invariant is
50 maintained."
51   `(unwind-protect
52         (let* ((*interrupts-enabled* nil)
53                (*gc-inhibit* t))
54           ,@body)
55      (when (or (and *interrupts-enabled* *interrupt-pending*)
56                (and (not *gc-inhibit*)
57                     (or *gc-pending* #!+sb-thread *stop-for-gc-pending*)))
58        (sb!unix::receive-pending-interrupt))))
59
60 \f
61 ;;; EOF-OR-LOSE is a useful macro that handles EOF.
62 (defmacro eof-or-lose (stream eof-error-p eof-value)
63   `(if ,eof-error-p
64        (error 'end-of-file :stream ,stream)
65        ,eof-value))
66
67 ;;; These macros handle the special cases of T and NIL for input and
68 ;;; output streams.
69 ;;;
70 ;;; FIXME: Shouldn't these be functions instead of macros?
71 (defmacro in-synonym-of (stream &optional check-type)
72   (let ((svar (gensym)))
73     `(let ((,svar ,stream))
74        (cond ((null ,svar) *standard-input*)
75              ((eq ,svar t) *terminal-io*)
76              (t ,@(when check-type `((enforce-type ,svar ,check-type))) ;
77                 #!+high-security
78                 (unless (input-stream-p ,svar)
79                   (error 'simple-type-error
80                          :datum ,svar
81                          :expected-type '(satisfies input-stream-p)
82                          :format-control "~S isn't an input stream"
83                          :format-arguments (list ,svar)))
84                 ,svar)))))
85 (defmacro out-synonym-of (stream &optional check-type)
86   (let ((svar (gensym)))
87     `(let ((,svar ,stream))
88        (cond ((null ,svar) *standard-output*)
89              ((eq ,svar t) *terminal-io*)
90              (t ,@(when check-type `((check-type ,svar ,check-type)))
91                 #!+high-security
92                 (unless (output-stream-p ,svar)
93                   (error 'simple-type-error
94                          :datum ,svar
95                          :expected-type '(satisfies output-stream-p)
96                          :format-control "~S isn't an output stream."
97                          :format-arguments (list ,svar)))
98                 ,svar)))))
99
100 ;;; WITH-mumble-STREAM calls the function in the given SLOT of the
101 ;;; STREAM with the ARGS for ANSI-STREAMs, or the FUNCTION with the
102 ;;; ARGS for FUNDAMENTAL-STREAMs.
103 (defmacro with-in-stream (stream (slot &rest args) &optional stream-dispatch)
104   `(let ((stream (in-synonym-of ,stream)))
105     ,(if stream-dispatch
106          `(if (ansi-stream-p stream)
107               (funcall (,slot stream) stream ,@args)
108               ,@(when stream-dispatch
109                   `(,(destructuring-bind (function &rest args) stream-dispatch
110                        `(,function stream ,@args)))))
111          `(funcall (,slot stream) stream ,@args))))
112
113 (defmacro with-out-stream/no-synonym (stream (slot &rest args) &optional stream-dispatch)
114   `(let ((stream ,stream))
115     ,(if stream-dispatch
116          `(if (ansi-stream-p stream)
117               (funcall (,slot stream) stream ,@args)
118               ,@(when stream-dispatch
119                   `(,(destructuring-bind (function &rest args) stream-dispatch
120                                          `(,function stream ,@args)))))
121          `(funcall (,slot stream) stream ,@args))))
122
123 (defmacro with-out-stream (stream (slot &rest args) &optional stream-dispatch)
124   `(with-out-stream/no-synonym (out-synonym-of ,stream)
125     (,slot ,@args) ,stream-dispatch))
126
127 \f
128 ;;;; These are hacks to make the reader win.
129
130 ;;; This macro sets up some local vars for use by the
131 ;;; FAST-READ-CHAR macro within the enclosed lexical scope. The stream
132 ;;; is assumed to be a ANSI-STREAM.
133 ;;;
134 ;;; KLUDGE: Some functions (e.g. ANSI-STREAM-READ-LINE) use these variables
135 ;;; directly, instead of indirecting through FAST-READ-CHAR.
136 (defmacro prepare-for-fast-read-char (stream &body forms)
137   `(let* ((%frc-stream% ,stream)
138           (%frc-method% (ansi-stream-in %frc-stream%))
139           (%frc-buffer% (ansi-stream-cin-buffer %frc-stream%))
140           (%frc-index% (ansi-stream-in-index %frc-stream%)))
141      (declare (type index %frc-index%)
142               (type ansi-stream %frc-stream%))
143      ,@forms))
144
145 ;;; This macro must be called after one is done with FAST-READ-CHAR
146 ;;; inside its scope to decache the ANSI-STREAM-IN-INDEX.
147 (defmacro done-with-fast-read-char ()
148   `(setf (ansi-stream-in-index %frc-stream%) %frc-index%))
149
150 ;;; a macro with the same calling convention as READ-CHAR, to be used
151 ;;; within the scope of a PREPARE-FOR-FAST-READ-CHAR.
152 (defmacro fast-read-char (&optional (eof-error-p t) (eof-value ()))
153   `(cond
154     ((not %frc-buffer%)
155      (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
156     ((= %frc-index% +ansi-stream-in-buffer-length+)
157      (prog1 (fast-read-char-refill %frc-stream% ,eof-error-p ,eof-value)
158             (setq %frc-index% (ansi-stream-in-index %frc-stream%))))
159     (t
160      (prog1 (aref %frc-buffer% %frc-index%)
161             (incf %frc-index%)))))
162
163 ;;;; And these for the fasloader...
164
165 ;;; Just like PREPARE-FOR-FAST-READ-CHAR except that we get the BIN
166 ;;; method. The stream is assumed to be a ANSI-STREAM.
167 ;;;
168 ;;; KLUDGE: It seems weird to have to remember to explicitly call
169 ;;; DONE-WITH-FAST-READ-BYTE at the end of this, given that we're
170 ;;; already wrapping the stuff inside in a block. Why not rename this
171 ;;; macro to WITH-FAST-READ-BYTE, do the DONE-WITH-FAST-READ-BYTE stuff
172 ;;; automatically at the end of the block, and eliminate
173 ;;; DONE-WITH-FAST-READ-BYTE as a separate entity? (and similarly
174 ;;; for the FAST-READ-CHAR stuff) -- WHN 19990825
175 (defmacro prepare-for-fast-read-byte (stream &body forms)
176   `(let* ((%frc-stream% ,stream)
177           (%frc-method% (ansi-stream-bin %frc-stream%))
178           (%frc-buffer% (ansi-stream-in-buffer %frc-stream%))
179           (%frc-index% (ansi-stream-in-index %frc-stream%)))
180      (declare (type index %frc-index%)
181               (type ansi-stream %frc-stream%))
182      ,@forms))
183
184 ;;; Similar to fast-read-char, but we use a different refill routine & don't
185 ;;; convert to characters. If ANY-TYPE is true, then this can be used on any
186 ;;; integer streams, and we don't assert the result type.
187 (defmacro fast-read-byte (&optional (eof-error-p t) (eof-value ()) any-type)
188   ;; KLUDGE: should use ONCE-ONLY on EOF-ERROR-P and EOF-VALUE -- WHN 19990825
189   `(truly-the
190     ,(if (and (eq eof-error-p t) (not any-type)) '(unsigned-byte 8) t)
191     (cond
192      ((not %frc-buffer%)
193       (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
194      ((= %frc-index% +ansi-stream-in-buffer-length+)
195       (prog1 (fast-read-byte-refill %frc-stream% ,eof-error-p ,eof-value)
196         (setq %frc-index% (ansi-stream-in-index %frc-stream%))))
197      (t
198       (prog1 (aref %frc-buffer% %frc-index%)
199         (incf %frc-index%))))))
200 (defmacro done-with-fast-read-byte ()
201   `(done-with-fast-read-char))