File: timeout.lisp

package info (click to toggle)
maxima-sage 5.45.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 113,788 kB
  • sloc: lisp: 440,833; fortran: 14,665; perl: 14,369; tcl: 10,997; sh: 4,475; makefile: 2,520; ansic: 447; python: 262; xml: 59; awk: 37; sed: 17
file content (44 lines) | stat: -rw-r--r-- 1,662 bytes parent folder | download | duplicates (7)
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. ~%")