source: trunk/source/level-1/l1-processes.lisp @ 9879

Last change on this file since 9879 was 9879, checked in by gz, 11 years ago

Propagate r9620 to trunk from working-0711

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