3 (use-package :test-util)
5 (defmacro assert-timeout (form)
6 (let ((ok (gensym "OK")))
12 (error "No timeout from form:~% ~S" ',form)))))
14 (defun run-sleep (seconds)
15 (sb-ext:run-program "sleep" (list (format nil "~D" seconds))
18 (with-test (:name (:deadline :run-program :trivial))
19 (assert-timeout (sb-sys:with-deadline (:seconds 1)
22 (with-test (:name (:deadline :defer-deadline-1))
26 (handler-bind ((sb-sys:deadline-timeout
30 (sb-sys:defer-deadline 0.1 c)))))
31 (sb-sys:with-deadline (:seconds 1)
33 (sb-sys:deadline-timeout (c)
38 (with-test (:name (:deadline :defer-deadline-2))
42 (handler-bind ((sb-sys:deadline-timeout
45 (sb-sys:defer-deadline 0.1 c))))
46 (sb-sys:with-deadline (:seconds 1)
48 (sb-sys:deadline-timeout (c)
51 (assert (not final))))
53 (with-test (:name (:deadline :cancel-deadline))
57 (handler-bind ((sb-sys:deadline-timeout
60 (sb-sys:cancel-deadline c))))
61 (sb-sys:with-deadline (:seconds 1)
63 (sb-sys:deadline-timeout (c)
66 (assert (not final))))
68 (with-test (:name (:deadline :get-mutex) :skipped-on '(not (and :sb-thread (not :sb-lutex))))
70 (let ((lock (sb-thread:make-mutex))
72 (sb-thread:make-thread (lambda ()
73 (sb-thread:get-mutex lock)
76 (loop while waitp do (sleep 0.01))
77 (sb-sys:with-deadline (:seconds 1)
78 (sb-thread:get-mutex lock)))))
80 (with-test (:name (:deadline :wait-on-semaphore) :skipped-on '(not (and :sb-thread (not :sb-lutex))))
82 (let ((sem (sb-thread::make-semaphore :count 0)))
83 (sb-sys:with-deadline (:seconds 1)
84 (sb-thread::wait-on-semaphore sem)))))
86 (with-test (:name (:deadline :join-thread) :skipped-on '(not (and :sb-thread (not :sb-lutex))))
88 (sb-sys:with-deadline (:seconds 1)
89 (sb-thread:join-thread
90 (sb-thread:make-thread (lambda () (loop (sleep 1))))))))
92 (with-test (:name (:deadline :futex-wait-eintr) :skipped-on '(not (and :sb-thread (not :sb-lutex))))
93 (let ((lock (sb-thread:make-mutex))
95 (sb-thread:make-thread (lambda ()
96 (sb-thread:get-mutex lock)
99 (loop while waitp do (sleep 0.01))
100 (let ((thread (sb-thread:make-thread
102 (let ((start (get-internal-real-time)))
104 (sb-sys:with-deadline (:seconds 1)
105 (sb-thread:get-mutex lock))
106 (sb-sys:deadline-timeout (x)
108 (let ((end (get-internal-real-time)))
109 (float (/ (- end start)
110 internal-time-units-per-second)
113 (sb-thread:interrupt-thread thread (lambda () 42))
114 (let ((seconds-passed (sb-thread:join-thread thread)))
115 (format t "Deadline in ~S~%" seconds-passed)
116 (assert (< seconds-passed 1.2))))))