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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.67 - (show annotations)
Tue Aug 18 10:42:07 2009 UTC (4 years, 7 months ago) by mevenson
Branch: MAIN
Changes since 1.66: +90 -76 lines
Add multithreading code for abcl-0.16. (Tobias Rittweiler)

  * swank-abcl.lisp: A multithread implementation taking advantage
    		     of the new synchronization primitives in abcl-0.16.
1 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;;*"; -*-
2 ;;;
3 ;;; swank-abcl.lisp --- Armedbear CL specific code for SLIME.
4 ;;;
5 ;;; Adapted from swank-acl.lisp, Andras Simon, 2004
6 ;;;
7 ;;; This code has been placed in the Public Domain. All warranties
8 ;;; are disclaimed.
9 ;;;
10
11 (in-package :swank-backend)
12
13 (eval-when (:compile-toplevel :load-toplevel :execute)
14 (require :collect) ;just so that it doesn't spoil the flying letters
15 (require :pprint))
16
17 (defun sys::break (&optional (format-control "BREAK called")
18 &rest format-arguments)
19 (let ((*saved-backtrace* (backtrace-as-list-ignoring-swank-calls)))
20 (with-simple-restart (continue "Return from BREAK.")
21 (invoke-debugger
22 (sys::%make-condition 'simple-condition
23 (list :format-control format-control
24 :format-arguments format-arguments))))
25 nil))
26
27 (defimplementation make-output-stream (write-string)
28 (ext:make-slime-output-stream write-string))
29
30 (defimplementation make-input-stream (read-string)
31 (ext:make-slime-input-stream read-string
32 (make-synonym-stream '*standard-output*)))
33
34 (defimplementation call-with-compilation-hooks (function)
35 (funcall function))
36
37 ;;; swank-mop
38
39 ;;dummies and definition
40
41 (defclass standard-slot-definition ()())
42
43 ;(defun class-finalized-p (class) t)
44
45 (defun slot-definition-documentation (slot) #+nil (documentation slot 't))
46 (defun slot-definition-type (slot) t)
47 (defun class-prototype (class))
48 (defun generic-function-declarations (gf))
49 (defun specializer-direct-methods (spec) (mop::class-direct-methods spec))
50
51 (defun slot-definition-name (slot)
52 (mop::%slot-definition-name slot))
53
54 (defun class-slots (class)
55 (mop::%class-slots class))
56
57 (defun method-generic-function (method)
58 (mop::%method-generic-function method))
59
60 (defun method-function (method)
61 (mop::%method-function method))
62
63 (defun slot-boundp-using-class (class object slotdef)
64 (system::slot-boundp object (slot-definition-name slotdef)))
65
66 (defun slot-value-using-class (class object slotdef)
67 (system::slot-value object (slot-definition-name slotdef)))
68
69 (import-to-swank-mop
70 '( ;; classes
71 cl:standard-generic-function
72 standard-slot-definition ;;dummy
73 cl:method
74 cl:standard-class
75 ;; standard-class readers
76 mop::class-default-initargs
77 mop::class-direct-default-initargs
78 mop::class-direct-slots
79 mop::class-direct-subclasses
80 mop::class-direct-superclasses
81 mop::eql-specializer
82 mop::class-finalized-p
83 cl:class-name
84 mop::class-precedence-list
85 class-prototype ;;dummy
86 class-slots
87 specializer-direct-methods
88 ;; eql-specializer accessors
89 mop::eql-specializer-object
90 ;; generic function readers
91 mop::generic-function-argument-precedence-order
92 generic-function-declarations ;;dummy
93 mop::generic-function-lambda-list
94 mop::generic-function-methods
95 mop::generic-function-method-class
96 mop::generic-function-method-combination
97 mop::generic-function-name
98 ;; method readers
99 method-generic-function
100 method-function
101 mop::method-lambda-list
102 mop::method-specializers
103 mop::method-qualifiers
104 ;; slot readers
105 mop::slot-definition-allocation
106 slot-definition-documentation ;;dummy
107 mop::slot-definition-initargs
108 mop::slot-definition-initform
109 mop::slot-definition-initfunction
110 slot-definition-name
111 slot-definition-type ;;dummy
112 mop::slot-definition-readers
113 mop::slot-definition-writers
114 slot-boundp-using-class
115 slot-value-using-class
116 ))
117
118 ;;;; TCP Server
119
120
121 (defimplementation preferred-communication-style ()
122 #+#.(cl:if (cl:find-package :threads) '(:and) '(:or))
123 :spawn
124 #-#.(cl:if (cl:find-package :threads) '(:and) '(:or))
125 nil
126 )
127
128 (defimplementation create-socket (host port)
129 (ext:make-server-socket port))
130
131 (defimplementation local-port (socket)
132 (java:jcall (java:jmethod "java.net.ServerSocket" "getLocalPort") socket))
133
134 (defimplementation close-socket (socket)
135 (ext:server-socket-close socket))
136
137 (defimplementation accept-connection (socket
138 &key external-format buffering timeout)
139 (declare (ignore buffering timeout))
140 (ext:get-socket-stream (ext:socket-accept socket)
141 :external-format external-format))
142
143 ;;;; External formats
144
145 (defvar *external-format-to-coding-system*
146 '((:iso-8859-1 "latin-1" "iso-latin-1" "iso-8859-1")
147 ((:iso-8859-1 :eol-style :lf) "latin-1-unix" "iso-latin-1-unix" "iso-8859-1-unix")
148 (:utf-8 "utf-8")
149 ((:utf-8 :eol-style :lf) "utf-8-unix")
150 (:euc-jp "euc-jp")
151 ((:euc-jp :eol-style :lf) "euc-jp-unix")
152 (:us-ascii "us-ascii")
153 ((:us-ascii :eol-style :lf) "us-ascii-unix")))
154
155 (defimplementation find-external-format (coding-system)
156 (car (rassoc-if (lambda (x)
157 (member coding-system x :test #'equal))
158 *external-format-to-coding-system*)))
159
160 ;;;; Unix signals
161
162 (defimplementation call-without-interrupts (fn)
163 (funcall fn))
164
165 (defimplementation getpid ()
166 (handler-case
167 (let* ((runtime
168 (java:jstatic "getRuntime" "java.lang.Runtime"))
169 (command
170 (java:jnew-array-from-array
171 "java.lang.String" #("sh" "-c" "echo $PPID")))
172 (runtime-exec-jmethod
173 ;; Complicated because java.lang.Runtime.exec() is
174 ;; overloaded on a non-primitive type (array of
175 ;; java.lang.String), so we have to use the actual
176 ;; parameter instance to get java.lang.Class
177 (java:jmethod "java.lang.Runtime" "exec"
178 (java:jcall
179 (java:jmethod "java.lang.Object" "getClass")
180 command)))
181 (process
182 (java:jcall runtime-exec-jmethod runtime command))
183 (output
184 (java:jcall (java:jmethod "java.lang.Process" "getInputStream")
185 process)))
186 (java:jcall (java:jmethod "java.lang.Process" "waitFor")
187 process)
188 (loop :with b :do
189 (setq b
190 (java:jcall (java:jmethod "java.io.InputStream" "read")
191 output))
192 :until (member b '(-1 #x0a)) ; Either EOF or LF
193 :collecting (code-char b) :into result
194 :finally (return
195 (parse-integer (coerce result 'string)))))
196 (t () 0)))
197
198 (defimplementation lisp-implementation-type-name ()
199 "armedbear")
200
201 (defimplementation set-default-directory (directory)
202 (let ((dir (sys::probe-directory directory)))
203 (when dir (setf *default-pathname-defaults* dir))
204 (namestring dir)))
205
206
207 ;;;; Misc
208
209 (defimplementation arglist (fun)
210 (cond ((symbolp fun)
211 (multiple-value-bind (arglist present) (sys::arglist fun)
212 (if present arglist :not-available)))
213 (t :not-available)))
214
215 (defimplementation function-name (function)
216 (nth-value 2 (function-lambda-expression function)))
217
218 (defimplementation macroexpand-all (form)
219 (macroexpand form))
220
221 (defimplementation describe-symbol-for-emacs (symbol)
222 (let ((result '()))
223 (flet ((doc (kind &optional (sym symbol))
224 (or (documentation sym kind) :not-documented))
225 (maybe-push (property value)
226 (when value
227 (setf result (list* property value result)))))
228 (maybe-push
229 :variable (when (boundp symbol)
230 (doc 'variable)))
231 (maybe-push
232 :function (if (fboundp symbol)
233 (doc 'function)))
234 (maybe-push
235 :class (if (find-class symbol nil)
236 (doc 'class)))
237 result)))
238
239
240 (defimplementation describe-definition (symbol namespace)
241 (ecase namespace
242 (:variable
243 (describe symbol))
244 ((:function :generic-function)
245 (describe (symbol-function symbol)))
246 (:class
247 (describe (find-class symbol)))))
248
249 (defimplementation describe-definition (symbol namespace)
250 (ecase namespace
251 (:variable
252 (describe symbol))
253 ((:function :generic-function)
254 (describe (symbol-function symbol)))
255 (:class
256 (describe (find-class symbol)))))
257
258
259 ;;;; Debugger
260
261 (defvar *sldb-topframe*)
262
263 (defun backtrace-as-list-ignoring-swank-calls ()
264 (let ((list (ext:backtrace-as-list)))
265 (subseq list (1+ (or (position (intern "SWANK-DEBUGGER-HOOK" 'swank) list :key 'car) -1)))))
266
267 (defimplementation call-with-debugging-environment (debugger-loop-fn)
268 (let ((*sldb-topframe* (car (backtrace-as-list-ignoring-swank-calls)) #+nil (excl::int-newest-frame)))
269 (funcall debugger-loop-fn)))
270
271 (defun nth-frame (index)
272 (nth index (backtrace-as-list-ignoring-swank-calls)))
273
274 (defimplementation compute-backtrace (start end)
275 (let ((end (or end most-positive-fixnum)))
276 (loop for f in (subseq (backtrace-as-list-ignoring-swank-calls) start end)
277 collect f)))
278
279 (defimplementation print-frame (frame stream)
280 (write-string (string-trim '(#\space #\newline)
281 (prin1-to-string frame))
282 stream))
283
284 (defimplementation frame-locals (index)
285 `(,(list :name "??" :id 0 :value "??")))
286
287 #+nil
288 (defimplementation disassemble-frame (index)
289 (disassemble (debugger:frame-function (nth-frame index))))
290
291 (defimplementation frame-source-location (index)
292 (list :error (format nil "Cannot find source for frame: ~A"
293 (nth-frame index))))
294
295 #+nil
296 (defimplementation eval-in-frame (form frame-number)
297 (debugger:eval-form-in-context
298 form
299 (debugger:environment-of-frame (nth-frame frame-number))))
300
301 #+nil
302 (defimplementation return-from-frame (frame-number form)
303 (let ((frame (nth-frame frame-number)))
304 (multiple-value-call #'debugger:frame-return
305 frame (debugger:eval-form-in-context
306 form
307 (debugger:environment-of-frame frame)))))
308
309 ;;; XXX doesn't work for frames with arguments
310 #+nil
311 (defimplementation restart-frame (frame-number)
312 (let ((frame (nth-frame frame-number)))
313 (debugger:frame-retry frame (debugger:frame-function frame))))
314
315 ;;;; Compiler hooks
316
317 (defvar *buffer-name* nil)
318 (defvar *buffer-start-position*)
319 (defvar *buffer-string*)
320 (defvar *compile-filename*)
321
322 (in-package :swank-backend)
323
324 (defun handle-compiler-warning (condition)
325 (let ((loc (when (and jvm::*compile-file-pathname*
326 system::*source-position*)
327 (cons jvm::*compile-file-pathname* system::*source-position*))))
328 ;; filter condition signaled more than once.
329 (unless (member condition *abcl-signaled-conditions*)
330 (push condition *abcl-signaled-conditions*)
331 (signal (make-condition
332 'compiler-condition
333 :original-condition condition
334 :severity :warning
335 :message (format nil "~A" condition)
336 :location (cond (*buffer-name*
337 (make-location
338 (list :buffer *buffer-name*)
339 (list :offset *buffer-start-position* 0)))
340 (loc
341 (destructuring-bind (file . pos) loc
342 (make-location
343 (list :file (namestring (truename file)))
344 (list :position (1+ pos)))))
345 (t
346 (make-location
347 (list :file (namestring *compile-filename*))
348 (list :position 1)))))))))
349
350 (defvar *abcl-signaled-conditions*)
351
352 (defimplementation swank-compile-file (input-file output-file
353 load-p external-format)
354 (declare (ignore external-format))
355 (let ((jvm::*resignal-compiler-warnings* t)
356 (*abcl-signaled-conditions* nil))
357 (handler-bind ((warning #'handle-compiler-warning))
358 (let ((*buffer-name* nil)
359 (*compile-filename* input-file))
360 (multiple-value-bind (fn warn fail)
361 (compile-file input-file :output-file output-file)
362 (values fn warn
363 (or fail
364 (and load-p
365 (not (load fn))))))))))
366
367 (defimplementation swank-compile-string (string &key buffer position filename
368 policy)
369 (declare (ignore filename policy))
370 (let ((jvm::*resignal-compiler-warnings* t)
371 (*abcl-signaled-conditions* nil))
372 (handler-bind ((warning #'handle-compiler-warning))
373 (let ((*buffer-name* buffer)
374 (*buffer-start-position* position)
375 (*buffer-string* string))
376 (funcall (compile nil (read-from-string
377 (format nil "(~S () ~A)" 'lambda string))))
378 t))))
379
380 #|
381 ;;;; Definition Finding
382
383 (defun find-fspec-location (fspec type)
384 (let ((file (excl::fspec-pathname fspec type)))
385 (etypecase file
386 (pathname
387 (let ((start (scm:find-definition-in-file fspec type file)))
388 (make-location (list :file (namestring (truename file)))
389 (if start
390 (list :position (1+ start))
391 (list :function-name (string fspec))))))
392 ((member :top-level)
393 (list :error (format nil "Defined at toplevel: ~A" fspec)))
394 (null
395 (list :error (format nil "Unkown source location for ~A" fspec))))))
396
397 (defun fspec-definition-locations (fspec)
398 (let ((defs (excl::find-multiple-definitions fspec)))
399 (loop for (fspec type) in defs
400 collect (list fspec (find-fspec-location fspec type)))))
401
402 (defimplementation find-definitions (symbol)
403 (fspec-definition-locations symbol))
404
405 |#
406
407 (defun source-location (symbol)
408 (when (pathnamep (ext:source-pathname symbol))
409 `(((,symbol)
410 (:location
411 (:file ,(namestring (ext:source-pathname symbol)))
412 (:position ,(or (ext:source-file-position symbol) 1))
413 (:align t))))))
414
415
416 (defimplementation find-definitions (symbol)
417 (source-location symbol))
418
419 #|
420 Uncomment this if you have patched xref.lisp, as in
421 http://article.gmane.org/gmane.lisp.slime.devel/2425
422 Also, make sure that xref.lisp is loaded by modifying the armedbear
423 part of *sysdep-pathnames* in swank.loader.lisp.
424
425 ;;;; XREF
426 (setq pxref:*handle-package-forms* '(cl:in-package))
427
428 (defmacro defxref (name function)
429 `(defimplementation ,name (name)
430 (xref-results (,function name))))
431
432 (defxref who-calls pxref:list-callers)
433 (defxref who-references pxref:list-readers)
434 (defxref who-binds pxref:list-setters)
435 (defxref who-sets pxref:list-setters)
436 (defxref list-callers pxref:list-callers)
437 (defxref list-callees pxref:list-callees)
438
439 (defun xref-results (symbols)
440 (let ((xrefs '()))
441 (dolist (symbol symbols)
442 (push (list symbol (cadar (source-location symbol))) xrefs))
443 xrefs))
444 |#
445
446 ;;;; Inspecting
447
448 (defmethod emacs-inspect ((slot mop::slot-definition))
449 `("Name: " (:value ,(mop::%slot-definition-name slot))
450 (:newline)
451 "Documentation:" (:newline)
452 ,@(when (slot-definition-documentation slot)
453 `((:value ,(slot-definition-documentation slot)) (:newline)))
454 "Initialization:" (:newline)
455 " Args: " (:value ,(mop::%slot-definition-initargs slot)) (:newline)
456 " Form: " ,(if (mop::%slot-definition-initfunction slot)
457 `(:value ,(mop::%slot-definition-initform slot))
458 "#<unspecified>") (:newline)
459 " Function: " (:value ,(mop::%slot-definition-initfunction slot))
460 (:newline)))
461
462 (defmethod emacs-inspect ((f function))
463 `(,@(when (function-name f)
464 `("Name: "
465 ,(princ-to-string (function-name f)) (:newline)))
466 ,@(multiple-value-bind (args present)
467 (sys::arglist f)
468 (when present `("Argument list: " ,(princ-to-string args) (:newline))))
469 (:newline)
470 #+nil,@(when (documentation f t)
471 `("Documentation:" (:newline) ,(documentation f t) (:newline)))
472 ,@(when (function-lambda-expression f)
473 `("Lambda expression:"
474 (:newline) ,(princ-to-string (function-lambda-expression f)) (:newline)))))
475
476 #|
477
478 (defmethod emacs-inspect ((o t))
479 (let* ((class (class-of o))
480 (slots (mop::class-slots class)))
481 (mapcar (lambda (slot)
482 (let ((name (mop::slot-definition-name slot)))
483 (cons (princ-to-string name)
484 (slot-value o name))))
485 slots)))
486 |#
487
488 ;;;; Multithreading
489
490 #+#.(cl:if (cl:find-package :threads) '(:and) '(:or))
491 (progn
492 (defimplementation spawn (fn &key name)
493 (threads:make-thread (lambda () (funcall fn)) :name name))
494
495 (defvar *thread-plists* (make-hash-table) ; should be a weak table
496 "A hashtable mapping threads to a plist.")
497
498 (defvar *thread-id-counter* 0)
499
500 (defimplementation thread-id (thread)
501 (threads:synchronized-on *thread-plists*
502 (or (getf (gethash thread *thread-plists*) 'id)
503 (setf (getf (gethash thread *thread-plists*) 'id)
504 (incf *thread-id-counter*)))))
505
506 (defimplementation find-thread (id)
507 (find id (all-threads)
508 :key (lambda (thread)
509 (getf (gethash thread *thread-plists*) 'id))))
510
511 (defimplementation thread-name (thread)
512 (threads:thread-name thread))
513
514 (defimplementation thread-status (thread)
515 (format nil "Thread is ~:[dead~;alive~]" (threads:thread-alive-p thread)))
516
517 ;; XXX should be a weak hash table
518 (defparameter *thread-description-map* (make-hash-table))
519
520 (defimplementation thread-description (thread)
521 (synchronized-on *thread-description-map*
522 (or (gethash thread *thread-description-map*)
523 "No description available.")))
524
525 (defimplementation set-thread-description (thread description)
526 (synchronized-on *thread-description-map*
527 (setf (gethash thread *thread-description-map*) description)))
528
529 (defimplementation make-lock (&key name)
530 (declare (ignore name))
531 (threads:make-thread-lock))
532
533 (defimplementation call-with-lock-held (lock function)
534 (threads:with-thread-lock (lock) (funcall function)))
535
536 (defimplementation current-thread ()
537 (threads:current-thread))
538
539 (defimplementation all-threads ()
540 (copy-list (threads:mapcar-threads #'identity)))
541
542 (defimplementation thread-alive-p (thread)
543 (member thread (all-threads)))
544
545 (defimplementation interrupt-thread (thread fn)
546 (threads:interrupt-thread thread fn))
547
548 (defimplementation kill-thread (thread)
549 (threads:destroy-thread thread))
550
551 (defstruct mailbox
552 (queue '()))
553
554 (defun mailbox (thread)
555 "Return THREAD's mailbox."
556 (threads:synchronized-on *thread-plists*
557 (or (getf (gethash thread *thread-plists*) 'mailbox)
558 (setf (getf (gethash thread *thread-plists*) 'mailbox)
559 (make-mailbox)))))
560
561 (defimplementation send (thread message)
562 (let ((mbox (mailbox thread)))
563 (threads:synchronized-on mbox
564 (setf (mailbox-queue mbox)
565 (nconc (mailbox-queue mbox) (list message)))
566 (threads:object-notify-all mbox))))
567
568 (defimplementation receive-if (test &optional timeout)
569 (let* ((mbox (mailbox (current-thread))))
570 (assert (or (not timeout) (eq timeout t)))
571 (loop
572 (check-slime-interrupts)
573 (threads:synchronized-on mbox
574 (let* ((q (mailbox-queue mbox))
575 (tail (member-if test q)))
576 (when tail
577 (setf (mailbox-queue mbox) (nconc (ldiff q tail) (cdr tail)))
578 (return (car tail)))
579 (when (eq timeout t) (return (values nil t)))
580 (threads:object-wait mbox 0.3)))))))
581
582 (defimplementation quit-lisp ()
583 (ext:exit))

  ViewVC Help
Powered by ViewVC 1.1.5