File: timeout.lisp

package info (click to toggle)
maxima 5.47.0-9
  • links: PTS
  • area: main
  • in suites: forky
  • size: 193,104 kB
  • sloc: lisp: 434,678; fortran: 14,665; tcl: 10,990; sh: 4,577; makefile: 2,763; ansic: 447; java: 328; python: 262; perl: 201; xml: 60; awk: 28; sed: 15; javascript: 2
file content (44 lines) | stat: -rw-r--r-- 1,662 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
;; stopme command, only for sbcl
(in-package :maxima)

;; stopme and  time_constrained are the same except that
;; time_constrained, in case of timeout simply returns
;; the expression Aborted.
;; stopme gives a message, and returns it. The message contains the unevaluated expr.
;; There are too many other variants possible to cover them all.
;; You may want a slightly different behavior. e.g. return unevaluated expr only.
;; (Mathematica returns "$Aborted" for timeout.)

#+sbcl
(defmspec $stopme(args)	;; usage:  stopme (do x:x+1, 2) ; stops infinite loop after 2 sec
  (let* ((evalme(cadr args))		  ;  not evaluated !
	 (timelimit (meval (caddr args))) ; evaluate the time limit
	 (v (sb-ext::make-timer ;; the timer program
	     (lambda()
	       (let ((msg `((mtext) ,evalme "   timed out after " ,timelimit " seconds")))
	       (mformat t "~m" msg)
	       (throw 'stopme msg ))))))
    (catch 'stopme
      (progn
	(sb-ext::schedule-timer v timelimit)
	(prog1 (meval evalme)
	  (sb-ext::unschedule-timer v))	))))

#+sbcl
(defmspec $time_constrained(args)	;; usage:  stopme (do x:x+1, 2) ; stops infinite loop after 2 sec
  (let* ((evalme(cadr args))		  ;  not evaluated !
	 (timelimit (meval (caddr args))) ; evaluate the time limit
	 (v (sb-ext::make-timer ;; the timer program
	     (lambda()(throw 'stopme '$Aborted
			     ;;evalme ;; just return unevaluated. 
			     ) ))))  
    (catch 'stopme
      (progn
	(sb-ext::schedule-timer v timelimit)
	(prog1 (meval evalme)
	  (sb-ext::unschedule-timer v))	))))

#-sbcl (format t "~%time_constrained and stopme 
are currently unimplemented
in your lisp. 
Available in sbcl versions of Maxima. ~%")