commander-s/scheme/job.scm

381 lines
10 KiB
Scheme
Raw Normal View History

2005-06-03 07:44:53 -04:00
(define-record-type job :job
(really-make-job name console
proc status
start-time end-time run-status)
2005-06-03 07:44:53 -04:00
job?
(name job-name)
(console job-console)
2005-06-03 07:44:53 -04:00
(proc job-proc)
2005-06-14 07:20:30 -04:00
(status really-job-status set-job-status!)
(start-time job-start-time)
(end-time job-end-time set-job-end-time!)
(run-status job-run-status set-job-run-status!))
2005-06-03 07:44:53 -04:00
2005-06-04 05:42:45 -04:00
(define-record-discloser :job
(lambda (r)
`(job ,(job-name r) ,(job-run-status r))))
2005-06-04 05:42:45 -04:00
2005-06-14 07:20:30 -04:00
(define (make-job-with-console name proc pty-in pty-out terminal-buffer)
(let ((job (really-make-job
name
(make-console pty-in pty-out
(app-window-curses-win (result-window))
terminal-buffer)
proc (make-placeholder)
(date) #f 'running)))
2005-06-03 07:44:53 -04:00
(spawn-job-status-surveillant job)
(add-job! job)
job))
(define (make-job-sans-console name proc)
(let ((job (really-make-job
name #f proc (make-placeholder)
2005-06-14 07:20:30 -04:00
(date) #f 'running)))
(spawn-job-status-surveillant job)
(add-job! job)
job))
2005-06-14 07:20:30 -04:00
(define (job-with-console? v)
(and (job? v) (job-console v)))
(define (job-sans-console? v)
(not (job-with-console? v)))
2005-06-03 07:44:53 -04:00
(define (job-status-rv job)
(placeholder-value-rv (really-job-status job)))
(define (job-status job)
(sync (job-status-rv job)))
(define (spawn-job-status-surveillant job)
2005-06-14 09:44:09 -04:00
(spawn
(lambda ()
(let ((status (wait (job-proc job) wait/stopped-children)))
2005-06-14 09:44:09 -04:00
(cond
((status:exit-val status)
=> (lambda (i)
(debug-message "spawn-job-status-surveillant exit-val " i)
2005-06-14 09:44:09 -04:00
(set-job-run-status! job 'ready)
(set-job-end-time! job (date))))
((status:stop-sig status)
=> (lambda (signal)
(debug-message "spawn-job-status-surveillant stop-sig " signal)
2005-06-14 09:44:09 -04:00
(cond
((= signal signal/ttin)
(set-job-run-status! job 'waiting-for-input))
((= signal signal/ttou)
(set-job-run-status! job 'new-output))
(else
(set-job-run-status! job 'stopped)))))
((status:term-sig status)
=> (lambda (i)
(debug-message "spawn-job-status-surveillant term-sig " i)
2005-06-14 09:44:09 -04:00
(set-job-run-status! job 'stopped))))
(placeholder-set!
(really-job-status job) status)))))
(define (job-running? job)
(eq? (job-run-status job) 'running))
(define (job-ready? job)
(eq? (job-run-status job) 'ready))
(define (job-waiting-for-input? job)
(eq? (job-run-status job) 'waiting-for-input))
(define (job-has-new-output? job)
(eq? (job-run-status job) 'new-output))
2005-06-03 07:44:53 -04:00
2005-06-14 07:20:30 -04:00
(define (job-stopped? job)
(eq? (job-run-status job) 'stopped))
2005-06-03 07:44:53 -04:00
(define (signal-job signal job)
(if (job-ready? job)
(debug-message "refusing to send signal to ready job")
(signal-process-group (job-proc job) signal)))
2005-06-03 07:44:53 -04:00
(define (stop-job job)
(signal-job signal/stop job))
2005-06-03 07:44:53 -04:00
(define (continue-job job)
2005-06-14 07:20:30 -04:00
(set-job-status! job (make-placeholder))
(set-job-run-status! job 'running)
(signal-process-group
(proc:pid (job-proc job)) signal/cont)
2005-06-14 09:44:09 -04:00
(spawn-job-status-surveillant job)
(send notify-continue-channel job))
2005-06-03 07:44:53 -04:00
(define (pause-job-output job)
(pause-console-output (job-console job)))
(define (resume-job-output job)
(resume-console-output (job-console job)))
2005-06-14 07:20:30 -04:00
(define (continue-job-in-foreground job)
(if (and (job-sans-console? job)
(not (job-ready? job)))
2005-06-14 07:20:30 -04:00
(begin
(drain-tty (current-output-port))
(def-prog-mode)
(clear)
(refresh)
2005-06-14 07:20:30 -04:00
(endwin)
; (newline)
; (drain-tty (current-output-port))
2005-06-14 07:20:30 -04:00
(obtain-lock paint-lock)
(set-tty-process-group
(current-output-port) (proc:pid (job-proc job)))
(continue-job job)
(job-status job)
(set-tty-process-group (current-output-port) (pid))
(display "Press any key to return to Commander S...")
(wait-for-key)
(set-redisplay-everything)
2005-06-14 07:20:30 -04:00
(release-lock paint-lock))))
(define (continue-job-in-background job)
(if (not (job-ready? job))
(continue-job job)))
2005-06-03 07:44:53 -04:00
;; channels for communicating with the joblist surveillant
(define add-job-channel
(make-channel))
(define get-job-list-channel
(make-channel))
(define clear-ready-jobs-channel
(make-channel))
(define notify-continue-channel
2005-06-14 09:44:09 -04:00
(make-channel))
2005-06-03 07:44:53 -04:00
(define (add-job! job)
(send add-job-channel job))
(define (running-jobs)
(let ((answer-channel (make-channel)))
(send get-job-list-channel (cons 'running answer-channel))
(receive answer-channel)))
(define (ready-jobs)
(let ((answer-channel (make-channel)))
(send get-job-list-channel (cons 'ready answer-channel))
(receive answer-channel)))
(define (stopped-jobs)
(let ((answer-channel (make-channel)))
(send get-job-list-channel (cons 'stopped answer-channel))
(receive answer-channel)))
(define (clear-ready-jobs!)
(send clear-ready-jobs-channel 'ignored))
2005-06-03 07:44:53 -04:00
(define (jobs-with-new-output)
(let ((answer-channel (make-channel)))
(send get-job-list-channel (cons 'new-output answer-channel))
(receive answer-channel)))
(define (jobs-waiting-for-input)
(let ((answer-channel (make-channel)))
(send get-job-list-channel (cons 'waiting-for-input answer-channel))
(receive answer-channel)))
(define (spawn-joblist-surveillant)
(let ((statistics-channel (make-channel)))
(spawn
(lambda ()
(let lp ((running '())
(ready '())
2005-06-14 07:20:30 -04:00
(stopped '())
2005-06-03 07:44:53 -04:00
(new-output '())
(waiting-for-input '())
(notify? #f))
2005-06-14 07:20:30 -04:00
(debug-message "spawn-joblist-surveillant "
running " " ready " " stopped " "
new-output " " waiting-for-input " " notify?)
2005-06-03 07:44:53 -04:00
(cond
(notify?
(send statistics-channel
(list (cons 'running (length running))
(cons 'ready (length ready))
2005-06-14 07:20:30 -04:00
(cons 'stopped (length stopped))
2005-06-03 07:44:53 -04:00
(cons 'new-output (length new-output))
(cons 'waiting-for-input (length waiting-for-input))))
2005-06-14 07:20:30 -04:00
(lp running ready stopped new-output waiting-for-input #f))
2005-06-03 07:44:53 -04:00
(else
(apply
select
(append
(list
(wrap (receive-rv add-job-channel)
(lambda (new-job)
2005-06-14 07:20:30 -04:00
(lp (cons new-job running)
ready stopped new-output waiting-for-input #t)))
(wrap (receive-rv notify-continue-channel)
2005-06-14 09:44:09 -04:00
(lambda (job)
(lp (cons job running)
ready
(delete job stopped)
(delete job new-output)
(delete job waiting-for-input) #t)))
(wrap (receive-rv clear-ready-jobs-channel)
(lambda (ignore)
2005-06-14 07:20:30 -04:00
(lp running '() stopped new-output waiting-for-input #t)))
2005-06-03 07:44:53 -04:00
(wrap (receive-rv get-job-list-channel)
(lambda (state.channel)
(send (cdr state.channel)
(case (car state.channel)
((running) running)
((ready) ready)
2005-06-14 07:20:30 -04:00
((stopped) stopped)
2005-06-03 07:44:53 -04:00
((new-output) new-output)
((waiting-for-input) waiting-for-input)
(else
(error "joblist-surveillant" state.channel))))
2005-06-14 07:20:30 -04:00
(lp running ready stopped new-output waiting-for-input #f))))
2005-06-03 07:44:53 -04:00
(map (lambda (job)
(wrap (job-status-rv job)
2005-06-14 07:20:30 -04:00
(lambda (status)
(cond
((status:exit-val status)
=> (lambda (ignore)
(lp (delete job running)
(cons job ready) stopped
new-output waiting-for-input #t)))
((status:stop-sig status)
=> (lambda (signal)
(cond
((= signal signal/ttin)
(lp (delete job running)
ready stopped new-output
(cons job waiting-for-input) #t))
((= signal signal/ttou)
(lp (delete job running)
ready stopped
(cons job new-output)
waiting-for-input #t))
((or (= signal signal/tstp)
(= signal signal/stop))
;; TODO catch any other signal here
2005-06-15 07:42:29 -04:00
(stop-job job)
(lp (delete job running)
ready (cons job stopped)
new-output waiting-for-input #t))
2005-06-14 07:20:30 -04:00
(else
(error "Unhandled signal" signal)))))
((status:term-sig status)
=> (lambda (signal)
(lp (delete job running)
ready (cons job stopped)
new-output waiting-for-input #t)))))))
2005-06-03 07:44:53 -04:00
running))))))))
statistics-channel))
(define (initial-job-statistics)
(list (cons 'running 0)
(cons 'ready 0)
(cons 'new-output 0)
(cons 'waiting-for-input 0)))
;; #### unfinished
(define (install-terminal/stop-handler)
(set-interrupt-handler
interrupt/tstp
(lambda args
(display args))))
2005-06-14 07:20:30 -04:00
(define (save-tty-excursion port thunk)
(let ((settings (tty-info port)))
(let ((val (thunk)))
(set-tty-info/now port settings)
val)))
2005-06-14 07:20:30 -04:00
;; run a job by running the program form
;; for use in command mode (used by command-line-compiler)
(define (run/console* s-expr)
(call-with-values
(lambda ()
(fork-pty-session
(lambda ()
(handle-signal-default signal/ttou)
(eval-s-expr/command s-expr))))
(lambda (proc pty-in pty-out tty-name)
(make-job-with-console
s-expr proc pty-in pty-out
(make-terminal-buffer
(result-buffer-num-cols (result-buffer))
(result-buffer-num-lines (result-buffer)))))))
;; for use in Scheme mode
(define-syntax run/console
2005-06-03 07:44:53 -04:00
(syntax-rules ()
((_ epf)
(run/console* '(exec-epf epf)))))
;; for use in command mode (used by command-line-compiler)
(define (run/fg* s-expr)
(debug-message "run/fg* " s-expr)
(save-tty-excursion
(current-input-port)
(lambda ()
(obtain-lock paint-lock)
(def-prog-mode)
(clear)
(refresh)
(endwin)
(restore-initial-tty-info! (current-input-port))
(drain-tty (current-output-port))
(let ((foreground-pgrp (tty-process-group (current-output-port)))
(proc
(fork
(lambda ()
(let ((child-pid (pid)))
(set-process-group child-pid)
(set-tty-process-group (current-output-port) child-pid)
(handle-signal-default signal/ttou)
(eval-s-expr/command s-expr))))))
(let* ((job (make-job-sans-console s-expr proc))
(status (job-status job)))
(set-tty-process-group (current-output-port) foreground-pgrp)
(newline)
(display "Press any key to return to Commander S...")
(wait-for-key)
(release-lock paint-lock)
job)))))
2005-06-14 07:20:30 -04:00
(define-syntax run/fg
2005-06-14 07:20:30 -04:00
(syntax-rules ()
((_ epf)
(run/fg* '(exec-epf epf)))))
;; for use in command mode (used by command-line-compiler)
(define (run/bg* s-expr)
(obtain-lock paint-lock)
(drain-tty (current-output-port))
(set-tty-process-group (current-output-port) (pid))
(let ((proc
(fork
(lambda ()
(set-process-group (pid) (pid))
(handle-signal-default signal/ttou)
(eval-s-expr/command s-expr)))))
(let ((job (make-job-sans-console s-expr proc)))
(release-lock paint-lock)
job)))
;; for use in Scheme mode
(define-syntax run/bg
2005-06-14 07:20:30 -04:00
(syntax-rules ()
((_ epf)
(run/bg* '(exec-epf epf)))))
;;; EOF