Replace DEF!METHOD and SB!XC:DEFMETHOD with just DEFMETHOD.
[sbcl.git] / src / code / late-extensions.lisp
blobb4e450cd490666e34b8d7e2e2aca217a4ec3665c
1 ;;;; various extensions (including SB-INT "internal extensions")
2 ;;;; available both in the cross-compilation host Lisp and in the
3 ;;;; target SBCL, but which can't be defined on the target until until
4 ;;;; some significant amount of machinery (e.g. error-handling) is
5 ;;;; defined
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
16 (in-package "SB!IMPL")
18 ;;; Is X a list for which LENGTH is meaningful, i.e. a list which is
19 ;;; not improper and which is not circular?
20 ;;; FIXME: the reason this can't be defined adjacent to its friends
21 ;;; PROPER-LIST-P and PROPER-LIST-OF-LENGTH-P is that HANDLER-BIND
22 ;;; does not work in 'primordial-extensions'.
23 (defun list-with-length-p (x)
24 (values (ignore-errors (list-length x))))
26 ;;; not used in 0.7.8, but possibly useful for defensive programming
27 ;;; in e.g. (COERCE ... 'VECTOR)
28 ;;;(defun list-length-or-die (x)
29 ;;; (or (list-length x)
30 ;;; ;; not clear how to do this best:
31 ;;; ;; * Should this be a TYPE-ERROR? Colloquially that'd make
32 ;;; ;; lots of sense, but since I'm not sure how to express
33 ;;; ;; "noncircular list" as a Lisp type expression, coding
34 ;;; ;; it seems awkward.
35 ;;; ;; * Should the ERROR object include the offending value?
36 ;;; ;; Ordinarily that's helpful, but if the user doesn't have
37 ;;; ;; his printer set up to deal with cyclicity, we might not
38 ;;; ;; be doing him a favor by printing the object here.
39 ;;; ;; -- WHN 2002-10-19
40 ;;; (error "can't calculate length of cyclic list")))
42 ;;; This is used in constructing arg lists for debugger printing,
43 ;;; and when needing to print unbound slots in PCL.
44 (defstruct (unprintable-object
45 (:constructor make-unprintable-object (string))
46 (:print-object (lambda (x s)
47 (print-unreadable-object (x s)
48 (write-string (unprintable-object-string x) s))))
49 (:copier nil))
50 (string nil :read-only t))
52 ;;; Used internally, but it would be nice to provide something
53 ;;; like this for users as well.
54 (defmacro define-structure-slot-addressor (name &key structure slot)
55 (let* ((dd (find-defstruct-description structure t))
56 (slotd (or (and dd (find slot (dd-slots dd) :key #'dsd-name))
57 (error "Slot ~S not found in ~S." slot structure)))
58 (index (dsd-index slotd)))
59 `(progn
60 (declaim (inline ,name))
61 (defun ,name (instance)
62 (declare (type ,structure instance) (optimize speed))
63 (truly-the
64 word
65 (+ (get-lisp-obj-address instance)
66 ,(+ (- sb!vm:instance-pointer-lowtag)
67 (* (+ sb!vm:instance-slots-offset index)
68 sb!vm:n-word-bytes))))))))
70 (defun spin-loop-hint ()
71 #!+sb-doc
72 "Hints the processor that the current thread is spin-looping."
73 (spin-loop-hint))
75 (defun call-hooks (kind hooks &key (on-error :error))
76 (dolist (hook hooks)
77 (handler-case
78 (funcall hook)
79 (serious-condition (c)
80 (if (eq :warn on-error)
81 (warn "Problem running ~A hook ~S:~% ~A" kind hook c)
82 (with-simple-restart (continue "Skip this ~A hook." kind)
83 (error "Problem running ~A hook ~S:~% ~A" kind hook c)))))))
85 ;;;; DEFGLOBAL
87 (sb!xc:defmacro defglobal (name value &optional (doc nil docp))
88 #!+sb-doc
89 "Defines NAME as a global variable that is always bound. VALUE is evaluated
90 and assigned to NAME both at compile- and load-time, but only if NAME is not
91 already bound.
93 Global variables share their values between all threads, and cannot be
94 locally bound, declared special, defined as constants, and neither bound
95 nor defined as symbol macros.
97 See also the declarations SB-EXT:GLOBAL and SB-EXT:ALWAYS-BOUND."
98 (let ((boundp (make-symbol "BOUNDP")))
99 `(progn
100 (eval-when (:compile-toplevel)
101 (let ((,boundp (boundp ',name)))
102 (%compiler-defglobal ',name :always-bound
103 (unless ,boundp ,value) (not ,boundp))))
104 (let ((,boundp (boundp ',name)))
105 (%defglobal ',name (unless ,boundp ,value) ,boundp ',doc ,docp
106 (sb!c:source-location))))))
108 (sb!xc:defmacro define-load-time-global (name value &optional (doc nil docp))
109 #!+sb-doc
110 "Defines NAME as a global variable that is always bound. VALUE is evaluated
111 and assigned to NAME at load-time, but only if NAME is not already bound.
113 Attempts to read NAME at compile-time will signal an UNBOUND-VARIABLE error
114 unless it has otherwise been assigned a value.
116 See also DEFGLOBAL which assigns the VALUE at compile-time too."
117 (let ((boundp (make-symbol "BOUNDP")))
118 `(progn
119 (eval-when (:compile-toplevel)
120 (%compiler-defglobal ',name :eventually nil nil))
121 (let ((,boundp (boundp ',name)))
122 (%defglobal ',name (unless ,boundp ,value) ,boundp ',doc ,docp
123 (sb!c:source-location))))))
125 (defun %compiler-defglobal (name always-boundp value assign-it-p)
126 (sb!xc:proclaim `(global ,name))
127 (when assign-it-p
128 #-sb-xc-host
129 (set-symbol-global-value name value)
130 #+sb-xc-host
131 (set name value))
132 (sb!c::process-variable-declaration
133 name 'always-bound
134 ;; don't "weaken" the proclamation if it's in fact always bound now
135 (if (eq (info :variable :always-bound name) :always-bound)
136 :always-bound
137 always-boundp)))
139 (defun %defglobal (name value boundp doc docp source-location)
140 (%compiler-defglobal name :always-bound value (not boundp))
141 (when docp
142 (setf (fdocumentation name 'variable) doc))
143 (when source-location
144 (setf (info :source-location :variable name) source-location))
145 name)
147 ;;;; WAIT-FOR -- waiting on arbitrary conditions
149 #-sb-xc-host
150 (defun %%wait-for (test stop-sec stop-usec)
151 (declare (function test))
152 (labels ((try ()
153 (declare (optimize (safety 0)))
154 (awhen (funcall test)
155 (return-from %%wait-for it)))
156 (tick (sec usec)
157 (declare (type fixnum sec usec))
158 ;; TICK is microseconds
159 (+ usec (* 1000000 sec)))
160 (get-tick ()
161 (multiple-value-call #'tick
162 (decode-internal-time (get-internal-real-time)))))
163 (let* ((timeout-tick (when stop-sec (tick stop-sec stop-usec)))
164 (start (get-tick))
165 ;; Rough estimate of how long a single attempt takes.
166 (try-ticks (progn
167 (try) (try) (try)
168 (max 1 (truncate (- (get-tick) start) 3)))))
169 ;; Scale sleeping between attempts:
171 ;; Start by sleeping for as many ticks as an average attempt
172 ;; takes, then doubling for each attempt.
174 ;; Max out at 0.1 seconds, or the 2 x time of a single try,
175 ;; whichever is longer -- with a hard cap of 10 seconds.
177 ;; FIXME: Maybe the API should have a :MAX-SLEEP argument?
178 (loop with max-ticks = (max 100000 (min (* 2 try-ticks)
179 (expt 10 7)))
180 for scale of-type fixnum = 1
181 then (let ((x (logand most-positive-fixnum (* 2 scale))))
182 (if (> scale x)
183 most-positive-fixnum
185 do (try)
186 (let* ((now (get-tick))
187 (sleep-ticks (min (* try-ticks scale) max-ticks))
188 (sleep
189 (if timeout-tick
190 ;; If sleep would take us past the
191 ;; timeout, shorten it so it's just
192 ;; right.
193 (if (>= (+ now sleep-ticks) timeout-tick)
194 (- timeout-tick now)
195 sleep-ticks)
196 sleep-ticks)))
197 (declare (type fixnum sleep))
198 (cond ((plusp sleep)
199 ;; microseconds to seconds and nanoseconds
200 (multiple-value-bind (sec nsec)
201 (truncate (* 1000 sleep) (expt 10 9))
202 (with-interrupts
203 (sb!unix:nanosleep sec nsec))))
205 (return-from %%wait-for nil))))))))
207 #-sb-xc-host
208 (defun %wait-for (test timeout)
209 (declare (function test))
210 (tagbody
211 :restart
212 (multiple-value-bind (to-sec to-usec stop-sec stop-usec deadlinep)
213 (decode-timeout timeout)
214 (declare (ignore to-sec to-usec))
215 (return-from %wait-for
216 (or (%%wait-for test stop-sec stop-usec)
217 (when deadlinep
218 (signal-deadline)
219 (go :restart)))))))
221 (defmacro wait-for (test-form &key timeout)
222 #!+sb-doc
223 "Wait until TEST-FORM evaluates to true, then return its primary value.
224 If TIMEOUT is provided, waits at most approximately TIMEOUT seconds before
225 returning NIL.
227 If WITH-DEADLINE has been used to provide a global deadline, signals a
228 DEADLINE-TIMEOUT if TEST-FORM doesn't evaluate to true before the
229 deadline.
231 Experimental: subject to change without prior notice."
232 `(dx-flet ((wait-for-test () (progn ,test-form)))
233 (%wait-for #'wait-for-test ,timeout)))
235 (defmacro with-progressive-timeout ((name &key seconds)
236 &body body)
237 #!+sb-doc
238 "Binds NAME as a local function for BODY. Each time #'NAME is called, it
239 returns SECONDS minus the time that has elapsed since BODY was entered, or
240 zero if more time than SECONDS has elapsed. If SECONDS is NIL, #'NAME
241 returns NIL each time."
242 (with-unique-names (deadline time-left sec)
243 `(let* ((,sec ,seconds)
244 (,deadline
245 (when ,sec
246 (+ (get-internal-real-time)
247 (round (* ,seconds internal-time-units-per-second))))))
248 (flet ((,name ()
249 (when ,deadline
250 (let ((,time-left (- ,deadline (get-internal-real-time))))
251 (if (plusp ,time-left)
252 (* (coerce ,time-left 'single-float)
253 (load-time-value (/ 1.0f0 internal-time-units-per-second) t))
254 0)))))
255 ,@body))))
257 (defun split-version-string (string)
258 (loop with subversion and start = 0
259 with end = (length string)
260 when (setf (values subversion start)
261 (parse-integer string :start start :junk-allowed t))
262 collect it
263 while (and subversion
264 (< start end)
265 (char= (char string start) #\.))
266 do (incf start)))
268 (defun version>= (x y)
269 (unless (or x y)
270 (return-from version>= t))
271 (let ((head-x (or (first x) 0))
272 (head-y (or (first y) 0)))
273 (or (> head-x head-y)
274 (and (= head-x head-y)
275 (version>= (rest x) (rest y))))))
277 (defun assert-version->= (&rest subversions)
278 #!+sb-doc
279 "Asserts that the current SBCL is of version equal to or greater than
280 the version specified in the arguments. A continuable error is signaled
281 otherwise.
283 The arguments specify a sequence of subversion numbers in big endian order.
284 They are compared lexicographically with the runtime version, and versions
285 are treated as though trailed by an unbounded number of 0s.
287 For example, (assert-version->= 1 1 4) asserts that the current SBCL is
288 version 1.1.4[.0.0...] or greater, and (assert-version->= 1) that it is
289 version 1[.0.0...] or greater."
290 (let ((version (split-version-string (lisp-implementation-version))))
291 (unless (version>= version subversions)
292 (cerror "Disregard this version requirement."
293 "SBCL ~A is too old for this program (version ~{~A~^.~} ~
294 or later is required)."
295 (lisp-implementation-version)
296 subversions))))
298 ;;; Signalling an error when trying to print an error condition is
299 ;;; generally a PITA, so whatever the failure encountered when
300 ;;; wondering about FILE-POSITION within a condition printer, 'tis
301 ;;; better silently to give up than to try to complain.
302 (defun file-position-or-nil-for-error (stream &optional (pos nil posp))
303 ;; Arguably FILE-POSITION shouldn't be signalling errors at all; but
304 ;; "NIL if this cannot be determined" in the ANSI spec doesn't seem
305 ;; absolutely unambiguously to prohibit errors when, e.g., STREAM
306 ;; has been closed so that FILE-POSITION is a nonsense question. So
307 ;; my (WHN) impression is that the conservative approach is to
308 ;; IGNORE-ERRORS. (I encountered this failure from within a homebrew
309 ;; defsystemish operation where the ERROR-STREAM had been CL:CLOSEd,
310 ;; I think by nonlocally exiting through a WITH-OPEN-FILE, by the
311 ;; time an error was reported.)
312 (ignore-errors
313 (if posp
314 (file-position stream pos)
315 (file-position stream))))
317 (defun stream-error-position-info (stream &optional position)
318 (when (and (not position) (form-tracking-stream-p stream))
319 (let ((line/col (line/col-from-charpos stream)))
320 (return-from stream-error-position-info
321 `((:line ,(car line/col))
322 (:column ,(cdr line/col))
323 ,@(let ((position (file-position-or-nil-for-error stream)))
324 ;; FIXME: 1- is technically broken for multi-byte external
325 ;; encodings, albeit bug-compatible with the broken code in
326 ;; the general case (below) for non-form-tracking-streams.
327 ;; i.e. If you position to this byte, it might not be the
328 ;; first byte of any character.
329 (when position `((:file-position ,(1- position)))))))))
331 ;; Give up early for interactive streams and non-character stream.
332 (when (or (ignore-errors (interactive-stream-p stream))
333 (not (subtypep (ignore-errors (stream-element-type stream))
334 'character)))
335 (return-from stream-error-position-info))
337 (flet ((read-content (old-position position)
338 "Read the content of STREAM into a buffer in order to count
339 lines and columns."
340 (unless (and old-position position
341 (< position sb!xc:array-dimension-limit))
342 (return-from read-content))
343 (let ((content
344 (make-string position :element-type (stream-element-type stream))))
345 (when (and (file-position-or-nil-for-error stream :start)
346 (eql position (ignore-errors (read-sequence content stream))))
347 (file-position-or-nil-for-error stream old-position)
348 content)))
349 ;; Lines count from 1, columns from 0. It's stupid and
350 ;; traditional.
351 (line (string)
352 (1+ (count #\Newline string)))
353 (column (string position)
354 (- position (or (position #\Newline string :from-end t) 0))))
355 (let* ((stream-position (file-position-or-nil-for-error stream))
356 (position (or position
357 ;; FILE-POSITION is the next character --
358 ;; error is at the previous one.
359 (and stream-position (plusp stream-position)
360 (1- stream-position))))
361 (content (read-content stream-position position)))
362 `(,@(when content `((:line ,(line content))
363 (:column ,(column content position))))
364 ,@(when position `((:file-position ,position)))))))