1999-11-02 17:34:09 -05:00
|
|
|
;;; Unix waitt & process objects for scsh
|
1999-09-14 09:32:05 -04:00
|
|
|
;;; Copyright (c) 1993, 1994, 1995 by Olin Shivers.
|
|
|
|
|
|
|
|
;;; This is a GC'd abstraction for Unix process id's.
|
1999-09-23 10:36:25 -04:00
|
|
|
; ;; The problem with Unix pids is (a) they clutter up the kernel
|
1999-09-14 09:32:05 -04:00
|
|
|
;;; process table until you wait(2) them, and (b) you can only
|
|
|
|
;;; wait(2) them once. Scsh's process objects are similar, but
|
|
|
|
;;; allow the storage to be allocated in the scsh address space,
|
|
|
|
;;; and out of the kernel process table, and they can be waited on
|
|
|
|
;;; multiple times.
|
|
|
|
|
|
|
|
;;; Process objects
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(define-record proc ; A process object
|
|
|
|
pid ; Proc's pid.
|
|
|
|
(finished? #f) ; Running, stopped, done
|
|
|
|
(status (make-placeholder)) ; The cached exit status of the process
|
|
|
|
(zombie #t) ; Misnomer. Whether or not the process has
|
|
|
|
; (not) been waited on.
|
|
|
|
;; Make proc objects print like #{proc 2318}.
|
1999-10-18 19:32:01 -04:00
|
|
|
((disclose p) (list "proc" (proc:pid p) (proc:finished? p))))
|
|
|
|
|
|
|
|
;; Unfortunately there is no way to specify the name of the constructor-
|
|
|
|
;; function in Olins define-record macro, so I had to do this...
|
|
|
|
(define (make-procobj pid)
|
|
|
|
(let ((procobj (make-proc pid)))
|
|
|
|
(add-finalizer! procobj procobj-finalizer)
|
|
|
|
procobj))
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
;; Weak pointer tables. Much more efficient than populations.
|
|
|
|
;; Maps pids to processes. Unexited processes are strong pointers, exited
|
|
|
|
;; procs are weak pointers (to allow gc'ing).
|
1999-10-18 19:32:01 -04:00
|
|
|
;;
|
1999-11-02 17:34:09 -05:00
|
|
|
;; JMG: why ever unexited processes were strong pointer, this won't work
|
1999-10-18 19:32:01 -04:00
|
|
|
;; with (autoreap-policy 'late), since then gc waits for the strong pointer
|
|
|
|
;; until it wait(2)s and the strong pointer waits for wait(2) which is
|
|
|
|
;; nothing but a deadlock
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2001-12-05 09:45:35 -05:00
|
|
|
(define process-table (make-integer-table))
|
|
|
|
(make-reinitializer (lambda ()
|
|
|
|
(set! process-table (make-integer-table))))
|
2001-04-09 04:05:58 -04:00
|
|
|
|
2002-02-13 09:56:11 -05:00
|
|
|
(define process-table-lock (make-lock))
|
2001-04-09 04:05:58 -04:00
|
|
|
(define (process-table-ref n)
|
2002-02-13 09:56:11 -05:00
|
|
|
(with-lock process-table-lock
|
|
|
|
(lambda ()
|
|
|
|
(weak-table-ref process-table n))))
|
2001-04-09 04:05:58 -04:00
|
|
|
|
|
|
|
(define (process-table-set! n val)
|
2002-02-13 09:56:11 -05:00
|
|
|
(with-lock process-table-lock
|
|
|
|
(lambda ()
|
|
|
|
(weak-table-set! process-table n val))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2002-02-14 08:32:19 -05:00
|
|
|
(define (process-table-delete-procobj! procobj)
|
|
|
|
(with-lock process-table-lock
|
|
|
|
(lambda ()
|
|
|
|
(if (eq? (weak-table-ref process-table (proc:pid procobj))
|
|
|
|
procobj)
|
|
|
|
(weak-table-set! process-table (proc:pid procobj) #f)))))
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
(define (maybe-pid->proc pid)
|
2001-04-09 04:05:58 -04:00
|
|
|
(process-table-ref pid))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
(define (pid->proc pid . maybe-probe?)
|
|
|
|
(let ((probe? (:optional maybe-probe? #f)))
|
|
|
|
(or (maybe-pid->proc pid)
|
|
|
|
(case probe?
|
|
|
|
((#f) (error "Pid has no corresponding process object" pid))
|
2001-04-09 04:05:58 -04:00
|
|
|
((create) (new-child-proc pid))
|
1999-09-14 09:32:05 -04:00
|
|
|
(else #f)))))
|
|
|
|
|
|
|
|
;;; Coerce pids and procs to procs.
|
|
|
|
|
|
|
|
(define (->proc proc/pid)
|
|
|
|
(cond ((proc? proc/pid) proc/pid)
|
|
|
|
((and (integer? proc/pid) (>= proc/pid 0))
|
2001-12-05 09:45:35 -05:00
|
|
|
(pid->proc proc/pid 'create))
|
1999-09-14 09:32:05 -04:00
|
|
|
(else (error "Illegal parameter" ->proc proc/pid))))
|
|
|
|
|
|
|
|
|
|
|
|
;;; Is X a pid or a proc?
|
|
|
|
|
1999-09-23 10:36:25 -04:00
|
|
|
(define (pid/proc? x) (or (proc? x) (and (integer? x) (>= x 0))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;; Process reaping
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; "Reaping" a process means using wait(2) to move its exit status from the
|
|
|
|
;;; kernel's process table into scsh, thus cleaning up the kernel's process
|
|
|
|
;;; table and saving the value in a gc'd data structure, where it can be
|
|
|
|
;;; referenced multiple times.
|
|
|
|
;;;
|
|
|
|
;;; - Stopped processes are never reaped, only dead ones. (May change -df)
|
|
|
|
;;;
|
|
|
|
;;; - Stopped process status codes are never cached in proc objects,
|
|
|
|
;;; only status codes for dead processes. So you can wait for a
|
|
|
|
;;; dead process multiple times, but only once per process-stop.
|
|
|
|
;;; (May change -df)
|
|
|
|
;;;
|
|
|
|
;;; - Unfortunately, reaping a process loses the information specifying its
|
|
|
|
;;; process group, so if a process is reaped into scsh, it cannot be
|
|
|
|
;;; waited for by WAIT-PROCESS-GROUP. Notice that only dead processes are
|
|
|
|
;;; reaped, not suspended ones. Programs almost never use WAIT-PROCESS-GROUP
|
|
|
|
;;; to wait for dead processes, so this is not likely to be a problem. If
|
|
|
|
;;; it is, turn autoreaping off with (autoreap-policy #f).
|
|
|
|
;;; (This never worked right, and it might be wiped out completely -fd)
|
|
|
|
;;;
|
|
|
|
;;; - Reaping can be encouraged by calling (REAP-ZOMBIES).
|
|
|
|
|
|
|
|
;;; (autoreap-policy [new-policy])
|
|
|
|
;;; Watch this area
|
|
|
|
|
2000-12-21 16:57:37 -05:00
|
|
|
|
|
|
|
;;; I'm really tired of opening everything (i.e. events) in scsh-level-0
|
|
|
|
;;; this is here until someone (Olin !!!) cleans up the scsh modules
|
|
|
|
|
2001-10-18 05:02:52 -04:00
|
|
|
(define next-sigevent (structure-ref sigevents next-sigevent))
|
|
|
|
(define most-recent-sigevent (structure-ref sigevents most-recent-sigevent))
|
2000-12-21 16:57:37 -05:00
|
|
|
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
(define *autoreap-policy* #f) ; Not exported from this module.
|
|
|
|
|
|
|
|
(define (autoreap-policy . maybe-policy)
|
|
|
|
(let ((old-policy *autoreap-policy*))
|
|
|
|
(if (pair? maybe-policy)
|
|
|
|
(let ((new-policy (car maybe-policy)))
|
|
|
|
(cond ((pair? (cdr maybe-policy))
|
|
|
|
(error "Too many args to autoreap-policy" maybe-policy))
|
1999-10-18 19:32:01 -04:00
|
|
|
((not (memq new-policy '(early late #f)))
|
1999-09-14 09:32:05 -04:00
|
|
|
(error "Illegal autoreap policy." new-policy))
|
|
|
|
(else (set! *autoreap-policy* new-policy)
|
1999-10-18 19:32:01 -04:00
|
|
|
(cond ((eq? new-policy 'early)
|
2000-12-21 16:57:37 -05:00
|
|
|
(set-sigchld-handler! early-sigchld-handler)
|
|
|
|
(set-post/gc-handler! reap-need-reaping))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
((eq? new-policy 'late)
|
2000-12-21 16:57:37 -05:00
|
|
|
(set-sigchld-handler! late-sigchld-handler)
|
|
|
|
(set-post/gc-handler! reap-need-reaping))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
(else
|
2000-12-21 16:57:37 -05:00
|
|
|
(set-sigchld-handler! noauto-sigchld-handler)
|
|
|
|
(set-post/gc-handler!
|
|
|
|
(lambda ()
|
|
|
|
#f))))))))
|
1999-09-14 09:32:05 -04:00
|
|
|
old-policy))
|
|
|
|
|
|
|
|
|
2000-12-21 16:57:37 -05:00
|
|
|
;;; we don't register the post/gc-handler until the first police change
|
|
|
|
;;; --- this made sense, but why?
|
|
|
|
(define *post/gc-handler*
|
|
|
|
(lambda () (error "*post/gc-handler* was not defined")))
|
|
|
|
|
|
|
|
(define (really-set-post/gc-handler! handler)
|
|
|
|
(set! *post/gc-handler* handler))
|
|
|
|
|
|
|
|
(define (start-set-post/gc-handler! handler)
|
|
|
|
(set! set-post/gc-handler! really-set-post/gc-handler!)
|
|
|
|
(set-post/gc-handler! handler)
|
|
|
|
(spawn (lambda ()
|
2001-04-09 04:05:58 -04:00
|
|
|
(let lp ((event (most-recent-sigevent)))
|
|
|
|
(let ((next-event (next-sigevent event interrupt/post-gc)))
|
2000-12-21 16:57:37 -05:00
|
|
|
(*post/gc-handler*)
|
2002-02-13 09:56:11 -05:00
|
|
|
(lp next-event))))
|
|
|
|
'*post/gc-handler*-thread))
|
|
|
|
|
2000-12-21 16:57:37 -05:00
|
|
|
(define set-post/gc-handler! start-set-post/gc-handler!)
|
|
|
|
|
|
|
|
|
|
|
|
(define (*sigchld-handler*) (early-sigchld-handler))
|
|
|
|
(define (set-sigchld-handler! handler)
|
|
|
|
(set! *sigchld-handler* handler))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
2001-04-09 04:05:58 -04:00
|
|
|
(define (with-autoreaping thunk)
|
2000-12-21 16:57:37 -05:00
|
|
|
(set! *autoreap-policy* 'early)
|
2001-10-03 10:41:01 -04:00
|
|
|
(run-as-long-as
|
|
|
|
(lambda ()
|
|
|
|
(let lp ((event (most-recent-sigevent)))
|
|
|
|
(let ((next-event (next-sigevent event interrupt/chld)))
|
|
|
|
(*sigchld-handler*)
|
|
|
|
(lp next-event))))
|
2001-11-08 03:14:37 -05:00
|
|
|
thunk
|
|
|
|
'auto-reaping))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
;;; This list contains pids whose proc-obj were gc'd before they died
|
|
|
|
;;; We try to reap them after every gc and maybe on every SIGCHLD
|
|
|
|
(define need-reaping '())
|
|
|
|
|
1999-11-02 17:34:09 -05:00
|
|
|
(define need-reaping-lock (make-lock))
|
|
|
|
|
1999-10-18 19:32:01 -04:00
|
|
|
(define (need-reaping-add! pid)
|
1999-11-02 17:34:09 -05:00
|
|
|
(obtain-lock need-reaping-lock)
|
|
|
|
(set! need-reaping (cons pid need-reaping))
|
|
|
|
(release-lock need-reaping-lock))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
(define (need-reaping-remove! pid)
|
1999-11-02 17:34:09 -05:00
|
|
|
(obtain-lock need-reaping-lock)
|
|
|
|
(set! need-reaping (del pid need-reaping))
|
|
|
|
(release-lock need-reaping-lock))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
(define (reap-need-reaping)
|
1999-11-02 17:34:09 -05:00
|
|
|
(obtain-lock need-reaping-lock)
|
|
|
|
(set! need-reaping (filter (lambda (pid) (not (reap-pid pid))) need-reaping))
|
|
|
|
(release-lock need-reaping-lock))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
;;; reap this special pid
|
|
|
|
;;; return status or #f
|
|
|
|
(define (reap-pid pid)
|
2002-02-13 09:56:11 -05:00
|
|
|
(with-lock
|
|
|
|
wait-lock
|
|
|
|
(lambda ()
|
|
|
|
(let ((status (atomic-wait pid wait/poll)))
|
|
|
|
(if status
|
|
|
|
(waited-by-reap pid status))
|
|
|
|
status))))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
;;; Handler for SIGCHLD according policy
|
2000-12-21 16:57:37 -05:00
|
|
|
(define (late-sigchld-handler) #f)
|
1999-10-18 19:32:01 -04:00
|
|
|
|
2000-12-21 16:57:37 -05:00
|
|
|
(define (early-sigchld-handler)
|
1999-10-18 19:32:01 -04:00
|
|
|
(reap-zombies))
|
|
|
|
|
2000-12-21 16:57:37 -05:00
|
|
|
(define (noauto-sigchld-handler) #f)
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;; Finalizer for procobjs
|
|
|
|
;;;
|
|
|
|
(define (procobj-finalizer procobj)
|
2002-02-14 08:32:19 -05:00
|
|
|
(process-table-delete-procobj! procobj)
|
1999-10-18 19:32:01 -04:00
|
|
|
(if (not (proc:finished? procobj))
|
1999-11-02 17:34:09 -05:00
|
|
|
(need-reaping-add! (proc:pid procobj))))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
;;; (reap-zombies) => bool
|
|
|
|
;;; Move any zombies from the kernel process table into scsh.
|
|
|
|
;;; Return true if no more outstanding children; #f if some still live.
|
|
|
|
|
|
|
|
(define (reap-zombies)
|
|
|
|
(let lp ()
|
2002-02-13 09:56:11 -05:00
|
|
|
(obtain-lock wait-lock)
|
|
|
|
(receive (pid status)
|
2001-12-05 09:45:35 -05:00
|
|
|
(%wait-any (bitwise-ior wait/poll wait/stopped-children))
|
1999-09-14 09:32:05 -04:00
|
|
|
(if pid
|
1999-11-02 17:34:09 -05:00
|
|
|
(begin (waited-by-reap pid status)
|
2002-02-13 09:56:11 -05:00
|
|
|
(release-lock wait-lock)
|
2000-09-12 13:43:48 -04:00
|
|
|
; (format (current-error-port)
|
|
|
|
; "Reaping ~d[~d]~%" pid status)
|
1999-09-14 09:32:05 -04:00
|
|
|
(lp))
|
2002-02-13 09:56:11 -05:00
|
|
|
(begin
|
|
|
|
(release-lock wait-lock)
|
|
|
|
status)))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
(define (new-child-proc pid)
|
1999-10-18 19:32:01 -04:00
|
|
|
(let ((proc (make-procobj pid)))
|
2001-04-09 04:05:58 -04:00
|
|
|
(process-table-set! pid proc)
|
1999-09-14 09:32:05 -04:00
|
|
|
proc))
|
|
|
|
|
|
|
|
;;; (WAIT proc/pid [flags])
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;;; (wait proc/pid [flags]) => status or #f
|
|
|
|
;;;
|
|
|
|
;;; FLAGS (default 0) is the exclusive or of the following:
|
|
|
|
;;; wait/poll
|
|
|
|
;;; Return #f immediately if there are no
|
|
|
|
;;; unwaited children available.
|
|
|
|
;;; wait/stopped-children
|
|
|
|
;;; Report on suspended children as well.
|
|
|
|
;;;
|
|
|
|
;;; If the process hasn't terminated (or suspended, if wait/stopped
|
|
|
|
;;; is set) and wait/poll is set, return #f.
|
|
|
|
;;; (I'm working on the flags -df)
|
|
|
|
|
1999-10-18 19:32:01 -04:00
|
|
|
;;; JMG: We have to be careful about wait/poll and autoreap-policy:
|
2001-04-09 04:05:58 -04:00
|
|
|
;;; If it was 'late at anytime, we may missed the exit of pid/proc
|
1999-10-18 19:32:01 -04:00
|
|
|
;;; So we cannot just block and hope reap-zombies will give us the status
|
|
|
|
|
|
|
|
|
2001-04-09 04:05:58 -04:00
|
|
|
;;; With this lock, we ensure that only one thread may call
|
2002-02-13 09:56:11 -05:00
|
|
|
;;; really-wait for a given pid and manipulates the associated process object
|
2001-04-09 04:05:58 -04:00
|
|
|
|
|
|
|
(define wait-lock (make-lock))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
(define (wait pid/proc . maybe-flags)
|
2001-12-05 09:45:35 -05:00
|
|
|
(let* ((flags (:optional maybe-flags 0))
|
|
|
|
(proc (->proc pid/proc))
|
|
|
|
(win (lambda (status)
|
|
|
|
(waited-by-wait proc status)
|
|
|
|
status)))
|
|
|
|
;; save the event before we check for finished
|
|
|
|
(let ((pre-event (most-recent-sigevent)))
|
2002-02-13 09:56:11 -05:00
|
|
|
(with-lock
|
|
|
|
wait-lock
|
|
|
|
(lambda ()
|
|
|
|
(cond ((atomic-wait proc (bitwise-ior flags wait/poll)) => win)
|
|
|
|
|
|
|
|
((zero? (bitwise-and flags wait/poll))
|
|
|
|
;; we have to block and hence use the event system
|
|
|
|
(let lp ((pre-event pre-event))
|
|
|
|
(cond ((atomic-wait proc (bitwise-ior flags wait/poll))
|
|
|
|
=> win)
|
|
|
|
(else
|
|
|
|
(release-lock wait-lock)
|
|
|
|
(let ((next-event (next-sigevent pre-event interrupt/chld)))
|
|
|
|
(obtain-lock wait-lock)
|
|
|
|
(lp next-event))))))
|
|
|
|
(else #f)))))))
|
2001-12-05 09:45:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
;;; -> process-object proc status/#f
|
|
|
|
(define (atomic-wait proc flags)
|
2002-02-13 09:56:11 -05:00
|
|
|
(cond ((proc:finished? proc)
|
|
|
|
(placeholder-value (proc:status proc)))
|
|
|
|
(else (really-wait (proc:pid proc) (bitwise-ior flags wait/poll)))))
|
2001-12-05 09:45:35 -05:00
|
|
|
|
1999-10-18 19:32:01 -04:00
|
|
|
;;; This one is used, to wait on a positive pid
|
|
|
|
;;; We NEVER do a blocking wait syscall
|
|
|
|
(define (really-wait pid flags)
|
|
|
|
(if (zero? (bitwise-and flags wait/poll))
|
|
|
|
(error "really-wait without wait/poll"))
|
|
|
|
(if (< pid 1)
|
|
|
|
(error "really-wait on nonpos pid" pid))
|
2001-10-18 04:51:32 -04:00
|
|
|
(receive (return_pid status)
|
|
|
|
(%wait-pid pid flags)
|
|
|
|
(cond ((zero? return_pid) #f) ; failed wait/poll
|
|
|
|
((= pid return_pid) status) ; made it
|
|
|
|
(else (error "mismatch in really-wait"
|
|
|
|
return_pid pid)))))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; All you have to do, if pid was reaped
|
|
|
|
;;; proc_obj is maybe no longer alive
|
1999-11-02 17:34:09 -05:00
|
|
|
(define (waited-by-reap pid status)
|
1999-10-18 19:32:01 -04:00
|
|
|
(cond ((maybe-pid->proc pid) =>
|
|
|
|
(lambda (proc)
|
|
|
|
(obituary proc status)
|
2002-02-13 09:56:11 -05:00
|
|
|
(push-reaped-proc proc)
|
|
|
|
))))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;; All you have to do, if a wait on proc was successful
|
1999-11-02 17:34:09 -05:00
|
|
|
(define (waited-by-wait proc status)
|
1999-10-18 19:32:01 -04:00
|
|
|
(obituary proc status)
|
|
|
|
(mark-proc-waited! proc))
|
|
|
|
|
|
|
|
;;; we know from somewhere that proc is dead
|
|
|
|
(define (obituary proc status)
|
|
|
|
(if (not (proc? proc))
|
1999-11-02 17:34:09 -05:00
|
|
|
(error "obituary: proc was not a procobj" proc))
|
1999-10-18 19:32:01 -04:00
|
|
|
(need-reaping-remove! (proc:pid proc)) ; in case it started during 'late
|
|
|
|
(placeholder-set! (proc:status proc) status)
|
|
|
|
(set-proc:finished? proc #t))
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;; (wait-any [flags]) => [proc status]
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; [#f #f] => non-blocking, none ready.
|
|
|
|
;;; [#f #t] => no more.
|
|
|
|
|
|
|
|
(define (wait-any . maybe-flags)
|
|
|
|
(let ((flags (:optional maybe-flags 0)))
|
|
|
|
(if (zero? (bitwise-and flags wait/poll))
|
1999-10-18 19:32:01 -04:00
|
|
|
(begin
|
|
|
|
(receive (pid status)
|
2001-12-05 09:45:35 -05:00
|
|
|
;; before we maybe block via placeholder-value
|
|
|
|
;; do a really-wait-any for the ones, missed by 'late
|
|
|
|
(really-wait-any (bitwise-ior flags wait/poll))
|
|
|
|
(if (not pid)
|
|
|
|
(let ((win (get-reaped-proc!)))
|
|
|
|
(values win (placeholder-value (proc:status win))))
|
|
|
|
(values pid status))))
|
|
|
|
|
|
|
|
;; The rest of this is quite crude and can be safely ignored. -df
|
|
|
|
;; JMG: wait-any is crude and so its implementation
|
|
|
|
;; It got even worse, now that we have this fu*$#%g 'late
|
1999-09-14 09:32:05 -04:00
|
|
|
(if (maybe-obtain-lock reaped-proc-pop-lock)
|
|
|
|
(if (eq? reaped-proc-head reaped-proc-tail)
|
1999-10-18 19:32:01 -04:00
|
|
|
;;; due to 'late we cannot be sure, that they all have been
|
|
|
|
;;; reaped
|
1999-09-14 09:32:05 -04:00
|
|
|
(begin
|
|
|
|
(release-lock reaped-proc-pop-lock)
|
1999-10-18 19:32:01 -04:00
|
|
|
(really-wait-any flags))
|
1999-09-14 09:32:05 -04:00
|
|
|
(let* ((retnode (placeholder-value reaped-proc-head))
|
|
|
|
(retval (weak-pointer-ref (reaped-proc:proc retnode))))
|
|
|
|
(set! reaped-proc-head (reaped-proc:next retnode))
|
|
|
|
(release-lock reaped-proc-pop-lock)
|
|
|
|
(if retval
|
|
|
|
(values retval (placeholder-value (proc:status retval)))
|
1999-10-18 19:32:01 -04:00
|
|
|
(values #f #f))))
|
|
|
|
(values #f #f)))))
|
|
|
|
|
|
|
|
(define (really-wait-any flags)
|
|
|
|
(if (zero? (bitwise-and flags wait/poll))
|
|
|
|
(error "real-wait-any without wait/poll" flags))
|
2002-02-13 09:56:11 -05:00
|
|
|
(with-lock
|
|
|
|
wait-lock
|
|
|
|
(lambda ()
|
|
|
|
(receive (pid status)
|
|
|
|
(%wait-any flags)
|
|
|
|
(if pid
|
|
|
|
(let ((proc (new-child-proc pid)))
|
|
|
|
(waited-by-wait proc status)
|
|
|
|
(values proc status))
|
|
|
|
(values #f #f))))))
|
1999-10-18 19:32:01 -04:00
|
|
|
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
;;; (wait-process-group [proc-group flags]) => [proc status]
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; [#f #f] => non-blocking, none ready.
|
|
|
|
;;; [#f #t] => no more.
|
|
|
|
;;;
|
|
|
|
;;;
|
|
|
|
;;; If you are doing process-group waits, you do *not* want to use
|
|
|
|
;;; early autoreaping, since the reaper loses process-group information.
|
|
|
|
;;; (I'm working on it -df)
|
|
|
|
|
|
|
|
(define (wait-process-group . args)
|
|
|
|
(let-optionals args ((proc-group 0) (flags 0))
|
|
|
|
(let ((proc-group (cond ((integer? proc-group) proc-group)
|
|
|
|
((proc? proc-group) (proc:pid proc-group))
|
|
|
|
(else (error "Illegal argument" wait-process-group
|
2001-12-05 09:45:35 -05:00
|
|
|
proc-group))))
|
|
|
|
(win (lambda (pid status)
|
|
|
|
(let ((proc (pid->proc pid 'create)))
|
|
|
|
(if proc (waited-by-wait proc status))
|
|
|
|
(values proc status)))))
|
|
|
|
;; save the event before we check for finished
|
|
|
|
(let ((pre-event (most-recent-sigevent)))
|
|
|
|
(receive (pid status)
|
|
|
|
(%wait-process-group proc-group (bitwise-ior flags wait/poll))
|
|
|
|
(cond (pid
|
|
|
|
(win pid status))
|
|
|
|
((zero? (bitwise-and flags wait/poll))
|
|
|
|
;; we have to block and hence use the event system
|
|
|
|
(let lp ((pre-event pre-event))
|
|
|
|
(receive (pid status)
|
|
|
|
(%wait-process-group proc-group (bitwise-ior flags wait/poll))
|
|
|
|
(if pid
|
|
|
|
(win pid status)
|
|
|
|
(lp (next-sigevent pre-event interrupt/chld))))))
|
|
|
|
(else
|
|
|
|
(values #f status))))))))
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; (%wait-any flags) (%wait-pid pid flags) (%wait-process-group pgrp flags)
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2002-02-13 09:56:11 -05:00
|
|
|
;;; Direct interfaces to waitpid(2) call. As opposed to %wait-pid this
|
|
|
|
;;; waits on any child (using -1) and gets along if no child is alive
|
|
|
|
;;; at all (i.e. catches errno/child).
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
;;; [#f #f] means no processes ready on a non-blocking wait.
|
|
|
|
;;; [#f #t] means no waitable process on wait-any.
|
|
|
|
|
|
|
|
(define (%wait-any flags)
|
2001-10-18 04:51:32 -04:00
|
|
|
(with-errno-handler
|
|
|
|
((errno packet)
|
|
|
|
((errno/child)
|
|
|
|
(values #f #t)))
|
|
|
|
(receive (pid status)
|
|
|
|
(%wait-pid -1 flags)
|
|
|
|
(if (zero? pid)
|
|
|
|
(values #f #f) ; None ready.
|
|
|
|
(values pid status)))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
(define (%wait-process-group pgrp flags)
|
2001-12-05 09:45:35 -05:00
|
|
|
(if (zero? (bitwise-and flags wait/poll))
|
|
|
|
(error "really-wait without wait/poll"))
|
2001-10-18 04:51:32 -04:00
|
|
|
(with-errno-handler
|
|
|
|
((errno packet)
|
|
|
|
((errno/child)
|
|
|
|
(values #f #t)))
|
|
|
|
(receive (pid status)
|
|
|
|
(%wait-pid (- pgrp) flags)
|
|
|
|
(if (zero? pid)
|
|
|
|
(values #f #f) ; None ready.
|
|
|
|
(values pid status)))))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;; Reaped process table
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; We keep track of procs that have been reaped but not yet waited on by
|
|
|
|
;;; the user's code. These proces are eligible for return by WAIT-ANY.
|
|
|
|
;;; We keep track of these so that WAIT-ANY will hand them out exactly once.
|
|
|
|
;;; What this code needs is traditional condition variables.
|
|
|
|
|
|
|
|
;;; This is (so far) reliable in the following ways:
|
|
|
|
;;; 1. No process will be returned twice by wait-any, ever. Even two different
|
|
|
|
;;; wait-anys.
|
|
|
|
;;; 2. Being un-reaped will not prevent garbage collection.
|
|
|
|
;;; (actually, there seems to be a problem with this -df)
|
|
|
|
;;; 3. If a process is waited on, or is gc'ed, wait-any will do the Right
|
|
|
|
;;; Thing.
|
|
|
|
|
|
|
|
;;; And UNreliable in the following ways:
|
|
|
|
;;; 1. If a wait and a wait-any are blocking simultaneously, the wait will
|
|
|
|
;;; always return the object. However, whether the wait-any will or not
|
|
|
|
;;; is based on racing semaphores.
|
|
|
|
;;; 2. While processes can still be garbage collected, the nodes on the
|
|
|
|
;;; wait-any list will not, and if the program never wait-any's, the queue
|
|
|
|
;;; will snake around, eating up memory like pac-man with the munchies.
|
|
|
|
;;; 3. The process may be garbage collected before wait-any gets to it, and
|
|
|
|
;;; that's just tough.
|
|
|
|
|
|
|
|
;;; -df
|
|
|
|
|
|
|
|
(define-record reaped-proc
|
|
|
|
proc
|
|
|
|
(next (make-placeholder))
|
|
|
|
prev)
|
|
|
|
|
2002-02-07 05:11:20 -05:00
|
|
|
(define reaped-proc-tail (make-reaped-proc (make-weak-pointer #f) 'head))
|
1999-09-14 09:32:05 -04:00
|
|
|
(define reaped-proc-head reaped-proc-tail)
|
|
|
|
(define reaped-proc-push-lock (make-lock))
|
|
|
|
(define reaped-proc-pop-lock (make-lock)) ;;; Zippy sez: pop lock!
|
|
|
|
|
|
|
|
(define (push-reaped-proc proc)
|
|
|
|
(obtain-lock reaped-proc-push-lock)
|
|
|
|
(let ((push-me (make-reaped-proc (make-weak-pointer proc) reaped-proc-tail)))
|
2002-02-07 05:11:20 -05:00
|
|
|
(placeholder-set! (reaped-proc:next reaped-proc-tail) push-me)
|
|
|
|
(add-finalizer! proc (make-reaped-proc-finalizer push-me))
|
|
|
|
(set! reaped-proc-tail push-me))
|
1999-09-14 09:32:05 -04:00
|
|
|
(release-lock reaped-proc-push-lock))
|
|
|
|
|
2002-02-07 05:11:20 -05:00
|
|
|
(define (make-reaped-proc-finalizer push-me)
|
|
|
|
(lambda ignore
|
|
|
|
(remove-reaped-proc push-me)))
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
(define (remove-reaped-proc reaped-proc)
|
|
|
|
(spawn (lambda () ;This is blocking, so should run by itself
|
2002-02-07 05:11:20 -05:00
|
|
|
(set-reaped-proc:prev
|
|
|
|
(placeholder-value (reaped-proc:next reaped-proc))
|
|
|
|
(reaped-proc:prev reaped-proc))
|
|
|
|
(set-reaped-proc:next
|
|
|
|
(reaped-proc:prev reaped-proc)
|
|
|
|
(reaped-proc:next reaped-proc)))
|
|
|
|
"reaped-proc-removing-thread"))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
(define (pop-reaped-proc)
|
|
|
|
(obtain-lock reaped-proc-pop-lock) ;;; pop lock pop lock pop lock!
|
2002-02-07 05:11:20 -05:00
|
|
|
(let ((pop-me (placeholder-value (reaped-proc:next reaped-proc-head))))
|
|
|
|
(set! reaped-proc-head pop-me)
|
1999-09-14 09:32:05 -04:00
|
|
|
(release-lock reaped-proc-pop-lock)
|
|
|
|
(weak-pointer-ref (reaped-proc:proc pop-me))))
|
|
|
|
|
1999-10-18 19:32:01 -04:00
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
;;; Pop one off the list.
|
|
|
|
(define (get-reaped-proc!)
|
|
|
|
(let loop ((try (pop-reaped-proc)))
|
|
|
|
(if (and try (proc:zombie try))
|
|
|
|
try
|
|
|
|
(loop (pop-reaped-proc)))))
|
|
|
|
|
|
|
|
;;; PROC no longer eligible to be in the list. Delete it.
|
|
|
|
(define (mark-proc-waited! proc)
|
|
|
|
(set-proc:zombie proc #f))
|