source: branches/working-0711/ccl/level-1/l1-processes.lisp @ 7949

Last change on this file since 7949 was 7949, checked in by gb, 13 years ago

Keep whostate in a per-thread variable, not in the PROCESS-OBJECT.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.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
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  (symbol-value-in-tcr sym (process-tcr process)))
245
246(defun (setf symbol-value-in-process) (value sym process)
247  (setf (symbol-value-in-tcr sym (process-tcr process)) value))
248
249
250(defun process-enable (p &optional (wait 1))
251  "Begin executing the initial function of a specified process."
252  (setq p (require-type p 'process))
253  (not-in-current-process p 'process-enable)
254  (unless (car (process-initial-form p))
255    (error "Process ~s has not been preset.  Use PROCESS-PRESET to preset the process." p))
256  (let* ((thread (process-thread p)))
257    (do* ((total-wait wait (+ total-wait wait)))
258         ((thread-enable thread (process-termination-semaphore p) (1- (integer-length (process-allocation-quantum p)))  wait)
259          p)
260      (cerror "Keep trying."
261              "Unable to enable process ~s; have been trying for ~s seconds."
262              p total-wait))))
263
264
265(defmethod (setf process-termination-semaphore) :after (new (p process))
266  (with-macptrs (tcrp)
267    (%setf-macptr-to-object tcrp (process-tcr p))
268    (unless (%null-ptr-p tcrp)
269      (setf (%get-ptr tcrp target::tcr.termination-semaphore)
270            (if new
271              (semaphore-value new)
272              (%null-ptr))))
273    new))
274
275(defun process-resume (p)
276  "Resume a specified process which had previously been suspended
277by process-suspend."
278  (setq p (require-type p 'process))
279  (%resume-tcr (process-tcr p)))
280
281(defun process-suspend (p)
282  "Suspend a specified process."
283  (setq p (require-type p 'process))
284  (if (eq p *current-process*)
285    (error "Suspending the current process can't work.  ~&(If the documentation claims otherwise, it's incorrect.)")
286    (%suspend-tcr (process-tcr p))))
287
288(defun process-suspend-count (p)
289  "Return the number of currently-pending suspensions applicable to
290a given process."
291  (setq p (require-type p 'process))
292  (let* ((thread (process-thread p)))
293    (if thread
294      (lisp-thread-suspend-count thread))))
295
296(defun process-active-p (p)
297  (setq p (require-type p 'process))
298  (and (eql 0 (process-suspend-count p))
299       (not (process-exhausted-p p))))
300 
301;;; Used by process-run-function
302(defun process-preset (process function &rest args)
303  "Set the initial function and arguments of a specified process."
304  (let* ((p (require-type process 'process))
305         (f (require-type function 'function))
306         (initial-form (process-initial-form p)))
307    (declare (type cons initial-form))
308    (not-in-current-process p 'process-preset)
309    ; Not quite right ...
310    (rplaca initial-form f)
311    (rplacd initial-form args)
312    (%process-preset-internal process)))
313
314(defun %process-preset-internal (process)
315   (let* ((initial-form (process-initial-form process))
316         (thread (process-thread process)))
317     (declare (type cons initial-form))
318     (thread-preset
319      thread
320      #'(lambda (process initial-form)
321          (let* ((*current-process* process))
322            (add-to-all-processes process)
323            (multiple-value-bind (syms values)
324                (initial-bindings (process-initial-bindings process))
325              (progv syms values
326                (setq *whostate* "Active")
327                (run-process-initial-form process initial-form)))))
328      process
329      initial-form)
330     process))
331
332
333(defun run-process-initial-form (process initial-form)
334  (let* ((exited nil)
335         (kill (handler-case
336                   (restart-case
337                    (let ((values
338                           (multiple-value-list
339                            (apply (car initial-form)
340                                   (cdr (the list initial-form)))))
341                          (result (process-result process)))
342                      (setf (cdr result) values
343                            (car result) t)
344                      (setq exited t)
345                      nil)
346                    (abort-break () :report "Reset this process")
347                    (abort () :report "Kill this process" (setq exited t)))
348                 (process-reset (condition)
349                   (process-reset-kill condition)))))
350    ;; We either exited from the initial form normally, were told to
351    ;; exit prematurely, or are being reset and should enter the
352    ;; "awaiting preset" state.
353    (if (or kill exited) 
354      (unless (eq kill :toplevel)
355        (process-initial-form-exited process (or kill t)))
356      (progn
357        (thread-change-state (process-thread process) :run :reset)
358        (tcr-set-preset-state (process-tcr process))))
359    nil))
360
361;;; Separated from run-process-initial-form just so I can change it easily.
362(defun process-initial-form-exited (process kill)
363  ;; Enter the *initial-process* and have it finish us up
364  (without-interrupts
365   (if (eq kill :shutdown)
366     (progn
367       (setq *whostate* "Shutdown")
368       (add-to-shutdown-processes process)))
369   (maybe-finish-process-kill process kill)))
370
371(defun maybe-finish-process-kill (process kill)
372  (when (and kill (neq kill :shutdown))
373    (setf (process-whostate process) "Dead")
374    (remove-from-all-processes process)
375    (let ((thread (process-thread process)))
376      (unless (or (eq thread *current-lisp-thread*)
377                  (thread-exhausted-p thread))
378        (kill-lisp-thread thread))))
379  nil)
380
381
382 
383
384(defun require-global-symbol (s &optional env)
385  (let* ((s (require-type s 'symbol))
386         (bits (%symbol-bits s)))
387    (unless (or (logbitp $sym_vbit_global bits)
388                (let* ((defenv (if env (definition-environment env))))
389                  (if defenv
390                    (eq :global (%cdr (assq s (defenv.specials defenv)))))))
391      (error "~s not defined with ~s" s 'defstatic))
392    s))
393
394
395(defmethod print-object ((s lock) stream)
396  (print-unreadable-object (s stream :type t :identity t)
397    (let* ((val (uvref s target::lock._value-cell))
398           (name (uvref s target::lock.name-cell)))
399      (when name
400        (format stream "~s " name))
401      (if (typep val 'macptr)
402        (format stream "[ptr @ #x~x]"
403                (%ptr-to-int val))))))
404
405(defun lockp (l)
406  (eq target::subtag-lock (typecode l)))
407
408(set-type-predicate 'lock 'lockp)
409
410(defun recursive-lock-p (l)
411  (and (eq target::subtag-lock (typecode l))
412       (eq 'recursive-lock (%svref l target::lock.kind-cell))))
413
414(defun read-write-lock-p (l)
415  (and (eq target::subtag-lock (typecode l))
416       (eq 'read-write-lock (%svref l target::lock.kind-cell))))
417
418(setf (type-predicate 'recursive-lock) 'recursive-lock-p
419      (type-predicate 'read-write-lock) 'read-write-lock-p)
420
421
422(defun grab-lock (lock &optional flag)
423  "Wait until a given lock can be obtained, then obtain it."
424  (%lock-recursive-lock-object lock flag))
425
426(defun release-lock (lock)
427  "Relinquish ownership of a given lock."
428  (%unlock-recursive-lock-object lock))
429
430(defun try-lock (lock &optional flag)
431  "Obtain the given lock, but only if it is not necessary to wait for it."
432  (%try-recursive-lock-object lock flag))
433
434(defun lock-acquisition-status (thing)
435  (if (istruct-typep thing 'lock-acquisition)
436    (lock-acquisition.status thing)
437    (report-bad-arg thing 'lock-acquisition)))
438
439(defun clear-lock-acquisition-status (thing)
440  (if (istruct-typep thing 'lock-acquisition)
441    (setf (lock-acquisition.status thing) nil)
442    (report-bad-arg thing 'lock-acquisition)))
443
444(defmethod print-object ((l lock-acquisition) stream)
445  (print-unreadable-object (l stream :type t :identity t)
446    (format stream "[status = ~s]" (lock-acquisition-status l))))
447
448(defun semaphore-notification-status (thing)
449  (if (istruct-typep thing 'semaphore-notification)
450    (semaphore-notification.status thing)
451    (report-bad-arg thing 'semaphore-notification)))
452
453(defun clear-semaphore-notification-status (thing)
454  (if (istruct-typep thing 'semaphore-notification)
455    (setf (semaphore-notification.status thing) nil)
456    (report-bad-arg thing 'semaphore-notification)))
457
458(defmethod print-object ((l semaphore-notification) stream)
459  (print-unreadable-object (l stream :type t :identity t)
460    (format stream "[status = ~s]" (semaphore-notification-status l))))
461
462(defun process-wait (whostate function &rest args)
463  "Causes the current lisp process (thread) to wait for a given
464predicate to return true."
465  (declare (dynamic-extent args))
466  (or (apply function args)
467      (with-process-whostate (whostate)
468        (loop
469          (when (apply function args)
470            (return))
471          ;; Sleep for a tick
472          (%nanosleep 0 *ns-per-tick*)))))
473
474
475
476(defun process-wait-with-timeout (whostate time function &rest args)
477  "Cause the current thread to wait for a given predicate to return true,
478or for a timeout to expire."
479  (declare (dynamic-extent args))
480  (cond ((null time)  (apply #'process-wait whostate function args) t)
481        (t (let* ((win nil)
482                  (when (+ (get-tick-count) time))
483                  (f #'(lambda () (let ((val (apply function args)))
484                                    (if val
485                                      (setq win val)
486                                      (> (get-tick-count) when))))))
487             (declare (dynamic-extent f))
488             (process-wait whostate f)
489             win))))
490
491
492(defmethod process-interrupt ((process process) function &rest args)
493  "Arrange for the target process to invoke a specified function at
494some point in the near future, and then return to what it was doing."
495  (let* ((p (require-type process 'process)))
496    (if (eq p *current-process*)
497      (progn
498        (apply function args)
499        t)
500      (thread-interrupt
501       (process-thread p)
502       process
503       #'apply
504       function args))))
505
506(defmethod process-debug-condition ((p process) condition frame-pointer)
507  (declare (ignore condition frame-pointer)))
508
509
510
511
512;;; This one is in the Symbolics documentation
513(defun process-allow-schedule ()
514  "Used for cooperative multitasking; probably never necessary."
515  (yield))
516
517
518;;; something unique that users won't get their hands on
519(defun process-reset-tag (process)
520  (process-splice process))
521
522(defun process-run-function (name-or-keywords function &rest args)
523  "Create a process, preset it, and enable it."
524  (if (listp name-or-keywords)
525    (%process-run-function name-or-keywords function args)
526    (let ((keywords (list :name name-or-keywords)))
527      (declare (dynamic-extent keywords))
528      (%process-run-function keywords function args))))
529
530(defun %process-run-function (keywords function args)
531  (destructuring-bind (&key (name "Anonymous")
532                            (priority  0)
533                            (stack-size *default-control-stack-size*)
534                            (vstack-size *default-value-stack-size*)
535                            (tstack-size *default-temp-stack-size*)
536                            (initial-bindings ())
537                            (persistent nil)
538                            (use-standard-initial-bindings t)
539                            (termination-semaphore nil)
540                            (allocation-quantum (default-allocation-quantum)))
541                      keywords
542    (setq priority (require-type priority 'fixnum))
543    (let* ((process (make-process name
544                                  :priority priority
545                                  :stack-size stack-size
546                                  :vstack-size vstack-size
547                                  :tstack-size tstack-size
548                                  :persistent persistent
549                                  :use-standard-initial-bindings use-standard-initial-bindings
550                                  :initial-bindings initial-bindings
551                                  :termination-semaphore termination-semaphore
552                                  :allocation-quantum allocation-quantum)))
553      (process-preset process #'(lambda () (apply function args)))
554      (process-enable process)
555      process)))
556
557(defmethod process-reset ((process process) &optional kill)
558  "Cause a specified process to cleanly exit from any ongoing computation."
559  (setq process (require-type process 'process))
560  (unless (memq kill '(nil :kill :shutdown))
561    (setq kill (require-type kill '(member nil :kill :shutdown))))
562  (if (eq process *current-process*)
563    (%process-reset kill)
564    (if (process-exhausted-p process)
565      (maybe-finish-process-kill process kill)
566      (progn
567        (process-interrupt process '%process-reset kill)))))
568
569
570(defun %process-reset (kill)
571  (signal 'process-reset :kill kill)
572  (maybe-finish-process-kill *current-process* kill))
573
574;;; By default, it's just fine with the current process
575;;; if the application/user wants to quit.
576(defmethod process-verify-quit ((process process))
577  t)
578
579(defmethod process-exit-application ((process process) thunk)
580  (when (eq process *initial-process*)
581    (prepare-to-quit)
582    (%set-toplevel thunk)
583    (fresh-line *stdout*)
584    (finish-output *stdout*)
585    (toplevel)))
586
587
588
589(defmethod process-kill ((process process))
590  "Cause a specified process to cleanly exit from any ongoing
591computation, and then exit."
592  (and (process-interrupt process #'%process-reset :kill)
593       (setf (process-kill-issued process) t)))
594
595(defun process-abort (process &optional condition)
596  "Cause a specified process to process an abort condition, as if it
597had invoked abort."
598  (process-interrupt process
599                     #'(lambda ()
600                         (abort condition))))
601
602(defmethod process-reset-and-enable ((process process))
603  (not-in-current-process process 'process-reset-and-enable)
604  (process-reset process)
605  (process-enable process))
606
607(defmethod process-kill-issued ((process process))
608  (cdr (process-splice process)))
609
610(defmethod (setf process-kill-issued) (val (process process))
611  (setf (cdr (process-splice process)) val))
612
613(defun tcr->process (tcr)
614  (dolist (p (all-processes))
615    (when (eq tcr (process-tcr p))
616      (return p))))
617
618(defun current-process-allocation-quantum ()
619  (process-allocation-quantum *current-process*))
620
621(defun (setf current-process-allocation-quantum) (new)
622  (if (valid-allocation-quantum-p new)
623    (with-macptrs (tcrp)
624      (%setf-macptr-to-object tcrp (%current-tcr))
625      (setf (slot-value *current-process* 'allocation-quantum) new
626            (%get-natural tcrp target::tcr.log2-allocation-quantum)
627            (1- (integer-length new)))
628      new)
629    (report-bad-arg new '(satisfies valid-allocation-quantum-p))))
630
631
632(def-standard-initial-binding *backtrace-contexts* nil)
633
634(defmethod exit-interactive-process ((p process))
635  (unless (eq p *initial-process*)
636    (when (eq p *current-process*)
637      (process-kill p))))
638
639(defclass tty-listener (process)
640    ())
641
642(defmethod exit-interactive-process ((p tty-listener))
643  (when (eq p *current-process*)
644    (quit)))
645
646(defmethod process-stop-dribbling ((p process))
647  (with-slots (dribble-stream dribble-saved-terminal-io) p
648    (when dribble-stream
649      (close dribble-stream)
650      (setq dribble-stream nil))
651    (when dribble-saved-terminal-io
652      (setq *terminal-io* dribble-saved-terminal-io
653            dribble-saved-terminal-io nil))))
654
655(defmethod process-dribble ((p process) path)
656  (with-slots (dribble-stream dribble-saved-terminal-io) p
657    (process-stop-dribbling p)
658    (when path
659      (let* ((in (two-way-stream-input-stream *terminal-io*))
660             (out (two-way-stream-output-stream *terminal-io*))
661             (f (open path :direction :output :if-exists :append 
662                      :if-does-not-exist :create)))
663        (without-interrupts
664         (setq dribble-stream f
665               dribble-saved-terminal-io *terminal-io*
666               *terminal-io* (make-echoing-two-way-stream
667                              (make-echo-stream in f)
668                              (make-broadcast-stream out f)))))
669      path)))
670
671(defmethod join-process ((p process) &key (default nil defaultp))
672  (wait-on-semaphore (process-termination-semaphore p) nil "join-process")
673  (let ((result (process-result p)))
674    (cond ((car result) (values-list (cdr result)))
675          (defaultp default)
676          (t (error "Failed to join ~s" p)))))
677
678
679(defmethod process-locks-held ((p process))
680  #+lock-accounting
681  (copy-list (symbol-value-in-process '*locks-held* p)))
682
683(defmethod process-locks-pending ((p process))
684  #+lock-accounting
685  (copy-list (symbol-value-in-process '*locks-pending* p)))
Note: See TracBrowser for help on using the repository browser.