source: trunk/source/level-1/l1-lisp-threads.lisp @ 12463

Last change on this file since 12463 was 12463, checked in by gz, 10 years ago

Some changes in support of Slime:

Implement CCL:COMPUTE-APPLICABLE-METHODS-USING-CLASSES

Add a :stream-args argument to CCL:ACCEPT-CONNECTION, for one-time initargs for the stream being created. E.g. (accept-connection listener :stream-args `(:external-format ,external-format-for-this-connection-only))

Add CCL:TEMP-PATHNAME

Bind new var CCL:*TOP-ERROR-FRAME* to the error frame in break loops, to make it available to debugger/break hooks.

Add CCL:*SELECT-INTERACTIVE-PROCESS-HOOK* and call it to select the process to use for handling SIGINT.

Add CCL:*MERGE-COMPILER-WARNINGS* to control whether warnings with the same format string and args but different source locations should be merged.

Export CCL:COMPILER-WARNING, CCL:STYLE-WARNING, CCL:COMPILER-WARNING-FUNCTION-NAME and CCL:COMPILER-WARNING-SOURCE-NOTE.

Create a CCL:COMPILER-WARNING-SOURCE-NOTE even if not otherwise saving source locations, just using the fasl file and toplevel stream position, but taking into account compile-file-original-truename and compiler-file-original-buffer-offset. Get rid of stream-position and file-name slots in compiler warnings.

Export CCL:REPORT-COMPILER-WARNING, and make it accept a :SHORT keyword arg to skip the textual representation of the warning location.

Export CCL:NAME-OF, and make it return the fully qualified name for methods, return object for eql-specializer

Make CCL:FIND-DEFINITION-SOURCES handle xref-entries.

Export CCL:SETF-FUNCTION-SPEC-NAME, make it explicitly ignore the long-form setf method case.

Export the basic inspector API from the inspector package.

Export EQL-SPECIALIZER and SLOT-DEFINITION-DOCUMENTATION from OPENMCL-MOP

Refactor things a bit in backtrace code, define and export an API for examining backtraces:

CCL:MAP-CALL-FRAMES
CCL:FRAME-FUNCTION
CCL:FRAME-SUPPLIED-ARGUMENTS
CCL:FRAME-NAMED-VARIABLES

other misc new exports:

CCL:DEFINITION-TYPE
CCL;CALLER-FUNCTIONS
CCL:SLOT-DEFINITION-DOCUMENTATION
CCL:*SAVE-ARGLIST-INFO*
CCL:NATIVE-TRANSLATED-NAMESTRING
CCL:NATIVE-TO-PATHNAME
CCL:HASH-TABLE-WEAK-P
CCL;PROCESS-SERIAL-NUMBER
CCL:PROCESS-EXHAUSTED-P
CCL:APPLY-IN-FRAME

Other misc tweaks:

Make cbreak-loop use the break message when given a uselessly empty condition.

Use setf-function-name-p more consistently

Make find-applicable-methods handle eql specializers better.

Try to more consistently recognize lists of the form (:method ...) as method names.

Add xref-entry-full-name (which wasn't needed in the end)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.0 KB
Line 
1;;; -*- Mode: LISP; Package: CCL -*-
2;;;
3;;;   Copyright (C) 1994-2001 Digitool, Inc
4;;;   This file is part of OpenMCL. 
5;;;
6;;;   OpenMCL is licensed under the terms of the Lisp Lesser GNU Public
7;;;   License , known as the LLGPL and distributed with OpenMCL as the
8;;;   file "LICENSE".  The LLGPL consists of a preamble and the LGPL,
9;;;   which is distributed with OpenMCL as the file "LGPL".  Where these
10;;;   conflict, the preamble takes precedence. 
11;;;
12;;;   OpenMCL is referenced in the preamble as the "LIBRARY."
13;;;
14;;;   The LLGPL is also available online at
15;;;   http://opensource.franz.com/preamble.html
16
17;; l1-lisp-threads.lisp
18
19(in-package "CCL")
20
21(defvar *bind-io-control-vars-per-process* nil
22  "If true, bind I/O control variables per process")
23
24
25             
26(defun lisp-thread-p (thing)
27  (istruct-typep thing 'lisp-thread))
28
29(setf (type-predicate 'lisp-thread) 'lisp-thread-p)
30
31(defloadvar *ticks-per-second*
32    #+windows-target 1000
33    #-windows-target
34    (#_sysconf #$_SC_CLK_TCK))
35
36(defloadvar *ns-per-tick*
37    (floor 1000000000 *ticks-per-second*))
38
39#-windows-target
40(defun %nanosleep (seconds nanoseconds)
41  (with-process-whostate ("Sleep")
42    (rlet ((a :timespec)
43           (b :timespec))
44      (setf (pref a :timespec.tv_sec) seconds
45            (pref a :timespec.tv_nsec) nanoseconds)
46      (let* ((aptr a)
47             (bptr b))
48        (loop
49          (let* ((result 
50                  (external-call #+darwin-target "_nanosleep"
51                                 #-darwin-target "nanosleep"
52                                 :address aptr
53                                 :address bptr
54                                 :signed-fullword)))
55            (declare (type (signed-byte 32) result))
56            (if (and (< result 0)
57                     (eql (%get-errno) (- #$EINTR)))
58              ;; x86-64 Leopard bug.
59              (let* ((asec (pref aptr :timespec.tv_sec))
60                     (bsec (pref bptr :timespec.tv_sec)))
61                (if (and (>= bsec 0)
62                         (or (< bsec asec)
63                             (and (= bsec asec)
64                                  (< (pref bptr :timespec.tv_nsec)
65                                     (pref aptr :timespec.tv_nsec)))))
66                  (psetq aptr bptr bptr aptr)
67                  (return)))
68              (return))))))))
69
70
71(defun timeval->ticks (tv)
72  (+ (* *ticks-per-second* (pref tv :timeval.tv_sec))
73     (round (pref tv :timeval.tv_usec) (floor 1000000 *ticks-per-second*))))
74
75
76(defun gettimeofday (ptimeval &optional ptz)
77  (int-errno-ffcall (%kernel-import target::kernel-import-lisp-gettimeofday)
78                    :address ptimeval
79                    :address (or ptz (%null-ptr))
80                    :int))
81
82(defloadvar *lisp-start-timeval*
83    (progn
84      (let* ((r (make-record :timeval)))
85        (gettimeofday r)
86        r)))
87
88
89(defloadvar *internal-real-time-session-seconds* nil)
90
91
92(defun get-internal-real-time ()
93  "Return the real time in the internal time format. (See
94  INTERNAL-TIME-UNITS-PER-SECOND.) This is useful for finding elapsed time."
95  (rlet ((tv :timeval))
96    (gettimeofday tv)
97    (let* ((units (truncate (the fixnum (pref tv :timeval.tv_usec)) (/ 1000000 internal-time-units-per-second)))
98           (initial *internal-real-time-session-seconds*))
99      (if initial
100        (locally
101            (declare (type (unsigned-byte 32) initial))
102          (+ (* internal-time-units-per-second
103                (the (unsigned-byte 32)
104                  (- (the (unsigned-byte 32) (pref tv :timeval.tv_sec))
105                     initial)))
106             units))
107        (progn
108          (setq *internal-real-time-session-seconds*
109                (pref tv :timeval.tv_sec))
110          units)))))
111
112(defun get-tick-count ()
113  (values (floor (get-internal-real-time)
114                 (floor internal-time-units-per-second
115                        *ticks-per-second*))))
116
117
118
119
120(defun %kernel-global-offset (name-or-offset)
121  (if (fixnump name-or-offset)
122    name-or-offset
123    (target::%kernel-global name-or-offset)))
124
125
126(defun %kernel-global-offset-form (name-or-offset-form)
127  (cond ((quoted-form-p name-or-offset-form)
128         `(%target-kernel-global ,name-or-offset-form))
129        ((fixnump name-or-offset-form)
130         name-or-offset-form)
131        (t `(%target-kernel-global ',name-or-offset-form))))
132
133
134
135(defmacro %set-kernel-global (name-or-offset new-value)
136  `(%set-kernel-global-from-offset
137    ,(%kernel-global-offset-form name-or-offset)
138    ,new-value))
139
140
141
142; The number of bytes in a consing (or stack) area
143(defun %area-size (area)
144  (ash (- (%fixnum-ref area target::area.high)
145          (%fixnum-ref area target::area.low))
146       target::fixnumshift))
147
148(defun %stack-area-usable-size (area)
149  (ash (- (%fixnum-ref area target::area.high)
150          (%fixnum-ref area target::area.softlimit))
151       target::fixnum-shift))
152
153(defun %cons-lisp-thread (name &optional tcr)
154  (%istruct 'lisp-thread
155            tcr
156            name
157            0
158            0
159            0
160            nil
161            nil
162            (make-lock)
163            nil
164            :reset
165            (make-lock)))
166
167(defvar *current-lisp-thread*
168  (%cons-lisp-thread "Initial" (%current-tcr)))
169
170(defstatic *initial-lisp-thread* *current-lisp-thread*)
171
172(defun thread-change-state (thread oldstate newstate)
173  (with-lock-grabbed ((lisp-thread.state-change-lock thread))
174    (when (eq (lisp-thread.state thread) oldstate)
175      (setf (lisp-thread.state thread) newstate))))
176
177(thread-change-state *initial-lisp-thread* :reset :run)
178
179(defun thread-state (thread)
180  (with-lock-grabbed ((lisp-thread.state-change-lock thread))
181    (lisp-thread.state thread)))
182 
183(defun thread-make-startup-function (thread tcr)
184  #'(lambda ()
185      (thread-change-state thread :reset :run)
186      (let* ((*current-lisp-thread* thread)
187             (initial-function (lisp-thread.initial-function.args thread)))
188        (tcr-clear-preset-state tcr)
189        (%set-tcr-toplevel-function tcr nil)
190        (setf (interrupt-level) 0)
191        (apply (car initial-function) (cdr initial-function))
192        (cleanup-thread-tcr thread tcr))))
193
194(defun init-thread-from-tcr (tcr thread)
195  (let* ((cs-area (%fixnum-ref tcr target::tcr.cs-area))
196         (vs-area (%fixnum-ref tcr target::tcr.vs-area))
197         (ts-area (%fixnum-ref tcr target::tcr.ts-area)))
198    (when (or (zerop cs-area)
199              (zerop vs-area)
200              (zerop ts-area))
201      (error "Can't allocate new thread"))
202    (setf (lisp-thread.tcr thread) tcr
203          (lisp-thread.cs-size thread)
204          (%stack-area-usable-size cs-area)
205          (lisp-thread.vs-size thread)
206          (%stack-area-usable-size vs-area)
207          (lisp-thread.ts-size thread)
208          (%stack-area-usable-size ts-area)
209          (lisp-thread.startup-function thread)
210          (thread-make-startup-function thread tcr)))
211  (thread-change-state thread :exit :reset)
212  thread)
213
214(defun default-allocation-quantum ()
215  (ash 1 (%get-kernel-global 'default-allocation-quantum)))
216
217(defun new-lisp-thread-from-tcr (tcr name)
218  (let* ((thread (%cons-lisp-thread name tcr)))   
219    (init-thread-from-tcr tcr thread)
220    (push thread (population-data *lisp-thread-population*))
221    thread))
222
223(def-ccl-pointers initial-thread ()
224  (init-thread-from-tcr (%current-tcr) *initial-lisp-thread*))
225
226(defmethod print-object ((thread lisp-thread) stream)
227  (print-unreadable-object (thread stream :type t :identity t)
228    (format stream "~a" (lisp-thread.name thread))
229    (let* ((tcr (lisp-thread.tcr thread)))
230      (if (and tcr (not (eql 0 tcr)))
231        (format stream " [tcr @ #x~x]" (ash tcr target::fixnumshift))))))
232
233
234(defvar *lisp-thread-population*
235  (%cons-population (list *initial-lisp-thread*) $population_weak-list nil))
236
237
238
239
240
241(defparameter *default-control-stack-size*
242  #+32-bit-target (ash 1 20)
243  #+64-bit-target (ash 2 20))
244(defparameter *default-value-stack-size*
245  #+32-bit-target (ash 1 20)
246  #+64-bit-target (ash 2 20))
247(defparameter *default-temp-stack-size*
248  #+32-bit-target (ash 1 19)
249  #+64-bit-target (ash 2 19))
250
251
252(defstatic *initial-listener-default-control-stack-size* *default-control-stack-size*)
253(defstatic *initial-listener-default-value-stack-size* *default-value-stack-size*)
254(defstatic *initial-listener-default-temp-stack-size* *default-temp-stack-size*)
255
256
257(def-ccl-pointers listener-stack-sizes ()
258  (let* ((size (%get-kernel-global 'stack-size))) ; set by --thread-stack-size
259    (declare (fixnum size))
260    (when (> size 0)
261      (setq *initial-listener-default-control-stack-size* size
262            *initial-listener-default-value-stack-size* size
263            *initial-listener-default-temp-stack-size* (floor size 2)))))
264
265
266(defmacro with-area-macptr ((var area) &body body)
267  `(with-macptrs (,var)
268     (%setf-macptr-to-object ,var ,area)
269     ,@body))
270
271
272(defun gc-area.return-sp (area)
273  (%fixnum-ref area target::area.gc-count))
274
275
276(defun (setf gc-area.return-sp) (return-sp area)
277  (setf (%fixnum-ref area target::area.gc-count) return-sp))
278
279
280
281(defun shutdown-lisp-threads ()
282  )
283
284(defun %current-xp ()
285  (let ((xframe (%fixnum-ref (%current-tcr) target::tcr.xframe)))
286    (when (eql xframe 0)
287      (error "No current exception frame"))
288    (%fixnum-ref xframe
289                 (get-field-offset :xframe-list.this))))
290
291(defun new-tcr (cs-size vs-size ts-size)
292  (let* ((tcr (macptr->fixnum
293               (ff-call
294                (%kernel-import target::kernel-import-newthread)
295                #+64-bit-target :unsigned-doubleword
296                #+32-bit-target :unsigned-fullword cs-size
297                #+64-bit-target :unsigned-doubleword
298                #+32-bit-target :unsigned-fullword vs-size
299                #+64-bit-target :unsigned-doubleword
300                #+32-bit-target :unsigned-fullword ts-size
301                :address))))
302    (declare (fixnum tcr))
303    (if (zerop tcr)
304      (error "Can't create thread")
305      tcr)))
306
307(defun new-thread (name cstack-size vstack-size tstack-size)
308  (new-lisp-thread-from-tcr (new-tcr cstack-size vstack-size tstack-size) name))
309
310(defun new-tcr-for-thread (thread)
311  (let* ((tcr (new-tcr
312               (lisp-thread.cs-size thread)
313               (lisp-thread.vs-size thread)
314               (lisp-thread.ts-size thread))))
315    (setf (lisp-thread.tcr thread) tcr
316          (lisp-thread.startup-function thread)
317          (thread-make-startup-function thread tcr))
318    (thread-change-state thread :exit :reset)
319    tcr))
320 
321         
322
323
324
325(defconstant cstack-hardprot (ash 100 10))
326(defconstant cstack-softprot (ash 100 10))
327
328
329
330(defun tcr-flags (tcr)
331  (%fixnum-ref tcr target::tcr.flags))
332
333
334
335(defun %tcr-frame-ptr (tcr)
336  (with-macptrs (p)
337    (%setf-macptr-to-object p tcr)
338    (%fixnum-from-macptr
339     (ff-call (%kernel-import target::kernel-import-tcr-frame-ptr)
340              :address p
341              :address))))
342 
343(defun thread-exhausted-p (thread)
344  (or (null thread)
345      (null (lisp-thread.tcr thread))))
346
347(defun thread-total-run-time (thread)
348  (unless (thread-exhausted-p thread)
349    nil))
350
351(defun %tcr-interrupt (tcr)
352  ;; The other thread's interrupt-pending flag might get cleared
353  ;; right after we look and see it set, but since this is called
354  ;; with the lock on the thread's interrupt queue held, the
355  ;; pending interrupt won't have been taken yet.
356  ;; When a thread dies, it should try to clear its interrupt-pending
357  ;; flag.
358  (if (eql 0 (%fixnum-ref tcr target::tcr.interrupt-pending))
359    (%%tcr-interrupt tcr)
360    0))
361
362
363
364     
365     
366
367(defun thread-interrupt (thread process function &rest args)
368  (with-lock-grabbed ((lisp-thread.state-change-lock thread))
369    (case (lisp-thread.state thread)
370      (:run 
371       (with-lock-grabbed ((lisp-thread.interrupt-lock thread))
372         (let ((tcr (lisp-thread.tcr thread)))
373           (when tcr
374             (push (cons function args)
375                   (lisp-thread.interrupt-functions thread))
376             (eql 0 (%tcr-interrupt tcr))))))
377      (:reset
378       ;; Preset the thread with a function that'll return to the :reset
379       ;; state
380       (let* ((pif (process-initial-form process))
381              (pif-f (car pif))
382              (pif-args (cdr pif)))
383         (process-preset process #'(lambda ()
384                                     (%rplaca pif pif-f)
385                                     (%rplacd pif pif-args)
386                                     (apply function args)
387                                     ;; If function returns normally,
388                                     ;; return to the reset state
389                                     (%process-reset nil)))
390         (thread-enable thread (process-termination-semaphore process) (1- (integer-length (process-allocation-quantum process))) 0)
391         t)))))
392
393(defun thread-handle-interrupts ()
394  (let* ((thread *current-lisp-thread*))
395    (with-process-whostate ("Active")
396      (loop
397        (let* ((f (with-lock-grabbed ((lisp-thread.interrupt-lock thread))
398                    (pop (lisp-thread.interrupt-functions thread)))))
399          (if f
400            (apply (car f) (cdr f))
401            (return)))))))
402
403
404       
405(defun  thread-preset (thread function &rest args)
406  (setf (lisp-thread.initial-function.args thread)
407        (cons function args)))
408
409(defun thread-enable (thread termination-semaphore allocation-quantum &optional (timeout (* 60 60 24)))
410  (let* ((tcr (or (lisp-thread.tcr thread) (new-tcr-for-thread thread))))
411    (with-macptrs (s)
412      (%setf-macptr-to-object s (%fixnum-ref tcr target::tcr.reset-completion))
413      (when (%timed-wait-on-semaphore-ptr s timeout nil)
414        (%set-tcr-toplevel-function
415         tcr
416         (lisp-thread.startup-function thread))
417        (%activate-tcr tcr termination-semaphore allocation-quantum)
418        thread))))
419                             
420
421(defun cleanup-thread-tcr (thread tcr)
422  (let* ((flags (%fixnum-ref tcr target::tcr.flags)))
423    (declare (fixnum flags))
424    (if (logbitp arch::tcr-flag-bit-awaiting-preset flags)
425      (thread-change-state thread :run :reset)
426      (progn
427        (thread-change-state thread :run :exit)
428        (setf (lisp-thread.tcr thread) nil)))))
429
430(defun kill-lisp-thread (thread)
431  (unless (eq thread *initial-lisp-thread*)
432    (let* ((tcr (lisp-thread.tcr thread)))
433      (when tcr
434        (setf (lisp-thread.tcr thread) nil
435              (lisp-thread.state thread) :exit)
436        (%kill-tcr tcr)))))
437
438;;; This returns the underlying pthread, whatever that is, as an
439;;; unsigned integer.
440(defun lisp-thread-os-thread (thread)
441  (with-macptrs (tcrp)
442    (%setf-macptr-to-object tcrp (lisp-thread.tcr thread))
443    (unless (%null-ptr-p tcrp)
444      (let* ((natural (%get-natural tcrp target::tcr.osid)))
445        (unless (zerop natural) natural)))))
446
447
448                         
449;;; This returns something lower-level than the pthread, if that
450;;; concept makes sense.  On current versions of Linux, it returns
451;;; the pid of the clone()d process; on Darwin, it returns a Mach
452;;; thread.  On some (near)future version of Linux, the concept
453;;; may not apply.
454;;; The future is here: on Linux systems using NPTL, this returns
455;;; exactly the same thing that (getpid) does.
456;;; This should probably be retired; even if it does something
457;;; interesting, is the value it returns useful ?
458
459(defun lisp-thread-native-thread (thread)
460  (with-macptrs (tcrp)
461    (%setf-macptr-to-object tcrp (lisp-thread.tcr thread))
462    (unless (%null-ptr-p tcrp)
463      (#+32-bit-target %get-unsigned-long
464       #+64-bit-target %%get-unsigned-longlong tcrp target::tcr.native-thread-id))))
465
466(defun lisp-thread-suspend-count (thread)
467  (with-macptrs (tcrp)
468    (%setf-macptr-to-object tcrp (lisp-thread.tcr thread))
469    (unless (%null-ptr-p tcrp)
470      (#+32-bit-target %get-unsigned-long
471       #+64-bit-target %%get-unsigned-longlong tcrp target::tcr.suspend-count))))
472
473(defun tcr-clear-preset-state (tcr)
474  (let* ((flags (%fixnum-ref tcr target::tcr.flags)))
475    (declare (fixnum flags))
476    (setf (%fixnum-ref tcr target::tcr.flags)
477          (bitclr arch::tcr-flag-bit-awaiting-preset flags))))
478
479(defun tcr-set-preset-state (tcr)
480  (let* ((flags (%fixnum-ref tcr target::tcr.flags)))
481    (declare (fixnum flags))
482    (setf (%fixnum-ref tcr target::tcr.flags)
483          (bitset arch::tcr-flag-bit-awaiting-preset flags)))) 
484
485;;; This doesn't quite activate the thread; see PROCESS-TCR-ENABLE.
486(defun %activate-tcr (tcr termination-semaphore allocation-quantum)
487  (if (and tcr (not (eql 0 tcr)))
488    (with-macptrs (tcrp)
489      (%setf-macptr-to-object tcrp tcr)
490      (setf (%get-natural tcrp target::tcr.log2-allocation-quantum)
491            (or allocation-quantum (default-allocation-quantum)))
492      (setf (%get-ptr tcrp target::tcr.termination-semaphore)
493            (if termination-semaphore
494              (semaphore-value termination-semaphore)
495              (%null-ptr)))
496      t)))
497                         
498(defvar *canonical-error-value*
499  '(*canonical-error-value*))
500
501
502(defun symbol-value-in-tcr (sym tcr)
503  (if (eq tcr (%current-tcr))
504    (%sym-value sym)
505    (unwind-protect
506         (progn
507           (%suspend-tcr tcr)
508           (let* ((loc (%tcr-binding-location tcr sym)))
509             (if loc
510               (%fixnum-ref loc)
511               (%sym-global-value sym))))
512      (%resume-tcr tcr))))
513
514(defun (setf symbol-value-in-tcr) (value sym tcr)
515  (if (eq tcr (%current-tcr))
516    (%set-sym-value sym value)
517    (unwind-protect
518         (progn
519           (%suspend-tcr tcr)
520           (let* ((loc (%tcr-binding-location tcr sym)))
521             (if loc
522               (setf (%fixnum-ref loc) value)
523               (%set-sym-global-value sym value))))
524      (%resume-tcr tcr))))
525
526;;; Backtrace support
527;;;
528
529
530
531(defmacro do-db-links ((db-link &optional var value) &body body)
532  (let ((thunk (gensym))
533        (var-var (or var (gensym)))
534        (value-var (or value (gensym))))
535    `(block nil
536       (let ((,thunk #'(lambda (,db-link ,var-var ,value-var)
537                         (declare (ignorable ,db-link))
538                         ,@(unless var (list `(declare (ignore ,var-var))))
539                         ,@(unless value (list `(declare (ignore ,value-var))))
540                         ,@body)))
541         (declare (dynamic-extent ,thunk))
542         (map-db-links ,thunk)))))
543
544
545
546
547(defun map-db-links (f)
548  (without-interrupts
549   (let ((db-link (%current-db-link)))
550     (loop
551       (when (eql 0 db-link) (return))
552       (funcall f db-link (%fixnum-ref db-link (* 1 target::node-size)) (%fixnum-ref db-link (* 2 target::node-size)))
553       (setq db-link (%fixnum-ref db-link))))))
554
555(defun %get-frame-ptr ()
556  (%current-frame-ptr))
557
558(defun %current-exception-frame ()
559  #+ppc-target *fake-stack-frames*
560  #+x86-target (or (let* ((xcf (%current-xcf)))
561                     (if xcf
562                       (%%frame-backlink xcf)))
563                   (%current-frame-ptr)))
564
565
566
567
568
569(defun next-catch (catch)
570  (let ((next-catch (uvref catch target::catch-frame.link-cell)))
571    (unless (eql next-catch 0) next-catch)))
572
573
574
575
576; @@@ this needs to load early so errors can work
577(defun next-lisp-frame (p context)
578  (let ((frame p))
579    (loop
580      (let ((parent (%frame-backlink frame context)))
581        (multiple-value-bind (lisp-frame-p bos-p) (lisp-frame-p parent context)
582          (if lisp-frame-p
583            (return parent)
584            (if bos-p
585              (return nil))))
586        (setq frame parent)))))
587
588(defun parent-frame (p context)
589  (loop
590    (let ((parent (next-lisp-frame p context)))
591      (when (or (null parent)
592                (not (catch-csp-p parent context)))
593        (return parent))
594      (setq p parent))))
595
596
597
598
599
600(defun last-frame-ptr (&optional context origin)
601  (let* ((current (or origin
602                      (if context (bt.current context) (%current-frame-ptr))))
603         (last current))
604    (loop
605      (setq current (parent-frame current context))
606      (if current
607        (setq last current)
608        (return last)))))
609
610
611
612(defun child-frame (p context )
613  (let* ((current (if context (bt.current context) (%current-frame-ptr)))
614         (last nil))
615    (loop
616      (when (null current)
617        (return nil))
618      (when (eq current p) (return last))
619      (setq last current
620            current (parent-frame current context)))))
621
622
623
624
625
626; This returns the current head of the db-link chain.
627(defun db-link (&optional context)
628  (if context
629    (bt.db-link context)
630    (%fixnum-ref (%current-tcr)  target::tcr.db-link)))
631
632(defun previous-db-link (db-link start )
633  (declare (fixnum db-link start))
634  (let ((prev nil))
635    (loop
636      (when (or (eql db-link start) (eql 0 start))
637        (return prev))
638      (setq prev start
639            start (%fixnum-ref start 0)))))
640
641(defun count-db-links-in-frame (vsp parent-vsp &optional context)
642  (declare (fixnum vsp parent-vsp))
643  (let ((db (db-link context))
644        (count 0)
645        (first nil)
646        (last nil))
647    (declare (fixnum db count))
648    (loop
649      (cond ((eql db 0)
650             (return (values count (or first 0) (or last 0))))
651            ((and (>= db vsp) (< db parent-vsp))
652             (unless first (setq first db))
653             (setq last db)
654             (incf count)))
655      (setq db (%fixnum-ref db)))))
656
657;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
658;;;
659;;; bogus-thing-p support
660;;;
661
662(defun %ptr-in-area-p (ptr area)
663  (declare (optimize (speed 3) (safety 0)) (fixnum ptr area))           ; lie, maybe
664  (and (<= (the fixnum (%fixnum-ref area target::area.low)) ptr)
665       (> (the fixnum (%fixnum-ref area target::area.high)) ptr)))
666
667(defun %active-area (area active)
668  (or (do ((a area (%fixnum-ref a target::area.older)))
669          ((eql a 0))
670        (when (%ptr-in-area-p active a)
671          (return a)))
672      (do ((a (%fixnum-ref area target::area.younger) (%fixnum-ref a target::area.younger)))
673          ((eql a 0))
674        (when (%ptr-in-area-p active a)
675          (return a)))))
676
677(defun %ptr-to-vstack-p (tcr idx)
678  (%ptr-in-area-p idx (%fixnum-ref tcr target::tcr.vs-area)))
679
680(defun %on-tsp-stack (tcr object)
681  (%ptr-in-area-p object (%fixnum-ref tcr target::tcr.ts-area)))
682
683(defun %on-csp-stack (tcr object)
684  (%ptr-in-area-p object (%fixnum-ref tcr target::tcr.cs-area)))
685
686(defparameter *aux-tsp-ranges* ())
687(defparameter *aux-vsp-ranges* ())
688(defparameter *aux-csp-ranges* ())
689
690(defun object-in-range-p (object range)
691  (declare (fixnum object))
692  (when range
693    (destructuring-bind (active . high) range
694      (declare (fixnum active high))
695      (and (< active object)
696           (< object high)))))
697
698(defun object-in-some-range (object ranges)
699  (dolist (r ranges)
700    (when (object-in-range-p object r)
701      (return t))))
702
703
704(defun on-any-tsp-stack (object)
705  (or (%on-tsp-stack (%current-tcr) object)
706      (object-in-some-range object *aux-tsp-ranges*)))
707
708(defun on-any-vstack (idx)
709  (or (%ptr-to-vstack-p (%current-tcr) idx)
710      (object-in-some-range idx *aux-vsp-ranges*)))
711
712(defun on-any-csp-stack (object)
713  (or (%on-csp-stack (%current-tcr) object)
714      (object-in-some-range object *aux-csp-ranges*)))
715
716;;; This MUST return either T or NIL.
717(defun temporary-cons-p (x)
718  (and (consp x)
719       (not (null (or (on-any-vstack x)
720                      (on-any-tsp-stack x))))))
721
722
723
724
725
726
727
728(defun %value-cell-header-at-p (cur-vsp)
729  (eql target::value-cell-header (%fixnum-address-of (%fixnum-ref cur-vsp))))
730
731(defun count-stack-consed-value-cells-in-frame (vsp parent-vsp)
732  (let ((cur-vsp vsp)
733        (count 0))
734    (declare (fixnum cur-vsp count))
735    (loop
736      (when (>= cur-vsp parent-vsp) (return))
737      (when (and (evenp cur-vsp) (%value-cell-header-at-p cur-vsp))
738        (incf count)
739        (incf cur-vsp))                 ; don't need to check value after header
740      (incf cur-vsp))
741    count))
742
743;;; stack consed value cells are one of two forms:
744;;; Well, they were of two forms.  When they existed, that is.
745;;;
746;;; nil             ; n-4
747;;; header          ; n = even address (multiple of 8)
748;;; value           ; n+4
749;;;
750;;; header          ; n = even address (multiple of 8)
751;;; value           ; n+4
752;;; nil             ; n+8
753
754(defun in-stack-consed-value-cell-p (arg-vsp vsp parent-vsp)
755  (declare (fixnum arg-vsp vsp parent-vsp))
756  (if (evenp arg-vsp)
757    (%value-cell-header-at-p arg-vsp)
758    (or (and (> arg-vsp vsp)
759             (%value-cell-header-at-p (the fixnum (1- arg-vsp))))
760        (let ((next-vsp (1+ arg-vsp)))
761          (declare (fixnum next-vsp))
762          (and (< next-vsp parent-vsp)
763               (%value-cell-header-at-p next-vsp))))))
764
765
766
767(defun count-values-in-frame (p context &optional child)
768  (declare (ignore child))
769  (multiple-value-bind (vsp parent-vsp) (vsp-limits p context)
770    (values
771     (- parent-vsp 
772        vsp
773        (* 2 (count-db-links-in-frame vsp parent-vsp context))))))
774
775(defun nth-value-in-frame-loc (sp n context lfun pc vsp parent-vsp)
776  (declare (fixnum sp))
777  (setq n (require-type n 'fixnum))
778  (unless (or (null vsp) (fixnump vsp))
779    (setq vsp (require-type vsp '(or null fixnum))))
780  (unless (or (null parent-vsp) (fixnump parent-vsp))
781    (setq parent-vsp (require-type parent-vsp '(or null fixnum))))
782  (unless (and vsp parent-vsp)
783    (multiple-value-setq (vsp parent-vsp) (vsp-limits sp context)))
784  (locally (declare (fixnum n vsp parent-vsp))
785    (multiple-value-bind (db-count first-db last-db)
786                         (count-db-links-in-frame vsp parent-vsp context)
787      (declare (ignore db-count))
788      (declare (fixnum first-db last-db))
789      (let ((arg-vsp (1- parent-vsp))
790            (cnt n)
791            (phys-cell 0)
792            db-link-p)
793        (declare (fixnum arg-vsp cnt phys-cell))
794        (loop
795          (if (eql (the fixnum (- arg-vsp 2)) last-db)
796            (setq db-link-p t
797                  arg-vsp last-db
798                  last-db (previous-db-link last-db first-db)
799                  phys-cell (+ phys-cell 2))
800            (setq db-link-p nil))
801            (when (< (decf cnt) 0)
802              (return
803               (if db-link-p
804                 (values (+ 2 arg-vsp)
805                         :saved-special
806                         (binding-index-symbol (%fixnum-ref (1+ arg-vsp))))
807                 (multiple-value-bind (type name) (find-local-name phys-cell lfun pc)
808                   (values arg-vsp type name)))))
809          (incf phys-cell)
810          (when (< (decf arg-vsp) vsp)
811            (error "~d out of range" n)))))))
812
813
814
815(defun nth-value-in-frame (sp n context &optional lfun pc vsp parent-vsp)
816  (multiple-value-bind (loc type name)
817                       (nth-value-in-frame-loc sp n context lfun pc vsp parent-vsp)
818    (let* ((val (%fixnum-ref loc)))
819      (when (and (eq type :saved-special)
820                 (eq val (%no-thread-local-binding-marker))
821                 name)
822        (setq val (%sym-global-value name)))
823      (values val  type name))))
824
825(defun set-nth-value-in-frame (sp n context new-value &optional vsp parent-vsp)
826  (multiple-value-bind (loc type name)
827      (nth-value-in-frame-loc sp n context nil nil vsp parent-vsp)
828    (let* ((old-value (%fixnum-ref loc)))
829      (if (and (eq type :saved-special)
830               (eq old-value (%no-thread-local-binding-marker))
831               name)
832        ;; Setting the (shallow-bound) value of the outermost
833        ;; thread-local binding of NAME.  Hmm.
834        (%set-sym-global-value name new-value)
835        (setf (%fixnum-ref loc) new-value)))))
836
837(defun nth-raw-frame (n start-frame context)
838  (declare (fixnum n))
839  (do* ((p start-frame (parent-frame p context))
840        (i 0 (1+ i))
841        (q (last-frame-ptr context)))
842       ((or (null p) (eq p q) (%stack< q p context)))
843    (declare (fixnum i))
844    (if (= i n)
845      (return p))))
846
847;;; True if the object is in one of the heap areas
848(defun %in-consing-area-p (x area)
849  (declare (optimize (speed 3) (safety 0)) (fixnum x))       ; lie
850  (let* ((low (%fixnum-ref area target::area.low))
851         (high (%fixnum-ref area target::area.high))
852)
853    (declare (fixnum low high))
854    (and (<= low x) (< x high))))
855
856
857
858(defun in-any-consing-area-p (x)
859  (do-consing-areas (area)
860    (when (%in-consing-area-p x area)
861      (return t))))
862
863
864
865
866
867
868
869
870
871;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
872;;;
873;;; terminate-when-unreachable
874;;;
875
876#|
877Message-Id: <v02130502ad3e6a2f1542@[205.231.144.48]>
878Mime-Version: 1.0
879Content-Type: text/plain; charset="us-ascii"
880Date: Wed, 7 Feb 1996 10:32:55 -0500
881To: pmcldev@digitool.com
882From: bitCraft@taconic.net (Bill St. Clair)
883Subject: terminate-when-unreachable
884
885I propose that we add a general termination mechanism to PPC MCL.
886We need it to properly terminate stack groups, it would be
887a nicer way to do the termination for macptrs than the current
888ad-hoc mechanism (which BTW is not yet part of PPC MCL), and
889it is a nice addition to MCL. I don't think it's hard to make
890the garbage collector support this, and I volunteer to do the
891work unless Gary really wants to.
892
893I see two ways to support termination:
894
8951) Do termination for hash tables. This was our plan for
896   2.0, but Gary got confused about how to mark the objects at
897   the right time (or so I remember).
898
8992) Resurrect weak alists (they're not part of the PPC garbage
900   collector) and add a termination bit to the population type.
901   This allows for termination of weak lists and weak alists,
902   though the termination mechanism really only needs termination
903   for a single weak alist.
904
905I prefer option 2, weak alists, since it avoids the overhead
906necessary to grow and rehash a hash table. It also uses less space,
907since a finalizeable hash table needs to allocate two cons cells
908for each entry so that the finalization code has some place to
909put the deleted entry.
910
911I propose the following interface (slightly modified from what
912Apple Dylan provides):
913
914terminate-when-unreachable object &optional (function 'terminate)
915  When OBJECT becomes unreachable, funcall FUNCTION with OBJECT
916  as a single argument. Each call of terminate-when-unreachable
917  on a single (EQ) object registers a new termination function.
918  All will be called when the object becomes unreachable.
919
920terminate object                                         [generic function]
921  The default termination function
922
923terminate (object t)                                     [method]
924  The default method. Ignores object. Returns nil.
925
926drain-termination-queue                                  [function]
927  Drain the termination queue. I.e. call the termination function
928  for every object that has become unreachable.
929
930*enable-automatic-termination*                           [variable]
931  If true, the default, drain-termination-queue will be automatically
932  called on the first event check after the garbage collector runs.
933  If you set this to false, you are responsible for calling
934  drain-termination-queue.
935
936cancel-terminate-when-unreachable object &optional function
937  Removes the effect of the last call to terminate-when-unreachable
938  for OBJECT & FUNCTION (both tested with EQ). Returns true if
939  it found a match (which it won't if the object has been moved
940  to the termination queue since terminate-when-unreachable was called).
941  If FUNCTION is NIL or unspecified, then it will not be used; the
942  last call to terminate-when-unreachable with the given OBJECT will
943  be undone.
944
945termination-function object
946  Return the function passed to the last call of terminate-when-unreachable
947  for OBJECT. Will be NIL if the object has been put in the
948  termination queue since terminate-when-unreachable was called.
949
950|#
951
952
953(defstatic *termination-population*
954  (%cons-terminatable-alist))
955
956(defstatic *termination-population-lock* (make-lock))
957
958
959(defvar *enable-automatic-termination* t)
960
961(defun terminate-when-unreachable (object &optional (function 'terminate))
962  "The termination mechanism is a way to have the garbage collector run a
963function right before an object is about to become garbage. It is very
964similar to the finalization mechanism which Java has. It is not standard
965Common Lisp, although other Lisp implementations have similar features.
966It is useful when there is some sort of special cleanup, deallocation,
967or releasing of resources which needs to happen when a certain object is
968no longer being used."
969  (let ((new-cell (cons object function))
970        (population *termination-population*))
971    (without-interrupts
972     (with-lock-grabbed (*termination-population-lock*)
973       (atomic-push-uvector-cell population population.data new-cell)))
974    function))
975
976(defmethod terminate ((object t))
977  nil)
978
979(defun drain-termination-queue ()
980  (with-lock-grabbed (*termination-population-lock*)
981    (let* ((population *termination-population*))
982      (loop
983        (multiple-value-bind (cell existed)
984            (atomic-pop-uvector-cell population population.termination-list)
985          (if (not existed)
986            (return)
987          (funcall (cdr cell) (car cell))))))))
988
989(defun cancel-terminate-when-unreachable (object &optional (function nil function-p))
990  (let* ((found nil))
991    (with-lock-grabbed (*termination-population-lock*)
992      ;; Have to defer GCing, e.g., defer responding to a GC
993      ;; suspend request here (that also defers interrupts)
994      ;; We absolutely, positively can't take an exception
995      ;; in here, so don't even bother to typecheck on
996      ;; car/cdr etc.
997      (with-deferred-gc
998          (do ((spine (population-data *termination-population*) (cdr spine))
999               (prev nil spine))
1000              ((null spine))
1001            (declare (optimize (speed 3) (safety 0)))
1002            (let* ((head (car spine))
1003                   (tail (cdr spine))
1004                   (o (car head))
1005                   (f (cdr head)))
1006              (when (and (eq o object)
1007                         (or (null function-p)
1008                             (eq function f)))
1009                (if prev
1010                  (setf (cdr prev) tail)
1011                  (setf (population-data *termination-population*) tail))
1012                (setq found t)
1013                (return)))))
1014      found)))
1015
1016
1017(defun termination-function (object)
1018  (without-interrupts
1019   (with-lock-grabbed (*termination-population-lock*)
1020     (cdr (assq object (population-data *termination-population*))))))
1021
1022(defun do-automatic-termination ()
1023  (when *enable-automatic-termination*
1024    (drain-termination-queue)))
1025
1026(queue-fixup
1027 (add-gc-hook 'do-automatic-termination :post-gc))
1028
1029;;; A callback to handle foreign thread preparation, initialization,
1030;;; and termination.
1031;;; "preparation" involves telling the kernel to reserve space for
1032;;; some initial thread-specific special bindings.  The kernel
1033;;; needs to reserve this space on the foreign thread's vstack;
1034;;; it needs us to tell it how much space to reserve (enough
1035;;; for bindings of *current-thread*, *current-process*, and
1036;;; the default initial bindings of *PACKAGE*, etc.)
1037;;;
1038;;; "initialization" involves making those special bindings in
1039;;; the vstack space reserved by the kernel, and setting the
1040;;; values of *current-thread* and *current-process* to newly
1041;;; created values.
1042;;;
1043;;; "termination" involves removing the current thread and
1044;;; current process from the global thread/process lists.
1045;;; "preparation" and "initialization" happen when the foreign
1046;;; thread first tries to call lisp code.  "termination" happens
1047;;; via the pthread thread-local-storage cleanup mechanism.
1048(defcallback %foreign-thread-control (:without-interrupts t :int param :int)
1049  (declare (fixnum param))
1050  (cond ((< param 0) (%foreign-thread-prepare))
1051        ((= param 0) (%foreign-thread-initialize) 0)
1052        (t (%foreign-thread-terminate) 0)))
1053
1054
1055
1056(defun %foreign-thread-prepare ()
1057  (let* ((initial-bindings (standard-initial-bindings)))
1058    (%save-standard-binding-list initial-bindings)
1059    (* 3 (+ 2 (length initial-bindings)))))
1060
1061
1062(defun %foreign-thread-initialize ()
1063  ;; Recover the initial-bindings alist.
1064  (let* ((bsp (%saved-bindings-address))
1065         (initial-bindings (%fixnum-ref bsp )))
1066    (declare (fixnum bsp))
1067    ;; Um, this is a little more complicated now that we use
1068    ;; thread-local shallow binding
1069    (flet ((save-binding (new-value sym prev)
1070             (let* ((idx (symbol-binding-index sym))
1071                    (byte-idx (ash idx target::fixnum-shift))
1072                    (binding-vector (%fixnum-ref (%current-tcr) target::tcr.tlb-pointer))
1073                    (old-value (%fixnum-ref  binding-vector byte-idx)))
1074             (setf (%fixnum-ref binding-vector byte-idx) new-value
1075                   (%fixnum-ref bsp (ash -1 target::word-shift)) old-value
1076                   (%fixnum-ref bsp (ash -2 target::word-shift)) idx
1077                   (%fixnum-ref bsp (ash -3 target::word-shift)) prev
1078                   bsp (- bsp 3)))))
1079      (save-binding nil '*current-lisp-thread* 0)
1080      (save-binding nil '*current-process* bsp)
1081      (dolist (pair initial-bindings)
1082        (save-binding (funcall (cdr pair)) (car pair) bsp))
1083      ;; These may (or may not) be the most recent special bindings.
1084      ;; If they are, just set the current tcr's db-link to point
1085      ;; to BSP; if not, "append" them to the end of the current
1086      ;; linked list.
1087      (let* ((current-db-link (%fixnum-ref (%current-tcr) target::tcr.db-link)))
1088        (declare (fixnum current-db-link))
1089        (if (zerop current-db-link)
1090          (setf (%fixnum-ref (%current-tcr) target::tcr.db-link) bsp)
1091          (do* ((binding current-db-link)
1092                (next (%fixnum-ref binding 0)
1093                      (%fixnum-ref binding 0)))
1094               ()
1095            (if (zerop next)
1096              (return (setf (%fixnum-ref binding 0) bsp))
1097              (setq binding next)))))
1098      ;; Ensure that pending unwind-protects (for WITHOUT-INTERRUPTS
1099      ;; on the callback) don't try to unwind the binding stack beyond
1100      ;; where it was just set.
1101      (do* ((catch (%fixnum-ref (%current-tcr) target::tcr.catch-top)
1102                   (%fixnum-ref catch target::catch-frame.link)))
1103           ((zerop catch))
1104        (declare (fixnum catch))
1105        (when (eql 0 (%fixnum-ref catch target::catch-frame.db-link))
1106          (setf (%fixnum-ref catch target::catch-frame.db-link) bsp)))))
1107  (let* ((thread (new-lisp-thread-from-tcr (%current-tcr) "foreign")))
1108    (setq *current-lisp-thread* thread
1109          *current-process*
1110          (make-process "foreign" :thread thread)
1111          *whostate* "Foreign thread callback")))
1112   
1113;;; Remove the foreign thread's lisp-thread and lisp process from
1114;;; the global lists.
1115(defun %foreign-thread-terminate ()
1116  (let* ((proc *current-process*))
1117    (when proc (remove-from-all-processes proc))))
Note: See TracBrowser for help on using the repository browser.