1 ;; Copyright (C) 2007 Vesa Karvonen
3 ;; MLton is released under a BSD-style license.
4 ;; See the file MLton-LICENSE for details.
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 "The background job module allows emacs to perform time consuming
14 processing jobs in the background while allowing the user to continue
15 editing. See the documentation of the `bg-job-start' function for
18 (defcustom bg-job-period
0.10
19 "Timer period in seconds for background processing interrupts. Must
24 (defcustom bg-job-cpu-ratio
0.15
25 "Ratio of CPU time allowed for background processing. Must be positive
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;; Background Processor
33 (defun bg-job-start (done? step finalize
&rest args
)
34 "Starts a background job. The job is considered active as longs as
38 returns nil. While the job is active,
42 will be called periodically to perform a (supposedly small) computation
43 step. After the job becomes inactive,
47 will be called once and the job will be discarded.
49 A job may call `bg-job-start' to start new jobs and multiple background
50 jobs may be active simultaneously."
51 (let ((job (cons args
(cons done?
(cons step finalize
)))))
52 (push job bg-job-queue
))
55 (defun bg-job-done?
(job)
56 (apply (cadr job
) (car job
)))
58 (defun bg-job-step (job)
59 (apply (caddr job
) (car job
)))
61 (defun bg-job-finalize (job)
62 (apply (cdddr job
) (car job
)))
64 (defvar bg-job-queue nil
)
65 (defvar bg-job-timer nil
)
67 (defun bg-job-timer-start ()
71 bg-job-period bg-job-period
(function bg-job-quantum
)))))
73 (defun bg-job-timer-stop ()
75 (compat-delete-timer bg-job-timer
)
76 (setq bg-job-timer nil
)))
78 (defun bg-job-quantum ()
79 (let ((end-time (+ (bg-job-time-to-double (current-time))
80 (* bg-job-period bg-job-cpu-ratio
))))
81 (while (and (< (bg-job-time-to-double (current-time))
84 (let ((job (pop bg-job-queue
)))
85 (if (bg-job-done? job
)
88 (setq bg-job-queue
(nconc bg-job-queue
(list job
)))))))
92 (defun bg-job-time-to-double (time)
93 (+ (* (car time
) 65536.0)
95 (* (caddr time
) 1e-06)))
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;