/[slime]/slime/swank-allegro.lisp
ViewVC logotype

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.91 - (hide annotations)
Fri Oct 20 00:15:15 2006 UTC (7 years, 6 months ago) by mbaringer
Branch: MAIN
Changes since 1.90: +3 -2 lines
(initialize-multiprocessing): Update for new api.
1 heller 1.79 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;;* "; -*-
2 heller 1.1 ;;;
3     ;;; swank-allegro.lisp --- Allegro CL specific code for SLIME.
4     ;;;
5 heller 1.60 ;;; Created 2003
6 heller 1.1 ;;;
7     ;;; This code has been placed in the Public Domain. All warranties
8 heller 1.74 ;;; are disclaimed.
9 heller 1.1 ;;;
10    
11 heller 1.21 (in-package :swank-backend)
12    
13 heller 1.1 (eval-when (:compile-toplevel :load-toplevel :execute)
14     (require :sock)
15 heller 1.79 (require :process))
16 heller 1.1
17 heller 1.79 (import-from :excl *gray-stream-symbols* :swank-backend)
18 heller 1.1
19 mbaringer 1.53 ;;; swank-mop
20    
21 heller 1.60 ;; maybe better change MOP to ACLMOP ?
22     ;; CLOS also works in ACL5. --he
23     (import-swank-mop-symbols :clos '(:slot-definition-documentation))
24 mbaringer 1.53
25 mbaringer 1.54 (defun swank-mop:slot-definition-documentation (slot)
26 heller 1.62 (documentation slot t))
27 mbaringer 1.53
28 heller 1.79
29 heller 1.8 ;;;; TCP Server
30 heller 1.1
31 heller 1.21 (defimplementation preferred-communication-style ()
32     :spawn)
33 heller 1.12
34 heller 1.14 (defimplementation create-socket (host port)
35     (socket:make-socket :connect :passive :local-port port
36     :local-host host :reuse-address t))
37 heller 1.5
38 heller 1.10 (defimplementation local-port (socket)
39 heller 1.6 (socket:local-port socket))
40    
41 heller 1.10 (defimplementation close-socket (socket)
42 heller 1.6 (close socket))
43    
44 dcrosher 1.85 (defimplementation accept-connection (socket &key external-format buffering
45     timeout)
46     (declare (ignore buffering timeout))
47 heller 1.66 (let ((ef (or external-format :iso-latin-1-unix))
48     (s (socket:accept-connection socket :wait t)))
49     (set-external-format s ef)
50 heller 1.63 s))
51 heller 1.6
52 heller 1.74 (defun find-external-format (coding-system)
53     #+(version>= 6)
54 heller 1.75 (let* ((name (ecase coding-system
55 heller 1.64 (:iso-latin-1-unix :latin1)
56 heller 1.79 (:utf-8-unix :utf8)
57 heller 1.74 (:emacs-mule-unix :emacs-mule))))
58 heller 1.79 (excl:crlf-base-ef (excl:find-external-format name :try-variant t)))
59     #-(version>= 6)
60     (ecase coding-system
61     (:iso-latin-1-unix :default)))
62 heller 1.74
63     (defun set-external-format (stream external-format)
64 heller 1.75 (setf (stream-external-format stream)
65     (find-external-format external-format)))
66 heller 1.64
67 heller 1.34 (defimplementation format-sldb-condition (c)
68     (princ-to-string c))
69    
70     (defimplementation condition-references (c)
71 heller 1.39 (declare (ignore c))
72 heller 1.34 '())
73 heller 1.7
74 heller 1.39 (defimplementation call-with-syntax-hooks (fn)
75     (funcall fn))
76    
77 heller 1.9 ;;;; Unix signals
78    
79 heller 1.10 (defimplementation call-without-interrupts (fn)
80 heller 1.9 (excl:without-interrupts (funcall fn)))
81    
82 heller 1.10 (defimplementation getpid ()
83 heller 1.8 (excl::getpid))
84 heller 1.6
85 heller 1.15 (defimplementation lisp-implementation-type-name ()
86     "allegro")
87    
88 pseibel 1.28 (defimplementation set-default-directory (directory)
89 mkoeppe 1.86 (let* ((dir (namestring (truename (merge-pathnames directory)))))
90     (setf *default-pathname-defaults* (pathname (excl:chdir dir)))
91 heller 1.66 dir))
92 pseibel 1.28
93 heller 1.35 (defimplementation default-directory ()
94 heller 1.66 (namestring (excl:current-directory)))
95 heller 1.35
96 heller 1.8 ;;;; Misc
97 heller 1.1
98 heller 1.21 (defimplementation arglist (symbol)
99 lgorrie 1.30 (handler-case (excl:arglist symbol)
100     (simple-error () :not-available)))
101 heller 1.21
102     (defimplementation macroexpand-all (form)
103     (excl::walk form))
104 heller 1.1
105 heller 1.10 (defimplementation describe-symbol-for-emacs (symbol)
106 heller 1.1 (let ((result '()))
107     (flet ((doc (kind &optional (sym symbol))
108     (or (documentation sym kind) :not-documented))
109     (maybe-push (property value)
110     (when value
111     (setf result (list* property value result)))))
112     (maybe-push
113     :variable (when (boundp symbol)
114     (doc 'variable)))
115     (maybe-push
116     :function (if (fboundp symbol)
117     (doc 'function)))
118     (maybe-push
119     :class (if (find-class symbol nil)
120     (doc 'class)))
121     result)))
122    
123 heller 1.20 (defimplementation describe-definition (symbol namespace)
124     (ecase namespace
125     (:variable
126     (describe symbol))
127     ((:function :generic-function)
128     (describe (symbol-function symbol)))
129     (:class
130     (describe (find-class symbol)))))
131 heller 1.10
132 lgorrie 1.43 (defimplementation make-stream-interactive (stream)
133     (setf (interactive-stream-p stream) t))
134    
135 heller 1.8 ;;;; Debugger
136    
137 heller 1.1 (defvar *sldb-topframe*)
138 heller 1.4
139 heller 1.10 (defimplementation call-with-debugging-environment (debugger-loop-fn)
140 heller 1.71 (let ((*sldb-topframe* (find-topframe))
141 heller 1.20 (excl::*break-hook* nil))
142 heller 1.4 (funcall debugger-loop-fn)))
143 heller 1.1
144 heller 1.71 (defun find-topframe ()
145 heller 1.73 (let ((skip-frames 3))
146     (do ((f (excl::int-newest-frame) (next-frame f))
147     (i 0 (1+ i)))
148     ((= i skip-frames) f))))
149 heller 1.71
150 heller 1.42 (defun next-frame (frame)
151     (let ((next (excl::int-next-older-frame frame)))
152     (cond ((not next) nil)
153     ((debugger:frame-visible-p next) next)
154     (t (next-frame next)))))
155    
156 heller 1.1 (defun nth-frame (index)
157 heller 1.42 (do ((frame *sldb-topframe* (next-frame frame))
158 heller 1.1 (i index (1- i)))
159     ((zerop i) frame)))
160    
161 heller 1.20 (defimplementation compute-backtrace (start end)
162 heller 1.1 (let ((end (or end most-positive-fixnum)))
163 heller 1.42 (loop for f = (nth-frame start) then (next-frame f)
164 heller 1.1 for i from start below end
165     while f
166 heller 1.42 collect f)))
167 heller 1.1
168 heller 1.20 (defimplementation print-frame (frame stream)
169     (debugger:output-frame stream frame :moderate))
170 heller 1.4
171 heller 1.10 (defimplementation frame-locals (index)
172 heller 1.1 (let ((frame (nth-frame index)))
173     (loop for i from 0 below (debugger:frame-number-vars frame)
174 mbaringer 1.19 collect (list :name (debugger:frame-var-name frame i)
175 heller 1.1 :id 0
176 mbaringer 1.19 :value (debugger:frame-var-value frame i)))))
177 heller 1.1
178 heller 1.39 (defimplementation frame-var-value (frame var)
179     (let ((frame (nth-frame frame)))
180     (debugger:frame-var-value frame var)))
181    
182 heller 1.10 (defimplementation frame-catch-tags (index)
183 heller 1.1 (declare (ignore index))
184     nil)
185    
186 heller 1.21 (defimplementation disassemble-frame (index)
187     (disassemble (debugger:frame-function (nth-frame index))))
188    
189 heller 1.10 (defimplementation frame-source-location-for-emacs (index)
190 lgorrie 1.37 (let* ((frame (nth-frame index))
191     (expr (debugger:frame-expression frame))
192     (fspec (first expr)))
193     (second (first (fspec-definition-locations fspec)))))
194 heller 1.4
195 heller 1.10 (defimplementation eval-in-frame (form frame-number)
196 heller 1.73 (let ((frame (nth-frame frame-number)))
197     ;; let-bind lexical variables
198     (let ((vars (loop for i below (debugger:frame-number-vars frame)
199     for name = (debugger:frame-var-name frame i)
200     if (symbolp name)
201     collect `(,name ',(debugger:frame-var-value frame i)))))
202     (debugger:eval-form-in-context
203     `(let* ,vars ,form)
204     (debugger:environment-of-frame frame)))))
205 heller 1.10
206 heller 1.11 (defimplementation return-from-frame (frame-number form)
207     (let ((frame (nth-frame frame-number)))
208     (multiple-value-call #'debugger:frame-return
209     frame (debugger:eval-form-in-context
210 heller 1.20 form
211     (debugger:environment-of-frame frame)))))
212 heller 1.67
213 heller 1.11 (defimplementation restart-frame (frame-number)
214     (let ((frame (nth-frame frame-number)))
215 heller 1.69 (cond ((debugger:frame-retryable-p frame)
216     (apply #'debugger:frame-retry frame (debugger:frame-function frame)
217     (cdr (debugger:frame-expression frame))))
218     (t "Frame is not retryable"))))
219 heller 1.67
220 heller 1.8 ;;;; Compiler hooks
221    
222 heller 1.1 (defvar *buffer-name* nil)
223     (defvar *buffer-start-position*)
224     (defvar *buffer-string*)
225 lgorrie 1.33 (defvar *compile-filename* nil)
226 heller 1.1
227 heller 1.66 (defun compiler-note-p (object)
228     (member (type-of object) '(excl::compiler-note compiler::compiler-note)))
229    
230     (defun compiler-undefined-functions-called-warning-p (object)
231 heller 1.75 #+(version>= 6)
232 heller 1.66 (typep object 'excl:compiler-undefined-functions-called-warning))
233 heller 1.64
234     (deftype compiler-note ()
235     `(satisfies compiler-note-p))
236    
237 heller 1.66 (defun signal-compiler-condition (&rest args)
238     (signal (apply #'make-condition 'compiler-condition args)))
239    
240 heller 1.1 (defun handle-compiler-warning (condition)
241 heller 1.66 (declare (optimize (debug 3) (speed 0) (space 0)))
242     (cond ((and (not *buffer-name*)
243     (compiler-undefined-functions-called-warning-p condition))
244     (handle-undefined-functions-warning condition))
245     (t
246     (signal-compiler-condition
247     :original-condition condition
248     :severity (etypecase condition
249     (warning :warning)
250     (compiler-note :note))
251     :message (format nil "~A" condition)
252     :location (location-for-warning condition)))))
253    
254     (defun location-for-warning (condition)
255 heller 1.1 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
256 heller 1.66 (cond (*buffer-name*
257     (make-location
258     (list :buffer *buffer-name*)
259     (list :position *buffer-start-position*)))
260     (loc
261     (destructuring-bind (file . pos) loc
262     (make-location
263     (list :file (namestring (truename file)))
264     (list :position (1+ pos)))))
265     (t
266     (list :error "No error location available.")))))
267    
268     (defun handle-undefined-functions-warning (condition)
269     (let ((fargs (slot-value condition 'excl::format-arguments)))
270 heller 1.67 (loop for (fname . pos-file) in (car fargs) do
271     (loop for (pos file) in pos-file do
272     (signal-compiler-condition
273     :original-condition condition
274     :severity :warning
275     :message (format nil "Undefined function referenced: ~S"
276     fname)
277     :location (make-location (list :file file)
278     (list :position (1+ pos))))))))
279 lgorrie 1.33
280     (defimplementation call-with-compilation-hooks (function)
281 heller 1.64 (handler-bind ((warning #'handle-compiler-warning)
282 heller 1.70 ;;(compiler-note #'handle-compiler-warning)
283     )
284 lgorrie 1.33 (funcall function)))
285 heller 1.1
286 heller 1.74 (defimplementation swank-compile-file (filename load-p
287     &optional external-format)
288 lgorrie 1.33 (with-compilation-hooks ()
289 heller 1.74 (let ((*buffer-name* nil)
290     (*compile-filename* filename)
291     (ef (if external-format
292     (find-external-format external-format)
293     :default)))
294     (compile-file *compile-filename* :load-after-compile load-p
295     :external-format ef))))
296 heller 1.1
297 heller 1.46 (defun call-with-temp-file (fn)
298     (let ((tmpname (system:make-temp-file-name)))
299     (unwind-protect
300     (with-open-file (file tmpname :direction :output :if-exists :error)
301     (funcall fn file tmpname))
302     (delete-file tmpname))))
303    
304     (defun compile-from-temp-file (string)
305     (call-with-temp-file
306     (lambda (stream filename)
307     (write-string string stream)
308     (finish-output stream)
309 mkoeppe 1.83 (let ((binary-filename
310     (excl:without-redefinition-warnings
311     ;; Suppress Allegro's redefinition warnings; they are
312     ;; pointless when we are compiling via a temporary
313     ;; file.
314     (compile-file filename :load-after-compile t))))
315 heller 1.46 (when binary-filename
316     (delete-file binary-filename))))))
317    
318 pseibel 1.51 (defimplementation swank-compile-string (string &key buffer position directory)
319 heller 1.46 ;; We store the source buffer in excl::*source-pathname* as a string
320 heller 1.50 ;; of the form <buffername>;<start-offset>. Quite ugly encoding, but
321 heller 1.46 ;; the fasl file is corrupted if we use some other datatype.
322 lgorrie 1.33 (with-compilation-hooks ()
323 heller 1.20 (let ((*buffer-name* buffer)
324 heller 1.1 (*buffer-start-position* position)
325 pseibel 1.51 (*buffer-string* string)
326     (*default-pathname-defaults*
327     (if directory (merge-pathnames (pathname directory))
328     *default-pathname-defaults*)))
329 heller 1.46 (compile-from-temp-file
330     (format nil "~S ~S~%~A"
331     `(in-package ,(package-name *package*))
332     `(eval-when (:compile-toplevel :load-toplevel)
333     (setq excl::*source-pathname*
334 heller 1.52 ',(format nil "~A;~D" buffer position)))
335 heller 1.46 string)))))
336 heller 1.1
337 heller 1.8 ;;;; Definition Finding
338    
339 heller 1.32 (defun fspec-primary-name (fspec)
340     (etypecase fspec
341 heller 1.62 (symbol fspec)
342     (list (fspec-primary-name (second fspec)))))
343 heller 1.32
344 heller 1.62 ;; If Emacs uses DOS-style eol conventions, \n\r are considered as a
345     ;; single character, but file-position counts them as two. Here we do
346     ;; our own conversion.
347     (defun count-cr (file pos)
348     (let* ((bufsize 256)
349 heller 1.63 (type '(unsigned-byte 8))
350     (buf (make-array bufsize :element-type type))
351 heller 1.62 (cr-count 0))
352 heller 1.63 (with-open-file (stream file :direction :input :element-type type)
353     (loop for bytes-read = (read-sequence buf stream) do
354     (incf cr-count (count (char-code #\return) buf
355     :end (min pos bytes-read)))
356     (decf pos bytes-read)
357     (when (<= pos 0)
358     (return cr-count))))))
359 heller 1.62
360 mkoeppe 1.81 (defun find-definition-in-file (fspec type file top-level)
361     (let* ((part
362     (or (scm::find-definition-in-definition-group
363     fspec type (scm:section-file :file file)
364     :top-level top-level)
365     (scm::find-definition-in-definition-group
366     (fspec-primary-name fspec)
367     type (scm:section-file :file file)
368     :top-level top-level)))
369     (start (and part
370     (scm::source-part-start part)))
371 heller 1.60 (pos (if start
372 heller 1.62 (list :position (1+ (- start (count-cr file start))))
373     (list :function-name (string (fspec-primary-name fspec))))))
374     (make-location (list :file (namestring (truename file)))
375     pos)))
376 heller 1.60
377     (defun find-definition-in-buffer (filename)
378     (let ((pos (position #\; filename :from-end t)))
379     (make-location
380     (list :buffer (subseq filename 0 pos))
381     (list :position (parse-integer (subseq filename (1+ pos)))))))
382    
383 mkoeppe 1.81 (defun find-fspec-location (fspec type file top-level)
384     (etypecase file
385     (pathname
386     (find-definition-in-file fspec type file top-level))
387     ((member :top-level)
388     (list :error (format nil "Defined at toplevel: ~A"
389     (fspec->string fspec))))
390     (string
391     (find-definition-in-buffer file))))
392 lgorrie 1.49
393 heller 1.50 (defun fspec->string (fspec)
394 lgorrie 1.49 (etypecase fspec
395 heller 1.50 (symbol (let ((*package* (find-package :keyword)))
396     (prin1-to-string fspec)))
397     (list (format nil "(~A ~A)"
398     (prin1-to-string (first fspec))
399     (let ((*package* (find-package :keyword)))
400     (prin1-to-string (second fspec)))))))
401 heller 1.21
402     (defun fspec-definition-locations (fspec)
403 mkoeppe 1.87 (cond
404     ((and (listp fspec)
405     (eql (car fspec) :top-level-form))
406     (destructuring-bind (top-level-form file position) fspec
407     (list
408     (list (list nil fspec)
409     (make-location (list :buffer file)
410 heller 1.90 (list :position position t))))))
411     ((and (listp fspec) (eq (car fspec) :internal))
412     (destructuring-bind (_internal next _n) fspec
413     (fspec-definition-locations next)))
414 mkoeppe 1.87 (t
415     (let ((defs (excl::find-source-file fspec)))
416     (if (null defs)
417     (list
418     (list (list nil fspec)
419     (list :error
420     (format nil "Unknown source location for ~A"
421     (fspec->string fspec)))))
422 mkoeppe 1.81 (loop for (fspec type file top-level) in defs
423 mkoeppe 1.87 collect (list (list type fspec)
424     (find-fspec-location fspec type file top-level))))))))
425 heller 1.21
426     (defimplementation find-definitions (symbol)
427     (fspec-definition-locations symbol))
428 heller 1.1
429 heller 1.8 ;;;; XREF
430    
431 heller 1.21 (defmacro defxref (name relation name1 name2)
432     `(defimplementation ,name (x)
433     (xref-result (xref:get-relation ,relation ,name1 ,name2))))
434    
435     (defxref who-calls :calls :wild x)
436 heller 1.70 (defxref calls-who :calls x :wild)
437 heller 1.21 (defxref who-references :uses :wild x)
438     (defxref who-binds :binds :wild x)
439     (defxref who-macroexpands :macro-calls :wild x)
440     (defxref who-sets :sets :wild x)
441    
442     (defun xref-result (fspecs)
443     (loop for fspec in fspecs
444     append (fspec-definition-locations fspec)))
445 heller 1.46
446     ;; list-callers implemented by groveling through all fbound symbols.
447     ;; Only symbols are considered. Functions in the constant pool are
448 heller 1.71 ;; searched recursively. Closure environments are ignored at the
449 heller 1.46 ;; moment (constants in methods are therefore not found).
450    
451     (defun map-function-constants (function fn depth)
452     "Call FN with the elements of FUNCTION's constant pool."
453     (do ((i 0 (1+ i))
454     (max (excl::function-constant-count function)))
455     ((= i max))
456     (let ((c (excl::function-constant function i)))
457     (cond ((and (functionp c)
458     (not (eq c function))
459     (plusp depth))
460     (map-function-constants c fn (1- depth)))
461     (t
462     (funcall fn c))))))
463    
464 heller 1.64 (defun in-constants-p (fun symbol)
465     (map-function-constants fun
466     (lambda (c)
467     (when (eq c symbol)
468     (return-from in-constants-p t)))
469     3))
470 heller 1.70
471 heller 1.46 (defun function-callers (name)
472     (let ((callers '()))
473     (do-all-symbols (sym)
474     (when (fboundp sym)
475     (let ((fn (fdefinition sym)))
476     (when (in-constants-p fn name)
477     (push sym callers)))))
478     callers))
479    
480     (defimplementation list-callers (name)
481     (xref-result (function-callers name)))
482 heller 1.4
483 heller 1.70 (defimplementation list-callees (name)
484     (let ((result '()))
485     (map-function-constants (fdefinition name)
486     (lambda (c)
487     (when (fboundp c)
488     (push c result)))
489     2)
490     (xref-result result)))
491    
492 mkoeppe 1.83 ;;;; Profiling
493    
494 heller 1.89 ;; Per-function profiling based on description in
495     ;; http://www.franz.com/support/documentation/8.0/doc/runtime-analyzer.htm#data-collection-control-2
496    
497     (defvar *profiled-functions* ())
498     (defvar *profile-depth* 0)
499    
500     (defmacro with-redirected-y-or-n-p (&body body)
501     ;; If the profiler is restarted when the data from the previous
502     ;; session is not reported yet, the user is warned via Y-OR-N-P.
503     ;; As the CL:Y-OR-N-P question is (for some reason) not directly
504     ;; sent to the Slime user, the function CL:Y-OR-N-P is temporarily
505     ;; overruled.
506     `(let* ((pkg (find-package "common-lisp"))
507     (saved-pdl (excl::package-definition-lock pkg))
508     (saved-ynp (symbol-function 'cl:y-or-n-p)))
509    
510     (setf (excl::package-definition-lock pkg) nil
511     (symbol-function 'cl:y-or-n-p) (symbol-function
512     (find-symbol "y-or-n-p-in-emacs"
513     "swank")))
514     (unwind-protect
515     (progn ,@body)
516    
517     (setf (symbol-function 'cl:y-or-n-p) saved-ynp
518     (excl::package-definition-lock pkg) saved-pdl))))
519    
520     (defun start-acl-profiler ()
521     (with-redirected-y-or-n-p
522     (prof:start-profiler :type :time :count t
523     :start-sampling-p nil :verbose nil)))
524     (defun acl-profiler-active-p ()
525     (not (eq (prof:profiler-status :verbose nil) :inactive)))
526    
527     (defun stop-acl-profiler ()
528     (prof:stop-profiler :verbose nil))
529    
530     (excl:def-fwrapper profile-fwrapper (&rest args)
531     ;; Ensures sampling is done during the execution of the function,
532     ;; taking into account recursion.
533     (declare (ignore args))
534     (cond ((zerop *profile-depth*)
535     (let ((*profile-depth* (1+ *profile-depth*)))
536     (prof:start-sampling)
537     (unwind-protect (excl:call-next-fwrapper)
538     (prof:stop-sampling))))
539     (t
540     (excl:call-next-fwrapper))))
541    
542     (defimplementation profile (fname)
543     (unless (acl-profiler-active-p)
544     (start-acl-profiler))
545     (excl:fwrap fname 'profile-fwrapper 'profile-fwrapper)
546     (push fname *profiled-functions*))
547    
548     (defimplementation profiled-functions ()
549     *profiled-functions*)
550    
551     (defimplementation unprofile (fname)
552     (excl:funwrap fname 'profile-fwrapper)
553     (setq *profiled-functions* (remove fname *profiled-functions*)))
554    
555 mkoeppe 1.83 (defimplementation profile-report ()
556 heller 1.89 (prof:show-flat-profile :verbose nil)
557     (when *profiled-functions*
558     (start-acl-profiler)))
559    
560     (defimplementation profile-reset ()
561     (when (acl-profiler-active-p)
562     (stop-acl-profiler)
563     (start-acl-profiler))
564     "Reset profiling counters.")
565 mkoeppe 1.83
566 heller 1.18 ;;;; Inspecting
567    
568 mbaringer 1.56 (defclass acl-inspector (inspector)
569     ())
570    
571     (defimplementation make-default-inspector ()
572     (make-instance 'acl-inspector))
573    
574 heller 1.62 (defmethod inspect-for-emacs ((f function) inspector)
575     inspector
576 mbaringer 1.56 (values "A function."
577 heller 1.62 (append
578     (label-value-line "Name" (function-name f))
579     `("Formals" ,(princ-to-string (arglist f)) (:newline))
580     (let ((doc (documentation (excl::external-fn_symdef f) 'function)))
581     (when doc
582     `("Documentation:" (:newline) ,doc))))))
583    
584     (defmethod inspect-for-emacs ((o t) (inspector acl-inspector))
585     inspector
586     (values "A value." (allegro-inspect o)))
587    
588     (defmethod inspect-for-emacs ((o function) (inspector acl-inspector))
589     inspector
590     (values "A function." (allegro-inspect o)))
591    
592 heller 1.82 (defmethod inspect-for-emacs ((o standard-object) (inspector acl-inspector))
593     inspector
594 mbaringer 1.84 (values (format nil "~A is a standard-object." o) (allegro-inspect o)))
595 heller 1.82
596 heller 1.62 (defun allegro-inspect (o)
597     (loop for (d dd) on (inspect::inspect-ctl o)
598 heller 1.73 append (frob-allegro-field-def o d)
599     until (eq d dd)))
600 heller 1.62
601 heller 1.73 (defun frob-allegro-field-def (object def)
602 heller 1.62 (with-struct (inspect::field-def- name type access) def
603 heller 1.73 (ecase type
604 heller 1.79 ((:unsigned-word :unsigned-byte :unsigned-natural
605     :unsigned-long :unsigned-half-long
606     :unsigned-3byte)
607 heller 1.73 (label-value-line name (inspect::component-ref-v object access type)))
608     ((:lisp :value)
609     (label-value-line name (inspect::component-ref object access)))
610     (:indirect
611     (destructuring-bind (prefix count ref set) access
612     (declare (ignore set prefix))
613     (loop for i below (funcall count object)
614     append (label-value-line (format nil "~A-~D" name i)
615     (funcall ref object i))))))))
616 mbaringer 1.55
617 heller 1.18 ;;;; Multithreading
618 heller 1.8
619 mbaringer 1.91 (defimplementation initialize-multiprocessing (continuation)
620     (mp:start-scheduler)
621     (funcall continuation))
622 heller 1.8
623 heller 1.10 (defimplementation spawn (fn &key name)
624 lgorrie 1.47 (mp:process-run-function name fn))
625 heller 1.8
626 heller 1.40 (defvar *id-lock* (mp:make-process-lock :name "id lock"))
627     (defvar *thread-id-counter* 0)
628    
629     (defimplementation thread-id (thread)
630     (mp:with-process-lock (*id-lock*)
631     (or (getf (mp:process-property-list thread) 'id)
632     (setf (getf (mp:process-property-list thread) 'id)
633     (incf *thread-id-counter*)))))
634    
635     (defimplementation find-thread (id)
636     (find id mp:*all-processes*
637     :key (lambda (p) (getf (mp:process-property-list p) 'id))))
638    
639 heller 1.13 (defimplementation thread-name (thread)
640     (mp:process-name thread))
641 heller 1.8
642 heller 1.13 (defimplementation thread-status (thread)
643     (format nil "~A ~D" (mp:process-whostate thread)
644     (mp:process-priority thread)))
645 heller 1.8
646 heller 1.10 (defimplementation make-lock (&key name)
647 heller 1.8 (mp:make-process-lock :name name))
648    
649 heller 1.10 (defimplementation call-with-lock-held (lock function)
650 heller 1.8 (mp:with-process-lock (lock) (funcall function)))
651 heller 1.12
652     (defimplementation current-thread ()
653     mp:*current-process*)
654    
655     (defimplementation all-threads ()
656 heller 1.13 (copy-list mp:*all-processes*))
657 heller 1.12
658     (defimplementation interrupt-thread (thread fn)
659     (mp:process-interrupt thread fn))
660    
661 heller 1.16 (defimplementation kill-thread (thread)
662     (mp:process-kill thread))
663    
664 heller 1.12 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
665    
666     (defstruct (mailbox (:conc-name mailbox.))
667     (mutex (mp:make-process-lock :name "process mailbox"))
668     (queue '() :type list))
669    
670     (defun mailbox (thread)
671     "Return THREAD's mailbox."
672     (mp:with-process-lock (*mailbox-lock*)
673     (or (getf (mp:process-property-list thread) 'mailbox)
674     (setf (getf (mp:process-property-list thread) 'mailbox)
675     (make-mailbox)))))
676    
677     (defimplementation send (thread message)
678     (let* ((mbox (mailbox thread))
679     (mutex (mailbox.mutex mbox)))
680 heller 1.25 (mp:process-wait-with-timeout
681     "yielding before sending" 0.1
682     (lambda ()
683     (mp:with-process-lock (mutex)
684 heller 1.26 (< (length (mailbox.queue mbox)) 10))))
685 heller 1.12 (mp:with-process-lock (mutex)
686     (setf (mailbox.queue mbox)
687     (nconc (mailbox.queue mbox) (list message))))))
688    
689     (defimplementation receive ()
690     (let* ((mbox (mailbox mp:*current-process*))
691     (mutex (mailbox.mutex mbox)))
692     (mp:process-wait "receive" #'mailbox.queue mbox)
693     (mp:with-process-lock (mutex)
694     (pop (mailbox.queue mbox)))))
695 mbaringer 1.27
696     (defimplementation quit-lisp ()
697     (excl:exit 0 :quiet t))
698 mbaringer 1.68
699 heller 1.69
700 mbaringer 1.68 ;;Trace implementations
701     ;;In Allegro 7.0, we have:
702     ;; (trace <name>)
703     ;; (trace ((method <name> <qualifier>? (<specializer>+))))
704     ;; (trace ((labels <name> <label-name>)))
705     ;; (trace ((labels (method <name> (<specializer>+)) <label-name>)))
706     ;; <name> can be a normal name or a (setf name)
707    
708 heller 1.70 (defimplementation toggle-trace (spec)
709 lgorrie 1.72 (ecase (car spec)
710     ((setf)
711     (toggle-trace-aux spec))
712 heller 1.70 (:defgeneric (toggle-trace-generic-function-methods (second spec)))
713 heller 1.71 ((setf :defmethod :labels :flet)
714 heller 1.70 (toggle-trace-aux (process-fspec-for-allegro spec)))
715 lgorrie 1.72 (:call
716 heller 1.70 (destructuring-bind (caller callee) (cdr spec)
717     (toggle-trace-aux callee
718     :inside (list (process-fspec-for-allegro caller)))))))
719    
720     (defun tracedp (fspec)
721 heller 1.71 (member fspec (eval '(trace)) :test #'equal))
722 heller 1.70
723     (defun toggle-trace-aux (fspec &rest args)
724     (cond ((tracedp fspec)
725     (eval `(untrace ,fspec))
726     (format nil "~S is now untraced." fspec))
727     (t
728     (eval `(trace (,fspec ,@args)))
729     (format nil "~S is now traced." fspec))))
730    
731     (defun toggle-trace-generic-function-methods (name)
732 mbaringer 1.68 (let ((methods (mop:generic-function-methods (fdefinition name))))
733 heller 1.70 (cond ((tracedp name)
734 mbaringer 1.68 (eval `(untrace ,name))
735     (dolist (method methods (format nil "~S is now untraced." name))
736     (excl:funtrace (mop:method-function method))))
737     (t
738 lgorrie 1.72 (eval `(trace (,name)))
739 heller 1.70 (dolist (method methods (format nil "~S is now traced." name))
740 mbaringer 1.68 (excl:ftrace (mop:method-function method)))))))
741    
742     (defun process-fspec-for-allegro (fspec)
743     (cond ((consp fspec)
744     (ecase (first fspec)
745 heller 1.71 ((setf) fspec)
746 mbaringer 1.68 ((:defun :defgeneric) (second fspec))
747     ((:defmethod) `(method ,@(rest fspec)))
748 heller 1.69 ((:labels) `(labels ,(process-fspec-for-allegro (second fspec))
749     ,(third fspec)))
750     ((:flet) `(flet ,(process-fspec-for-allegro (second fspec))
751     ,(third fspec)))))
752 mbaringer 1.68 (t
753     fspec)))
754 heller 1.88
755    
756     ;;;; Weak hashtables
757    
758     (defimplementation make-weak-key-hash-table (&rest args)
759     (apply #'make-hash-table :weak-keys t args))
760    
761     (defimplementation make-weak-value-hash-table (&rest args)
762     (apply #'make-hash-table :values :weak args))

  ViewVC Help
Powered by ViewVC 1.1.5