More documentation and comment out debugging print.
[maxima.git] / share / contrib / timeout.lisp
bloba2e6a9719bb83fdd51ebc20aacbf39bd3d92eb9d
1 ;; stopme command, only for sbcl
2 (in-package :maxima)
4 ;; stopme and time_constrained are the same except that
5 ;; time_constrained, in case of timeout simply returns
6 ;; the expression Aborted.
7 ;; stopme gives a message, and returns it. The message contains the unevaluated expr.
8 ;; There are too many other variants possible to cover them all.
9 ;; You may want a slightly different behavior. e.g. return unevaluated expr only.
10 ;; (Mathematica returns "$Aborted" for timeout.)
12 #+sbcl
13 (defmspec $stopme(args) ;; usage: stopme (do x:x+1, 2) ; stops infinite loop after 2 sec
14 (let* ((evalme(cadr args)) ; not evaluated !
15 (timelimit (meval (caddr args))) ; evaluate the time limit
16 (v (sb-ext::make-timer ;; the timer program
17 (lambda()
18 (let ((msg `((mtext) ,evalme " timed out after " ,timelimit " seconds")))
19 (mformat t "~m" msg)
20 (throw 'stopme msg ))))))
21 (catch 'stopme
22 (progn
23 (sb-ext::schedule-timer v timelimit)
24 (prog1 (meval evalme)
25 (sb-ext::unschedule-timer v)) ))))
27 #+sbcl
28 (defmspec $time_constrained(args) ;; usage: stopme (do x:x+1, 2) ; stops infinite loop after 2 sec
29 (let* ((evalme(cadr args)) ; not evaluated !
30 (timelimit (meval (caddr args))) ; evaluate the time limit
31 (v (sb-ext::make-timer ;; the timer program
32 (lambda()(throw 'stopme '$Aborted
33 ;;evalme ;; just return unevaluated.
34 ) ))))
35 (catch 'stopme
36 (progn
37 (sb-ext::schedule-timer v timelimit)
38 (prog1 (meval evalme)
39 (sb-ext::unschedule-timer v)) ))))
41 #-sbcl (format t "~%time_constrained and stopme
42 are currently unimplemented
43 in your lisp.
44 Available in sbcl versions of Maxima. ~%")