Path: blob/master/elisp/slime/slime-1.2/swank-lispworks.lisp
1497 views
;;; -*- indent-tabs-mode: nil -*-1;;;2;;; swank-lispworks.lisp --- LispWorks specific code for SLIME.3;;;4;;; Created 2003, Helmut Eller5;;;6;;; This code has been placed in the Public Domain. All warranties7;;; are disclaimed.8;;;910(in-package :swank-backend)1112(eval-when (:compile-toplevel :load-toplevel :execute)13(require "comm")14(import-from :stream *gray-stream-symbols* :swank-backend))1516(import-swank-mop-symbols :clos '(:slot-definition-documentation17:slot-boundp-using-class18:slot-value-using-class19:slot-makunbound-using-class20:eql-specializer21:eql-specializer-object22:compute-applicable-methods-using-classes))2324(defun swank-mop:slot-definition-documentation (slot)25(documentation slot t))2627(defun swank-mop:slot-boundp-using-class (class object slotd)28(clos:slot-boundp-using-class class object29(clos:slot-definition-name slotd)))3031(defun swank-mop:slot-value-using-class (class object slotd)32(clos:slot-value-using-class class object33(clos:slot-definition-name slotd)))3435(defun (setf swank-mop:slot-value-using-class) (value class object slotd)36(setf (clos:slot-value-using-class class object37(clos:slot-definition-name slotd))38value))3940(defun swank-mop:slot-makunbound-using-class (class object slotd)41(clos:slot-makunbound-using-class class object42(clos:slot-definition-name slotd)))4344(defun swank-mop:compute-applicable-methods-using-classes (gf classes)45(clos::compute-applicable-methods-from-classes gf classes))4647;; lispworks doesn't have the eql-specializer class, it represents48;; them as a list of `(EQL ,OBJECT)49(deftype swank-mop:eql-specializer () 'cons)5051(defun swank-mop:eql-specializer-object (eql-spec)52(second eql-spec))5354(eval-when (:compile-toplevel :execute :load-toplevel)55(defvar *original-defimplementation* (macro-function 'defimplementation))56(defmacro defimplementation (&whole whole name args &body body57&environment env)58(declare (ignore args body))59`(progn60(dspec:record-definition '(defun ,name) (dspec:location)61:check-redefinition-p nil)62,(funcall *original-defimplementation* whole env))))6364;;; TCP server6566(defimplementation preferred-communication-style ()67:spawn)6869(defun socket-fd (socket)70(etypecase socket71(fixnum socket)72(comm:socket-stream (comm:socket-stream-socket socket))))7374(defimplementation create-socket (host port)75(multiple-value-bind (socket where errno)76#-(or lispworks4.1 (and macosx lispworks4.3))77(comm::create-tcp-socket-for-service port :address host)78#+(or lispworks4.1 (and macosx lispworks4.3))79(comm::create-tcp-socket-for-service port)80(cond (socket socket)81(t (error 'network-error82:format-control "~A failed: ~A (~D)"83:format-arguments (list where84(list #+unix (lw:get-unix-error errno))85errno))))))8687(defimplementation local-port (socket)88(nth-value 1 (comm:get-socket-address (socket-fd socket))))8990(defimplementation close-socket (socket)91(comm::close-socket (socket-fd socket)))9293(defimplementation accept-connection (socket94&key external-format buffering timeout)95(declare (ignore buffering))96(let* ((fd (comm::get-fd-from-socket socket)))97(assert (/= fd -1))98(assert (valid-external-format-p external-format))99(cond ((member (first external-format) '(:latin-1 :ascii))100(make-instance 'comm:socket-stream101:socket fd102:direction :io103:read-timeout timeout104:element-type 'base-char))105(t106(make-flexi-stream107(make-instance 'comm:socket-stream108:socket fd109:direction :io110:read-timeout timeout111:element-type '(unsigned-byte 8))112external-format)))))113114(defun make-flexi-stream (stream external-format)115(unless (member :flexi-streams *features*)116(error "Cannot use external format ~A without having installed flexi-streams in the inferior-lisp."117external-format))118(funcall (read-from-string "FLEXI-STREAMS:MAKE-FLEXI-STREAM")119stream120:external-format121(apply (read-from-string "FLEXI-STREAMS:MAKE-EXTERNAL-FORMAT")122external-format)))123124;;; Coding Systems125126(defun valid-external-format-p (external-format)127(member external-format *external-format-to-coding-system*128:test #'equal :key #'car))129130(defvar *external-format-to-coding-system*131'(((:latin-1 :eol-style :lf)132"latin-1-unix" "iso-latin-1-unix" "iso-8859-1-unix")133((:latin-1)134"latin-1" "iso-latin-1" "iso-8859-1")135((:utf-8) "utf-8")136((:utf-8 :eol-style :lf) "utf-8-unix")137((:euc-jp) "euc-jp")138((:euc-jp :eol-style :lf) "euc-jp-unix")139((:ascii) "us-ascii")140((:ascii :eol-style :lf) "us-ascii-unix")))141142(defimplementation find-external-format (coding-system)143(car (rassoc-if (lambda (x) (member coding-system x :test #'equal))144*external-format-to-coding-system*)))145146;;; Unix signals147148(defun sigint-handler ()149(with-simple-restart (continue "Continue from SIGINT handler.")150(invoke-debugger "SIGINT")))151152(defun make-sigint-handler (process)153(lambda (&rest args)154(declare (ignore args))155(mp:process-interrupt process #'sigint-handler)))156157(defun set-sigint-handler ()158;; Set SIGINT handler on Swank request handler thread.159#-win32160(sys::set-signal-handler +sigint+161(make-sigint-handler mp:*current-process*)))162163#-win32164(defimplementation install-sigint-handler (handler)165(sys::set-signal-handler +sigint+166(let ((self mp:*current-process*))167(lambda (&rest args)168(declare (ignore args))169(mp:process-interrupt self handler)))))170171(defimplementation getpid ()172#+win32 (win32:get-current-process-id)173#-win32 (system::getpid))174175(defimplementation lisp-implementation-type-name ()176"lispworks")177178(defimplementation set-default-directory (directory)179(namestring (hcl:change-directory directory)))180181;;;; Documentation182183(defun map-list (function list)184"Map over proper and not proper lists."185(loop for (car . cdr) on list186collect (funcall function car) into result187when (null cdr) return result188when (atom cdr) return (nconc result (funcall function cdr))))189190(defun replace-strings-with-symbols (tree)191(map-list192(lambda (x)193(typecase x194(list195(replace-strings-with-symbols x))196(symbol197x)198(string199(intern x))200(t201(intern (write-to-string x)))))202tree))203204(defimplementation arglist (symbol-or-function)205(let ((arglist (lw:function-lambda-list symbol-or-function)))206(etypecase arglist207((member :dont-know)208:not-available)209(list210(replace-strings-with-symbols arglist)))))211212(defimplementation function-name (function)213(nth-value 2 (function-lambda-expression function)))214215(defimplementation macroexpand-all (form)216(walker:walk-form form))217218(defun generic-function-p (object)219(typep object 'generic-function))220221(defimplementation describe-symbol-for-emacs (symbol)222"Return a plist describing SYMBOL.223Return NIL if the symbol is unbound."224(let ((result '()))225(labels ((first-line (string)226(let ((pos (position #\newline string)))227(if (null pos) string (subseq string 0 pos))))228(doc (kind &optional (sym symbol))229(let ((string (or (documentation sym kind))))230(if string231(first-line string)232:not-documented)))233(maybe-push (property value)234(when value235(setf result (list* property value result)))))236(maybe-push237:variable (when (boundp symbol)238(doc 'variable)))239(maybe-push240:generic-function (if (and (fboundp symbol)241(generic-function-p (fdefinition symbol)))242(doc 'function)))243(maybe-push244:function (if (and (fboundp symbol)245(not (generic-function-p (fdefinition symbol))))246(doc 'function)))247(maybe-push248:setf (let ((setf-name (sys:underlying-setf-name `(setf ,symbol))))249(if (fboundp setf-name)250(doc 'setf))))251(maybe-push252:class (if (find-class symbol nil)253(doc 'class)))254result)))255256(defimplementation describe-definition (symbol type)257(ecase type258(:variable (describe-symbol symbol))259(:class (describe (find-class symbol)))260((:function :generic-function) (describe-function symbol))261(:setf (describe-function (sys:underlying-setf-name `(setf ,symbol))))))262263(defun describe-function (symbol)264(cond ((fboundp symbol)265(format t "(~A ~/pprint-fill/)~%~%~:[(not documented)~;~:*~A~]~%"266symbol267(lispworks:function-lambda-list symbol)268(documentation symbol 'function))269(describe (fdefinition symbol)))270(t (format t "~S is not fbound" symbol))))271272(defun describe-symbol (sym)273(format t "~A is a symbol in package ~A." sym (symbol-package sym))274(when (boundp sym)275(format t "~%~%Value: ~A" (symbol-value sym)))276(let ((doc (documentation sym 'variable)))277(when doc278(format t "~%~%Variable documentation:~%~A" doc)))279(when (fboundp sym)280(describe-function sym)))281282;;; Debugging283284(defclass slime-env (env:environment)285((debugger-hook :initarg :debugger-hoook)))286287(defun slime-env (hook io-bindings)288(make-instance 'slime-env :name "SLIME Environment"289:io-bindings io-bindings290:debugger-hoook hook))291292(defmethod env-internals:environment-display-notifier293((env slime-env) &key restarts condition)294(declare (ignore restarts condition))295(funcall (swank-sym :swank-debugger-hook) condition *debugger-hook*)296;; nil297)298299(defmethod env-internals:environment-display-debugger ((env slime-env))300*debug-io*)301302(defmethod env-internals:confirm-p ((e slime-env) &optional msg &rest args)303(apply (swank-sym :y-or-n-p-in-emacs) msg args))304305(defimplementation call-with-debugger-hook (hook fun)306(let ((*debugger-hook* hook))307(env:with-environment ((slime-env hook '()))308(funcall fun))))309310(defimplementation install-debugger-globally (function)311(setq *debugger-hook* function)312(setf (env:environment) (slime-env function '())))313314(defvar *sldb-top-frame*)315316(defun interesting-frame-p (frame)317(cond ((or (dbg::call-frame-p frame)318(dbg::derived-call-frame-p frame)319(dbg::foreign-frame-p frame)320(dbg::interpreted-call-frame-p frame))321t)322((dbg::catch-frame-p frame) dbg:*print-catch-frames*)323((dbg::binding-frame-p frame) dbg:*print-binding-frames*)324((dbg::handler-frame-p frame) dbg:*print-handler-frames*)325((dbg::restart-frame-p frame) dbg:*print-restart-frames*)326(t nil)))327328(defun nth-next-frame (frame n)329"Unwind FRAME N times."330(do ((frame frame (dbg::frame-next frame))331(i n (if (interesting-frame-p frame) (1- i) i)))332((or (not frame)333(and (interesting-frame-p frame) (zerop i)))334frame)))335336(defun nth-frame (index)337(nth-next-frame *sldb-top-frame* index))338339(defun find-top-frame ()340"Return the most suitable top-frame for the debugger."341(or (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)342(nth-next-frame frame 1)))343((or (null frame) ; no frame found!344(and (dbg::call-frame-p frame)345(eq (dbg::call-frame-function-name frame)346'invoke-debugger)))347(nth-next-frame frame 1)))348;; if we can't find a invoke-debugger frame, take any old frame at the top349(dbg::debugger-stack-current-frame dbg::*debugger-stack*)))350351(defimplementation call-with-debugging-environment (fn)352(dbg::with-debugger-stack ()353(let ((*sldb-top-frame* (find-top-frame)))354(funcall fn))))355356(defimplementation compute-backtrace (start end)357(let ((end (or end most-positive-fixnum))358(backtrace '()))359(do ((frame (nth-frame start) (dbg::frame-next frame))360(i start))361((or (not frame) (= i end)) (nreverse backtrace))362(when (interesting-frame-p frame)363(incf i)364(push frame backtrace)))))365366(defun frame-actual-args (frame)367(let ((*break-on-signals* nil))368(mapcar (lambda (arg)369(case arg370((&rest &optional &key) arg)371(t372(handler-case (dbg::dbg-eval arg frame)373(error (e) (format nil "<~A>" arg))))))374(dbg::call-frame-arglist frame))))375376(defimplementation print-frame (frame stream)377(cond ((dbg::call-frame-p frame)378(format stream "~S ~S"379(dbg::call-frame-function-name frame)380(frame-actual-args frame)))381(t (princ frame stream))))382383(defun frame-vars (frame)384(first (dbg::frame-locals-format-list frame #'list 75 0)))385386(defimplementation frame-locals (n)387(let ((frame (nth-frame n)))388(if (dbg::call-frame-p frame)389(mapcar (lambda (var)390(destructuring-bind (name value symbol location) var391(declare (ignore name location))392(list :name symbol :id 0393:value value)))394(frame-vars frame)))))395396(defimplementation frame-var-value (frame var)397(let ((frame (nth-frame frame)))398(destructuring-bind (_n value _s _l) (nth var (frame-vars frame))399(declare (ignore _n _s _l))400value)))401402(defimplementation frame-source-location (frame)403(let ((frame (nth-frame frame))404(callee (if (plusp frame) (nth-frame (1- frame)))))405(if (dbg::call-frame-p frame)406(let ((dspec (dbg::call-frame-function-name frame))407(cname (and (dbg::call-frame-p callee)408(dbg::call-frame-function-name callee))))409(if dspec410(frame-location dspec cname))))))411412(defimplementation eval-in-frame (form frame-number)413(let ((frame (nth-frame frame-number)))414(dbg::dbg-eval form frame)))415416(defimplementation return-from-frame (frame-number form)417(let* ((frame (nth-frame frame-number))418(return-frame (dbg::find-frame-for-return frame)))419(dbg::dbg-return-from-call-frame frame form return-frame420dbg::*debugger-stack*)))421422(defimplementation restart-frame (frame-number)423(let ((frame (nth-frame frame-number)))424(dbg::restart-frame frame :same-args t)))425426(defimplementation disassemble-frame (frame-number)427(let* ((frame (nth-frame frame-number)))428(when (dbg::call-frame-p frame)429(let ((function (dbg::get-call-frame-function frame)))430(disassemble function)))))431432;;; Definition finding433434(defun frame-location (dspec callee-name)435(let ((infos (dspec:find-dspec-locations dspec)))436(cond (infos437(destructuring-bind ((rdspec location) &rest _) infos438(declare (ignore _))439(let ((name (and callee-name (symbolp callee-name)440(string callee-name))))441(make-dspec-location rdspec location442`(:call-site ,name)))))443(t444(list :error (format nil "Source location not available for: ~S"445dspec))))))446447(defimplementation find-definitions (name)448(let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))449(loop for (dspec location) in locations450collect (list dspec (make-dspec-location dspec location)))))451452453;;; Compilation454455(defmacro with-swank-compilation-unit ((location &rest options) &body body)456(lw:rebinding (location)457`(let ((compiler::*error-database* '()))458(with-compilation-unit ,options459(multiple-value-prog1 (progn ,@body)460(signal-error-data-base compiler::*error-database*461,location)462(signal-undefined-functions compiler::*unknown-functions*463,location))))))464465(defimplementation swank-compile-file (input-file output-file466load-p external-format467&key policy)468(declare (ignore policy))469(with-swank-compilation-unit (input-file)470(compile-file input-file471:output-file output-file472:load load-p473:external-format external-format)))474475(defvar *within-call-with-compilation-hooks* nil476"Whether COMPILE-FILE was called from within CALL-WITH-COMPILATION-HOOKS.")477478(defvar *undefined-functions-hash* nil479"Hash table to map info about undefined functions to pathnames.")480481(lw:defadvice (compile-file compile-file-and-collect-notes :around)482(pathname &rest rest)483(multiple-value-prog1 (apply #'lw:call-next-advice pathname rest)484(when *within-call-with-compilation-hooks*485(maphash (lambda (unfun dspecs)486(dolist (dspec dspecs)487(let ((unfun-info (list unfun dspec)))488(unless (gethash unfun-info *undefined-functions-hash*)489(setf (gethash unfun-info *undefined-functions-hash*)490pathname)))))491compiler::*unknown-functions*))))492493(defimplementation call-with-compilation-hooks (function)494(let ((compiler::*error-database* '())495(*undefined-functions-hash* (make-hash-table :test 'equal))496(*within-call-with-compilation-hooks* t))497(with-compilation-unit ()498(prog1 (funcall function)499(signal-error-data-base compiler::*error-database*)500(signal-undefined-functions compiler::*unknown-functions*)))))501502(defun map-error-database (database fn)503(loop for (filename . defs) in database do504(loop for (dspec . conditions) in defs do505(dolist (c conditions)506(funcall fn filename dspec (if (consp c) (car c) c))))))507508(defun lispworks-severity (condition)509(cond ((not condition) :warning)510(t (etypecase condition511(error :error)512(style-warning :warning)513(warning :warning)))))514515(defun signal-compiler-condition (message location condition)516(check-type message string)517(signal518(make-instance 'compiler-condition :message message519:severity (lispworks-severity condition)520:location location521:original-condition condition)))522523(defvar *temp-file-format* '(:utf-8 :eol-style :lf))524525(defun compile-from-temp-file (string filename)526(unwind-protect527(progn528(with-open-file (s filename :direction :output529:if-exists :supersede530:external-format *temp-file-format*)531532(write-string string s)533(finish-output s))534(multiple-value-bind (binary-filename warnings? failure?)535(compile-file filename :load t536:external-format *temp-file-format*)537(declare (ignore warnings?))538(when binary-filename539(delete-file binary-filename))540(not failure?)))541(delete-file filename)))542543(defun dspec-function-name-position (dspec fallback)544(etypecase dspec545(cons (let ((name (dspec:dspec-primary-name dspec)))546(typecase name547((or symbol string)548(list :function-name (string name)))549(t fallback))))550(null fallback)551(symbol (list :function-name (string dspec)))))552553(defmacro with-fairly-standard-io-syntax (&body body)554"Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."555(let ((package (gensym))556(readtable (gensym)))557`(let ((,package *package*)558(,readtable *readtable*))559(with-standard-io-syntax560(let ((*package* ,package)561(*readtable* ,readtable))562,@body)))))563564(defun skip-comments (stream)565(let ((pos0 (file-position stream)))566(cond ((equal (ignore-errors (list (read-delimited-list #\( stream)))567'(()))568(file-position stream (1- (file-position stream))))569(t (file-position stream pos0)))))570571#-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3572(defun dspec-stream-position (stream dspec)573(with-fairly-standard-io-syntax574(loop (let* ((pos (progn (skip-comments stream) (file-position stream)))575(form (read stream nil '#1=#:eof)))576(when (eq form '#1#)577(return nil))578(labels ((check-dspec (form)579(when (consp form)580(let ((operator (car form)))581(case operator582((progn)583(mapcar #'check-dspec584(cdr form)))585((eval-when locally macrolet symbol-macrolet)586(mapcar #'check-dspec587(cddr form)))588((in-package)589(let ((package (find-package (second form))))590(when package591(setq *package* package))))592(otherwise593(let ((form-dspec (dspec:parse-form-dspec form)))594(when (dspec:dspec-equal dspec form-dspec)595(return pos)))))))))596(check-dspec form))))))597598(defun dspec-file-position (file dspec)599(let* ((*compile-file-pathname* (pathname file))600(*compile-file-truename* (truename *compile-file-pathname*))601(*load-pathname* *compile-file-pathname*)602(*load-truename* *compile-file-truename*))603(with-open-file (stream file)604(let ((pos605#-(or lispworks4.1 lispworks4.2)606(dspec-stream-position stream dspec)))607(if pos608(list :position (1+ pos))609(dspec-function-name-position dspec `(:position 1)))))))610611(defun emacs-buffer-location-p (location)612(and (consp location)613(eq (car location) :emacs-buffer)))614615(defun make-dspec-location (dspec location &optional hints)616(etypecase location617((or pathname string)618(multiple-value-bind (file err)619(ignore-errors (namestring (truename location)))620(if err621(list :error (princ-to-string err))622(make-location `(:file ,file)623(dspec-file-position file dspec)624hints))))625(symbol626`(:error ,(format nil "Cannot resolve location: ~S" location)))627((satisfies emacs-buffer-location-p)628(destructuring-bind (_ buffer offset string) location629(declare (ignore _ string))630(make-location `(:buffer ,buffer)631(dspec-function-name-position dspec `(:offset ,offset 0))632hints)))))633634(defun make-dspec-progenitor-location (dspec location)635(let ((canon-dspec (dspec:canonicalize-dspec dspec)))636(make-dspec-location637(if canon-dspec638(if (dspec:local-dspec-p canon-dspec)639(dspec:dspec-progenitor canon-dspec)640canon-dspec)641nil)642location)))643644(defun signal-error-data-base (database &optional location)645(map-error-database646database647(lambda (filename dspec condition)648(signal-compiler-condition649(format nil "~A" condition)650(make-dspec-progenitor-location dspec (or location filename))651condition))))652653(defun unmangle-unfun (symbol)654"Converts symbols like 'SETF::|\"CL-USER\" \"GET\"| to655function names like \(SETF GET)."656(cond ((sys::setf-symbol-p symbol)657(sys::setf-pair-from-underlying-name symbol))658(t symbol)))659660(defun signal-undefined-functions (htab &optional filename)661(maphash (lambda (unfun dspecs)662(dolist (dspec dspecs)663(signal-compiler-condition664(format nil "Undefined function ~A" (unmangle-unfun unfun))665(make-dspec-progenitor-location dspec666(or filename667(gethash (list unfun dspec)668*undefined-functions-hash*)))669nil)))670htab))671672(defimplementation swank-compile-string (string &key buffer position filename673policy)674(declare (ignore filename policy))675(assert buffer)676(assert position)677(let* ((location (list :emacs-buffer buffer position string))678(tmpname (hcl:make-temp-file nil "lisp")))679(with-swank-compilation-unit (location)680(compile-from-temp-file681(with-output-to-string (s)682(let ((*print-radix* t))683(print `(eval-when (:compile-toplevel)684(setq dspec::*location* (list ,@location)))685s))686(write-string string s))687tmpname))))688689;;; xref690691(defmacro defxref (name function)692`(defimplementation ,name (name)693(xref-results (,function name))))694695(defxref who-calls hcl:who-calls)696(defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too697(defxref calls-who hcl:calls-who)698(defxref list-callers list-callers-internal)699;; (defxref list-callees list-callees-internal)700701(defun list-callers-internal (name)702(let ((callers (make-array 100703:fill-pointer 0704:adjustable t)))705(hcl:sweep-all-objects706#'(lambda (object)707(when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)708#-Harlequin-PC-Lisp (sys::callablep object)709(system::find-constant$funcallable name object))710(vector-push-extend object callers))))711;; Delay dspec:object-dspec until after sweep-all-objects712;; to reduce allocation problems.713(loop for object across callers714collect (if (symbolp object)715(list 'function object)716(or (dspec:object-dspec object) object)))))717718;; only for lispworks 4.2 and above719#-lispworks4.1720(progn721(defxref who-references hcl:who-references)722(defxref who-binds hcl:who-binds)723(defxref who-sets hcl:who-sets))724725(defimplementation who-specializes (classname)726(let ((methods (clos:class-direct-methods (find-class classname))))727(xref-results (mapcar #'dspec:object-dspec methods))))728729(defun xref-results (dspecs)730(flet ((frob-locs (dspec locs)731(cond (locs732(loop for (name loc) in locs733collect (list name (make-dspec-location name loc))))734(t `((,dspec (:error "Source location not available")))))))735(loop for dspec in dspecs736append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))737738;;; Inspector739740(defmethod emacs-inspect ((o t))741(lispworks-inspect o))742743(defmethod emacs-inspect ((o function))744(lispworks-inspect o))745746;; FIXME: slot-boundp-using-class in LW works with names so we can't747;; use our method in swank.lisp.748(defmethod emacs-inspect ((o standard-object))749(lispworks-inspect o))750751(defun lispworks-inspect (o)752(multiple-value-bind (names values _getter _setter type)753(lw:get-inspector-values o nil)754(declare (ignore _getter _setter))755(append756(label-value-line "Type" type)757(loop for name in names758for value in values759append (label-value-line name value)))))760761;;; Miscellaneous762763(defimplementation quit-lisp ()764(lispworks:quit))765766;;; Tracing767768(defun parse-fspec (fspec)769"Return a dspec for FSPEC."770(ecase (car fspec)771((:defmethod) `(method ,(cdr fspec)))))772773(defun tracedp (dspec)774(member dspec (eval '(trace)) :test #'equal))775776(defun toggle-trace-aux (dspec)777(cond ((tracedp dspec)778(eval `(untrace ,dspec))779(format nil "~S is now untraced." dspec))780(t781(eval `(trace (,dspec)))782(format nil "~S is now traced." dspec))))783784(defimplementation toggle-trace (fspec)785(toggle-trace-aux (parse-fspec fspec)))786787;;; Multithreading788789(defimplementation initialize-multiprocessing (continuation)790(cond ((not mp::*multiprocessing*)791(push (list "Initialize SLIME" '() continuation)792mp:*initial-processes*)793(mp:initialize-multiprocessing))794(t (funcall continuation))))795796(defimplementation spawn (fn &key name)797(mp:process-run-function name () fn))798799(defvar *id-lock* (mp:make-lock))800(defvar *thread-id-counter* 0)801802(defimplementation thread-id (thread)803(mp:with-lock (*id-lock*)804(or (getf (mp:process-plist thread) 'id)805(setf (getf (mp:process-plist thread) 'id)806(incf *thread-id-counter*)))))807808(defimplementation find-thread (id)809(find id (mp:list-all-processes)810:key (lambda (p) (getf (mp:process-plist p) 'id))))811812(defimplementation thread-name (thread)813(mp:process-name thread))814815(defimplementation thread-status (thread)816(format nil "~A ~D"817(mp:process-whostate thread)818(mp:process-priority thread)))819820(defimplementation make-lock (&key name)821(mp:make-lock :name name))822823(defimplementation call-with-lock-held (lock function)824(mp:with-lock (lock) (funcall function)))825826(defimplementation current-thread ()827mp:*current-process*)828829(defimplementation all-threads ()830(mp:list-all-processes))831832(defimplementation interrupt-thread (thread fn)833(mp:process-interrupt thread fn))834835(defimplementation kill-thread (thread)836(mp:process-kill thread))837838(defimplementation thread-alive-p (thread)839(mp:process-alive-p thread))840841(defstruct (mailbox (:conc-name mailbox.))842(mutex (mp:make-lock :name "thread mailbox"))843(queue '() :type list))844845(defvar *mailbox-lock* (mp:make-lock))846847(defun mailbox (thread)848(mp:with-lock (*mailbox-lock*)849(or (getf (mp:process-plist thread) 'mailbox)850(setf (getf (mp:process-plist thread) 'mailbox)851(make-mailbox)))))852853(defimplementation receive-if (test &optional timeout)854(let* ((mbox (mailbox mp:*current-process*))855(lock (mailbox.mutex mbox)))856(assert (or (not timeout) (eq timeout t)))857(loop858(check-slime-interrupts)859(mp:with-lock (lock "receive-if/try")860(let* ((q (mailbox.queue mbox))861(tail (member-if test q)))862(when tail863(setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))864(return (car tail)))))865(when (eq timeout t) (return (values nil t)))866(mp:process-wait-with-timeout867"receive-if" 0.3 (lambda () (some test (mailbox.queue mbox)))))))868869(defimplementation send (thread message)870(let ((mbox (mailbox thread)))871(mp:with-lock ((mailbox.mutex mbox))872(setf (mailbox.queue mbox)873(nconc (mailbox.queue mbox) (list message))))))874875(defimplementation set-default-initial-binding (var form)876(setq mp:*process-initial-bindings*877(acons var `(eval (quote ,form))878mp:*process-initial-bindings* )))879880(defimplementation thread-attributes (thread)881(list :priority (mp:process-priority thread)882:idle (mp:process-idle-time thread)))883884;;; Some intergration with the lispworks environment885886(defun swank-sym (name) (find-symbol (string name) :swank))887888889;;;; Weak hashtables890891(defimplementation make-weak-key-hash-table (&rest args)892(apply #'make-hash-table :weak-kind :key args))893894(defimplementation make-weak-value-hash-table (&rest args)895(apply #'make-hash-table :weak-kind :value args))896897898