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

Last change on this file since 10393 was 10393, checked in by gb, 11 years ago

PROCESS-SUSPEND, PROCESS-RESUME: don't pass NIL to %SUSPEND-TCR, %RESUME-TCR.

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