| 1 |
;;; bg-job.el --- bg-job.el -*- lexical-binding: t; -*- |
| 2 |
;; Copyright (C) 2007 Vesa Karvonen |
| 3 |
;; |
| 4 |
;; MLton is released under a HPND-style license. |
| 5 |
;; See the file MLton-LICENSE for details. |
| 6 |
|
| 7 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 8 |
;; Customization |
| 9 |
|
| 10 |
(defgroup bg-job nil |
| 11 |
"The background job module allows emacs to perform time consuming |
| 12 |
processing jobs in the background while allowing the user to continue |
| 13 |
editing. See the documentation of the `bg-job-start' function for |
| 14 |
details.") |
| 15 |
|
| 16 |
(defcustom bg-job-period 0.10 |
| 17 |
"Timer period in seconds for background processing interrupts. Must |
| 18 |
be positive." |
| 19 |
:type 'number |
| 20 |
:group 'bg-job) |
| 21 |
|
| 22 |
(defcustom bg-job-cpu-ratio 0.15 |
| 23 |
"Ratio of CPU time allowed for background processing. Must be positive |
| 24 |
and less than 1." |
| 25 |
:type 'number |
| 26 |
:group 'bg-job) |
| 27 |
|
| 28 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 29 |
;; Background Processor |
| 30 |
|
| 31 |
(defun bg-job-start (done? step finalize &rest args) |
| 32 |
"Starts a background job. The job is considered active as longs as |
| 33 |
|
| 34 |
(apply done? args) |
| 35 |
|
| 36 |
returns nil. While the job is active, |
| 37 |
|
| 38 |
(apply step args) |
| 39 |
|
| 40 |
will be called periodically to perform a (supposedly small) computation |
| 41 |
step. After the job becomes inactive, |
| 42 |
|
| 43 |
(apply finalize args) |
| 44 |
|
| 45 |
will be called once and the job will be discarded. |
| 46 |
|
| 47 |
A job may call `bg-job-start' to start new jobs and multiple background |
| 48 |
jobs may be active simultaneously." |
| 49 |
(let ((job (cons args (cons done? (cons step finalize))))) |
| 50 |
(push job bg-job-queue)) |
| 51 |
(bg-job-timer-start)) |
| 52 |
|
| 53 |
(defun bg-job-done? (job) |
| 54 |
(apply (cadr job) (car job))) |
| 55 |
|
| 56 |
(defun bg-job-step (job) |
| 57 |
(apply (caddr job) (car job))) |
| 58 |
|
| 59 |
(defun bg-job-finalize (job) |
| 60 |
(apply (cdddr job) (car job))) |
| 61 |
|
| 62 |
(defvar bg-job-queue nil) |
| 63 |
(defvar bg-job-timer nil) |
| 64 |
|
| 65 |
(defun bg-job-timer-start () |
| 66 |
(unless bg-job-timer |
| 67 |
(setq bg-job-timer |
| 68 |
(run-with-timer |
| 69 |
bg-job-period bg-job-period (function bg-job-quantum))))) |
| 70 |
|
| 71 |
(defun bg-job-timer-stop () |
| 72 |
(when bg-job-timer |
| 73 |
(cancel-timer bg-job-timer) |
| 74 |
(setq bg-job-timer nil))) |
| 75 |
|
| 76 |
(defun bg-job-quantum () |
| 77 |
(let ((end-time (+ (bg-job-time-to-double (current-time)) |
| 78 |
(* bg-job-period bg-job-cpu-ratio)))) |
| 79 |
(while (and (< (bg-job-time-to-double (current-time)) |
| 80 |
end-time) |
| 81 |
bg-job-queue) |
| 82 |
(let ((job (pop bg-job-queue))) |
| 83 |
(if (bg-job-done? job) |
| 84 |
(bg-job-finalize job) |
| 85 |
(bg-job-step job) |
| 86 |
(setq bg-job-queue (nconc bg-job-queue (list job))))))) |
| 87 |
(unless bg-job-queue |
| 88 |
(bg-job-timer-stop))) |
| 89 |
|
| 90 |
(defun bg-job-time-to-double (time) |
| 91 |
(+ (* (car time) 65536.0) |
| 92 |
(cadr time) |
| 93 |
(* (caddr time) 1e-06))) |
| 94 |
|
| 95 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 96 |
|
| 97 |
(provide 'bg-job) |