source: branches/1.2/devel/source/level-1/l1-processes.lisp @ 8130

Last change on this file since 8130 was 8130, checked in by gb, 12 years ago

Merge changes from working-0711 branch

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.2 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
18;; L1-processes.lisp
19
20(cl:in-package "CCL")
21
22
23(let* ((all-processes ())
24       (shutdown-processes ())
25       (all-processes-lock (make-lock)))
26  (defun add-to-all-processes (p)
27    (with-lock-grabbed (all-processes-lock)
28      (pushnew p all-processes :test #'eq)
29      p))
30  (defun remove-from-all-processes (p)
31    (with-lock-grabbed (all-processes-lock)
32      (setq all-processes (delete p all-processes))
33      t))
34  (defun all-processes ()
35    "Obtain a fresh list of all known Lisp threads."
36    (with-lock-grabbed (all-processes-lock)
37      (copy-list all-processes)))
38  (defun shutdown-processes ()
39    (with-lock-grabbed (all-processes-lock)
40      (copy-list shutdown-processes)))
41  (defun %clear-shutdown-proceses ()
42    (setq shutdown-processes nil))
43  (defun add-to-shutdown-processes (p)
44    (with-lock-grabbed (all-processes-lock)
45      (pushnew p shutdown-processes :test #'eq))
46    t)
47  (defun pop-shutdown-processes ()
48    (with-lock-grabbed (all-processes-lock)
49      (pop shutdown-processes)))
50  (defun find-process (id)
51    (etypecase id
52      (process id)
53      (integer (with-lock-grabbed (all-processes-lock)
54                 (find id all-processes
55                       :key #'(lambda (p)
56                                (process-serial-number p)))))
57      (string (with-lock-grabbed (all-processes-lock)
58                (find id all-processes
59                      :key #'(lambda (p)
60                               (process-name p))
61                      :test #'equal))))))
62
63
64
65(defun not-in-current-process (p operation)
66  (if (eq p *current-process*)
67    (error "The current process (~s) can't perform the ~a operation on itself."
68           p operation)))
69
70(defun startup-shutdown-processes ()
71  (let* ((p))
72    (loop
73      (unless (setq p (pop-shutdown-processes)) (return))
74      (new-tcr-for-thread (process-thread p))
75      (%process-preset-internal p)
76      (process-enable p)
77      )))
78
79;;; Done with a queue-fixup so that it will be the last thing
80;;; that happens on startup.
81(queue-fixup
82 (pushnew 'startup-shutdown-processes *lisp-system-pointer-functions*))
83
84
85
86
87
88
89
90(defun wrap-initial-bindings (alist)
91  (mapcar #'(lambda (pair)
92              (destructuring-bind (symbol . valform) pair
93                (cons (require-type symbol 'symbol)
94                      (cond ((symbolp valform)
95                             (constantly (symbol-value valform)))
96                            ((typep valform 'function) valform)
97                            ((consp valform)
98                             (if (eq (car valform) 'quote)
99                               (constantly (cadr valform))
100                               #'(lambda () (apply (car valform) (cdr valform)))))
101                            (t
102                             (constantly valform))))))
103          alist))
104
105
106(defun valid-allocation-quantum-p (x)
107  (and (>= x *host-page-size*)
108       (<= x (default-allocation-quantum))
109       (= (logcount x) 1)))
110
111 
112(let* ((psn -1))
113  (defun %new-psn () (incf psn)))
114
115(defclass process ()
116    ((name :initform nil :initarg :name :accessor process-name)
117     (thread :initarg :thread :accessor process-thread)
118     (initial-form :initform (cons nil nil) :reader process-initial-form)
119     (priority :initform 0 :initarg :priority :accessor process-priority)
120     (persistent :initform nil :initarg :persistent :reader process-persistent)
121     (splice :initform (cons nil nil) :accessor process-splice)
122     (initial-bindings :initform nil :initarg :initial-bindings
123                       :accessor process-initial-bindings)
124     (serial-number :initform (%new-psn) :accessor process-serial-number)
125     (creation-time :initform (get-tick-count) :reader process-creation-time)
126     (total-run-time :initform nil :accessor %process-total-run-time)
127     (ui-object :initform (application-ui-object *application*)
128                :accessor process-ui-object)
129     (termination-semaphore :initform nil
130                            :initarg :termination-semaphore
131                            :accessor process-termination-semaphore
132                            :type (or null semaphore))
133     (allocation-quantum :initform (default-allocation-quantum)
134                         :initarg :allocation-quantum
135                         :reader process-allocation-quantum
136                         :type (satisfies valid-allocation-quantum-p))
137     (dribble-stream :initform nil)
138     (dribble-saved-terminal-io :initform nil)
139     (result :initform (cons nil nil)
140             :reader process-result))
141  (:primary-p t))
142
143(defmethod print-object ((p process) s)
144  (print-unreadable-object (p s :type t :identity t)
145    (format s "~a(~d) [~a]" (process-name p)
146            (process-serial-number p)(process-whostate p))))
147
148(defvar *process-class* (find-class 'process))
149
150(defun processp (p)
151  (memq *process-class* (class-precedence-list (class-of p))))
152
153(set-type-predicate 'process 'processp)
154
155(defun make-process (name &key 
156                          thread
157                          persistent
158                          (priority 0)
159                          (stack-size *default-control-stack-size*)
160                          (vstack-size *default-value-stack-size*)
161                          (tstack-size *default-temp-stack-size*)
162                          (initial-bindings ())
163                          (use-standard-initial-bindings t)
164                          (class (find-class 'process))
165                          (termination-semaphore ())
166                          (allocation-quantum (default-allocation-quantum)))
167  "Create and return a new process."
168  (declare (ignore flavor))
169  (let* ((p (make-instance
170             class
171             :name name
172             :thread (or thread
173                         (new-thread name stack-size  vstack-size  tstack-size))
174             :priority priority
175             :persistent persistent
176             :initial-bindings (append (if use-standard-initial-bindings
177                                         (standard-initial-bindings))
178                                       (wrap-initial-bindings
179                                        initial-bindings))
180             :termination-semaphore (or termination-semaphore
181                                        (make-semaphore))
182             :allocation-quantum allocation-quantum)))
183    (add-to-all-processes p)
184    (setf (car (process-splice p)) p)
185    p))
186
187
188(defstatic *initial-process*
189    (let* ((p (make-process
190               "Initial"
191               :thread *initial-lisp-thread*
192               :priority 0)))
193      p))
194
195
196(defvar *current-process* *initial-process*
197  "Bound in each process, to that process itself.")
198
199(defstatic *interactive-abort-process* *initial-process*)
200
201
202
203
204(defun process-tcr (p)
205  (lisp-thread.tcr (process-thread p)))
206
207
208
209(defun process-exhausted-p (p)
210  (let* ((thread (process-thread p)))
211    (or (null thread)
212        (thread-exhausted-p thread))))
213 
214
215(defun process-whostate (p)
216  "Return a string which describes the status of a specified process."
217  (if (process-exhausted-p p)
218    "Exhausted"
219    (symbol-value-in-process '*whostate* p)))
220
221(defun (setf process-whostate) (new p)
222  (unless (process-exhausted-p p)
223    (setf (symbol-value-in-process '*whostate* p) new)))
224
225
226
227(defun process-total-run-time (p)
228  (or (%process-total-run-time p)
229      (thread-total-run-time (process-thread p))))
230
231
232
233
234(defun initial-bindings (alist)
235  (let* ((symbols ())
236         (values ()))
237    (dolist (a alist (values (nreverse symbols) (nreverse values)))
238      (push (car a) symbols)
239      (push (funcall (cdr a)) values))))
240
241
242                           
243(defun symbol-value-in-process (sym process)
244  (if (eq process *current-process*)
245    (symbol-value sym)
246    (symbol-value-in-tcr sym (process-tcr process))))
247
248(defun (setf symbol-value-in-process) (value sym process)
249  (if (eq process *current-process*)
250    (setf (symbol-value sym) value)
251    (setf (symbol-value-in-tcr sym (process-tcr process)) value)))
252
253
254(defun process-enable (p &optional (wait 1))
255  "Begin executing the initial function of a specified process."
256  (setq p (require-type p 'process))
257  (not-in-current-process p 'process-enable)
258  (unless (car (process-initial-form p))
259    (error "Process ~s has not been preset.  Use PROCESS-PRESET to preset the process." p))
260  (let* ((thread (process-thread p)))
261    (do* ((total-wait wait (+ total-wait wait)))
262         ((thread-enable thread (process-termination-semaphore p) (1- (integer-length (process-allocation-quantum p)))  wait)
263          p)
264      (cerror "Keep trying."
265              "Unable to enable process ~s; have been trying for ~s seconds."
266              p total-wait))))
267
268
269(defmethod (setf process-termination-semaphore) :after (new (p process))
270  (with-macptrs (tcrp)
271    (%setf-macptr-to-object tcrp (process-tcr p))
272    (unless (%null-ptr-p tcrp)
273      (setf (%get-ptr tcrp target::tcr.termination-semaphore)
274            (if new
275              (semaphore-value new)
276              (%null-ptr))))
277    new))
278
279(defun process-resume (p)
280  "Resume a specified process which had previously been suspended
281by process-suspend."
282  (setq p (require-type p 'process))
283  (%resume-tcr (process-tcr p)))
284
285(defun process-suspend (p)
286  "Suspend a specified process."
287  (setq p (require-type p 'process))
288  (if (eq p *current-process*)
289    (error "Suspending the current process can't work.  ~&(If the documentation claims otherwise, it's incorrect.)")
290    (%suspend-tcr (process-tcr p))))
291
292(defun process-suspend-count (p)
293  "Return the number of currently-pending suspensions applicable to
294a given process."
295  (setq p (require-type p 'process))
296  (let* ((thread (process-thread p)))
297    (if thread
298      (lisp-thread-suspend-count thread))))
299
300(defun process-active-p (p)
301  (setq p (require-type p 'process))
302  (and (eql 0 (process-suspend-count p))
303       (not (process-exhausted-p p))))
304 
305;;; Used by process-run-function
306(defun process-preset (process function &rest args)
307  "Set the initial function and arguments of a specified process."
308  (let* ((p (require-type process 'process))
309         (f (require-type function 'function))
310         (initial-form (process-initial-form p)))
311    (declare (type cons initial-form))
312    (not-in-current-process p 'process-preset)
313    ; Not quite right ...
314    (rplaca initial-form f)
315    (rplacd initial-form args)
316    (%process-preset-internal process)))
317
318(defun %process-preset-internal (process)
319   (let* ((initial-form (process-initial-form process))
320         (thread (process-thread process)))
321     (declare (type cons initial-form))
322     (thread-preset
323      thread
324      #'(lambda (process initial-form)
325          (let* ((*current-process* process))
326            (add-to-all-processes process)
327            (multiple-value-bind (syms values)
328                (initial-bindings (process-initial-bindings process))
329              (progv syms values
330                (setq *whostate* "Active")
331                (run-process-initial-form process initial-form)))))
332      process
333      initial-form)
334     process))
335
336
337(defun run-process-initial-form (process initial-form)
338  (let* ((exited nil)
339         (kill (handler-case
340                   (restart-case
341                    (let ((values
342                           (multiple-value-list
343                            (apply (car initial-form)
344                                   (cdr (the list initial-form)))))
345                          (result (process-result process)))
346                      (setf (cdr result) values
347                            (car result) t)
348                      (setq exited t)
349                      nil)
350                    (abort-break () :report "Reset this process")
351                    (abort () :report "Kill this process" (setq exited t)))
352                 (process-reset (condition)
353                   (process-reset-kill condition)))))
354    ;; We either exited from the initial form normally, were told to
355    ;; exit prematurely, or are being reset and should enter the
356    ;; "awaiting preset" state.
357    (if (or kill exited) 
358      (unless (eq kill :toplevel)
359        (process-initial-form-exited process (or kill t)))
360      (progn
361        (thread-change-state (process-thread process) :run :reset)
362        (tcr-set-preset-state (process-tcr process))))
363    nil))
364
365;;; Separated from run-process-initial-form just so I can change it easily.
366(defun process-initial-form-exited (process kill)
367  ;; Enter the *initial-process* and have it finish us up
368  (without-interrupts
369   (if (eq kill :shutdown)
370     (progn
371       (setq *whostate* "Shutdown")
372       (add-to-shutdown-processes process)))
373   (maybe-finish-process-kill process kill)))
374
375(defun maybe-finish-process-kill (process kill)
376  (when (and kill (neq kill :shutdown))
377    (setf (process-whostate process) "Dead")
378    (remove-from-all-processes process)
379    (let ((thread (process-thread process)))
380      (unless (or (eq thread *current-lisp-thread*)
381                  (thread-exhausted-p thread))
382        (kill-lisp-thread thread))))
383  nil)
384
385
386 
387
388(defun require-global-symbol (s &optional env)
389  (let* ((s (require-type s 'symbol))
390         (bits (%symbol-bits s)))
391    (unless (or (logbitp $sym_vbit_global bits)
392                (let* ((defenv (if env (definition-environment env))))
393                  (if defenv
394                    (eq :global (%cdr (assq s (defenv.specials defenv)))))))
395      (error "~s not defined with ~s" s 'defstatic))
396    s))
397
398
399(defmethod print-object ((s lock) stream)
400  (print-unreadable-object (s stream :type t :identity t)
401    (let* ((val (uvref s target::lock._value-cell))
402           (name (uvref s target::lock.name-cell)))
403      (when name
404        (format stream "~s " name))
405      (if (typep val 'macptr)
406        (format stream "[ptr @ #x~x]"
407                (%ptr-to-int val))))))
408
409(defun lockp (l)
410  (eq target::subtag-lock (typecode l)))
411
412(set-type-predicate 'lock 'lockp)
413
414(defun recursive-lock-p (l)
415  (and (eq target::subtag-lock (typecode l))
416       (eq 'recursive-lock (%svref l target::lock.kind-cell))))
417
418(defun read-write-lock-p (l)
419  (and (eq target::subtag-lock (typecode l))
420       (eq 'read-write-lock (%svref l target::lock.kind-cell))))
421
422(setf (type-predicate 'recursive-lock) 'recursive-lock-p
423      (type-predicate 'read-write-lock) 'read-write-lock-p)
424
425
426(defun grab-lock (lock &optional flag)
427  "Wait until a given lock can be obtained, then obtain it."
428  (%lock-recursive-lock-object lock flag))
429
430(defun release-lock (lock)
431  "Relinquish ownership of a given lock."
432  (%unlock-recursive-lock-object lock))
433
434(defun try-lock (lock &optional flag)
435  "Obtain the given lock, but only if it is not necessary to wait for it."
436  (%try-recursive-lock-object lock flag))
437
438(defun lock-acquisition-status (thing)
439  (if (istruct-typep thing 'lock-acquisition)
440    (lock-acquisition.status thing)
441    (report-bad-arg thing 'lock-acquisition)))
442
443(defun clear-lock-acquisition-status (thing)
444  (if (istruct-typep thing 'lock-acquisition)
445    (setf (lock-acquisition.status thing) nil)
446    (report-bad-arg thing 'lock-acquisition)))
447
448(defmethod print-object ((l lock-acquisition) stream)
449  (print-unreadable-object (l stream :type t :identity t)
450    (format stream "[status = ~s]" (lock-acquisition-status l))))
451
452(defun semaphore-notification-status (thing)
453  (if (istruct-typep thing 'semaphore-notification)
454    (semaphore-notification.status thing)
455    (report-bad-arg thing 'semaphore-notification)))
456
457(defun clear-semaphore-notification-status (thing)
458  (if (istruct-typep thing 'semaphore-notification)
459    (setf (semaphore-notification.status thing) nil)
460    (report-bad-arg thing 'semaphore-notification)))
461
462(defmethod print-object ((l semaphore-notification) stream)
463  (print-unreadable-object (l stream :type t :identity t)
464    (format stream "[status = ~s]" (semaphore-notification-status l))))
465
466(defun process-wait (whostate function &rest args)
467  "Causes the current lisp process (thread) to wait for a given
468predicate to return true."
469  (declare (dynamic-extent args))
470  (or (apply function args)
471      (with-process-whostate (whostate)
472        (loop
473          (when (apply function args)
474            (return))
475          ;; Sleep for a tick
476          (%nanosleep 0 *ns-per-tick*)))))
477
478
479
480(defun process-wait-with-timeout (whostate time function &rest args)
481  "Cause the current thread to wait for a given predicate to return true,
482or for a timeout to expire."
483  (declare (dynamic-extent args))
484  (cond ((null time)  (apply #'process-wait whostate function args) t)
485        (t (let* ((win nil)
486                  (when (+ (get-tick-count) time))
487                  (f #'(lambda () (let ((val (apply function args)))
488                                    (if val
489                                      (setq win val)
490                                      (> (get-tick-count) when))))))
491             (declare (dynamic-extent f))
492             (process-wait whostate f)
493             win))))
494
495
496(defmethod process-interrupt ((process process) function &rest args)
497  "Arrange for the target process to invoke a specified function at
498some point in the near future, and then return to what it was doing."
499  (let* ((p (require-type process 'process)))
500    (if (eq p *current-process*)
501      (progn
502        (apply function args)
503        t)
504      (thread-interrupt
505       (process-thread p)
506       process
507       #'apply
508       function args))))
509
510(defmethod process-debug-condition ((p process) condition frame-pointer)
511  (declare (ignore condition frame-pointer)))
512
513
514
515
516;;; This one is in the Symbolics documentation
517(defun process-allow-schedule ()
518  "Used for cooperative multitasking; probably never necessary."
519  (yield))
520
521
522;;; something unique that users won't get their hands on
523(defun process-reset-tag (process)
524  (process-splice process))
525
526(defun process-run-function (name-or-keywords function &rest args)
527  "Create a process, preset it, and enable it."
528  (if (listp name-or-keywords)
529    (%process-run-function name-or-keywords function args)
530    (let ((keywords (list :name name-or-keywords)))
531      (declare (dynamic-extent keywords))
532      (%process-run-function keywords function args))))
533
534(defun %process-run-function (keywords function args)
535  (destructuring-bind (&key (name "Anonymous")
536                            (priority  0)
537                            (stack-size *default-control-stack-size*)
538                            (vstack-size *default-value-stack-size*)
539                            (tstack-size *default-temp-stack-size*)
540                            (initial-bindings ())
541                            (persistent nil)
542                            (use-standard-initial-bindings t)
543                            (termination-semaphore nil)
544                            (allocation-quantum (default-allocation-quantum)))
545                      keywords
546    (setq priority (require-type priority 'fixnum))
547    (let* ((process (make-process name
548                                  :priority priority
549                                  :stack-size stack-size
550                                  :vstack-size vstack-size
551                                  :tstack-size tstack-size
552                                  :persistent persistent
553                                  :use-standard-initial-bindings use-standard-initial-bindings
554                                  :initial-bindings initial-bindings
555                                  :termination-semaphore termination-semaphore
556                                  :allocation-quantum allocation-quantum)))
557      (process-preset process #'(lambda () (apply function args)))
558      (process-enable process)
559      process)))
560
561(defmethod process-reset ((process process) &optional kill)
562  "Cause a specified process to cleanly exit from any ongoing computation."
563  (setq process (require-type process 'process))
564  (unless (memq kill '(nil :kill :shutdown))
565    (setq kill (require-type kill '(member nil :kill :shutdown))))
566  (if (eq process *current-process*)
567    (%process-reset kill)
568    (if (process-exhausted-p process)
569      (maybe-finish-process-kill process kill)
570      (progn
571        (process-interrupt process '%process-reset kill)))))
572
573
574(defun %process-reset (kill)
575  (signal 'process-reset :kill kill)
576  (maybe-finish-process-kill *current-process* kill))
577
578;;; By default, it's just fine with the current process
579;;; if the application/user wants to quit.
580(defmethod process-verify-quit ((process process))
581  t)
582
583(defmethod process-exit-application ((process process) thunk)
584  (when (eq process *initial-process*)
585    (prepare-to-quit)
586    (%set-toplevel thunk)
587    (fresh-line *stdout*)
588    (finish-output *stdout*)
589    (toplevel)))
590
591
592
593(defmethod process-kill ((process process))
594  "Cause a specified process to cleanly exit from any ongoing
595computation, and then exit."
596  (and (process-interrupt process #'%process-reset :kill)
597       (setf (process-kill-issued process) t)))
598
599(defun process-abort (process &optional condition)
600  "Cause a specified process to process an abort condition, as if it
601had invoked abort."
602  (process-interrupt process
603                     #'(lambda ()
604                         (abort condition))))
605
606(defmethod process-reset-and-enable ((process process))
607  (not-in-current-process process 'process-reset-and-enable)
608  (process-reset process)
609  (process-enable process))
610
611(defmethod process-kill-issued ((process process))
612  (cdr (process-splice process)))
613
614(defmethod (setf process-kill-issued) (val (process process))
615  (setf (cdr (process-splice process)) val))
616
617(defun tcr->process (tcr)
618  (dolist (p (all-processes))
619    (when (eq tcr (process-tcr p))
620      (return p))))
621
622(defun current-process-allocation-quantum ()
623  (process-allocation-quantum *current-process*))
624
625(defun (setf current-process-allocation-quantum) (new)
626  (if (valid-allocation-quantum-p new)
627    (with-macptrs (tcrp)
628      (%setf-macptr-to-object tcrp (%current-tcr))
629      (setf (slot-value *current-process* 'allocation-quantum) new
630            (%get-natural tcrp target::tcr.log2-allocation-quantum)
631            (1- (integer-length new)))
632      new)
633    (report-bad-arg new '(satisfies valid-allocation-quantum-p))))
634
635
636(def-standard-initial-binding *backtrace-contexts* nil)
637
638(defmethod exit-interactive-process ((p process))
639  (unless (eq p *initial-process*)
640    (when (eq p *current-process*)
641      (process-kill p))))
642
643(defclass tty-listener (process)
644    ())
645
646(defmethod exit-interactive-process ((p tty-listener))
647  (when (eq p *current-process*)
648    (quit)))
649
650(defmethod process-stop-dribbling ((p process))
651  (with-slots (dribble-stream dribble-saved-terminal-io) p
652    (when dribble-stream
653      (close dribble-stream)
654      (setq dribble-stream nil))
655    (when dribble-saved-terminal-io
656      (setq *terminal-io* dribble-saved-terminal-io
657            dribble-saved-terminal-io nil))))
658
659(defmethod process-dribble ((p process) path)
660  (with-slots (dribble-stream dribble-saved-terminal-io) p
661    (process-stop-dribbling p)
662    (when path
663      (let* ((in (two-way-stream-input-stream *terminal-io*))
664             (out (two-way-stream-output-stream *terminal-io*))
665             (f (open path :direction :output :if-exists :append 
666                      :if-does-not-exist :create)))
667        (without-interrupts
668         (setq dribble-stream f
669               dribble-saved-terminal-io *terminal-io*
670               *terminal-io* (make-echoing-two-way-stream
671                              (make-echo-stream in f)
672                              (make-broadcast-stream out f)))))
673      path)))
674
675(defmethod join-process ((p process) &key (default nil defaultp))
676  (wait-on-semaphore (process-termination-semaphore p) nil "join-process")
677  (let ((result (process-result p)))
678    (cond ((car result) (values-list (cdr result)))
679          (defaultp default)
680          (t (error "Failed to join ~s" p)))))
681
682
683(defmethod process-locks-held ((p process))
684  #+lock-accounting
685  (copy-list (symbol-value-in-process '*locks-held* p)))
686
687(defmethod process-locks-pending ((p process))
688  #+lock-accounting
689  (copy-list (symbol-value-in-process '*locks-pending* p)))
Note: See TracBrowser for help on using the repository browser.