1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
12 (in-package "CL-USER")
14 (use-package :test-util)
16 (with-test (:name :heap)
18 (heap (make-array size :adjustable t :fill-pointer 0))
19 (unsorted (loop for i below size collect (random size)))
20 (sorted (sort (copy-list unsorted) #'>=))
22 (map nil #'(lambda (val) (sb-impl::heap-insert heap val)) unsorted)
23 (setf heap-sorted (loop for i below size
24 collect (sb-impl::heap-extract-maximum heap)))
25 (unless (equal sorted heap-sorted)
26 (error "Heap sort failure ~S" heap-sorted))))
28 (sb-alien:define-alien-routine "check_deferrables_blocked_or_lose"
30 (where sb-alien:unsigned-long))
31 (sb-alien:define-alien-routine "check_deferrables_unblocked_or_lose"
33 (where sb-alien:unsigned-long))
35 (defun make-limited-timer (fn n &rest args)
38 (apply #'sb-ext:make-timer
40 (sb-sys:without-interrupts
43 (warn "Unscheduling timer ~A ~
44 upon reaching run limit. System too slow?"
46 (sb-ext:unschedule-timer timer))
48 (sb-sys:allow-with-interrupts
52 (defun make-and-schedule-and-wait (fn time)
53 (let ((finishedp nil))
54 (sb-ext:schedule-timer (sb-ext:make-timer
56 (sb-sys:without-interrupts
58 (sb-sys:allow-with-interrupts
60 (setq finishedp t)))))
62 (loop until finishedp)))
64 (with-test (:name (:timer :deferrables-blocked) :skipped-on :win32)
65 (make-and-schedule-and-wait (lambda ()
66 (check-deferrables-blocked-or-lose 0))
68 (check-deferrables-unblocked-or-lose 0))
70 (with-test (:name (:timer :deferrables-unblocked) :skipped-on :win32)
71 (make-and-schedule-and-wait (lambda ()
72 (sb-sys:with-interrupts
73 (check-deferrables-unblocked-or-lose 0)))
75 (check-deferrables-unblocked-or-lose 0))
77 (with-test (:name (:timer :deferrables-unblocked :unwind) :skipped-on :win32)
79 (make-and-schedule-and-wait (lambda ()
80 (check-deferrables-blocked-or-lose 0)
84 (check-deferrables-unblocked-or-lose 0))
86 (defmacro raises-timeout-p (&body body)
87 `(handler-case (progn (progn ,@body) nil)
88 (sb-ext:timeout () t)))
90 (with-test (:name (:timer :relative)
91 :fails-on '(and :sparc :linux)
93 (let* ((has-run-p nil)
94 (timer (make-timer (lambda () (setq has-run-p t))
95 :name "simple timer")))
96 (schedule-timer timer 0.5)
98 (assert (not has-run-p))
101 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
103 (with-test (:name (:timer :absolute)
104 :fails-on '(and :sparc :linux)
106 (let* ((has-run-p nil)
107 (timer (make-timer (lambda () (setq has-run-p t))
108 :name "simple timer")))
109 (schedule-timer timer (+ 1/2 (get-universal-time)) :absolute-p t)
111 (assert (not has-run-p))
114 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
116 (with-test (:name (:timer :other-thread) :skipped-on '(not :sb-thread))
117 (let* ((thread (sb-thread:make-thread (lambda () (sleep 2))))
118 (timer (make-timer (lambda ()
119 (assert (eq thread sb-thread:*current-thread*)))
121 (schedule-timer timer 0.1)))
123 (with-test (:name (:timer :new-thread) :skipped-on '(not :sb-thread))
124 (let* ((original-thread sb-thread:*current-thread*)
127 (assert (not (eq original-thread
128 sb-thread:*current-thread*))))
130 (schedule-timer timer 0.1)))
132 (with-test (:name (:timer :repeat-and-unschedule)
133 :fails-on '(and :sparc :linux)
138 (make-timer (lambda ()
139 (when (= 5 (incf run-count))
140 (unschedule-timer timer)))))
141 (schedule-timer timer 0 :repeat-interval 0.2)
142 (assert (timer-scheduled-p timer :delta 0.3))
144 (assert (= 5 run-count))
145 (assert (not (timer-scheduled-p timer)))
146 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
148 (with-test (:name (:timer :reschedule) :skipped-on :win32)
149 (let* ((has-run-p nil)
150 (timer (make-timer (lambda ()
151 (setq has-run-p t)))))
152 (schedule-timer timer 0.2)
153 (schedule-timer timer 0.3)
156 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
158 (with-test (:name (:timer :stress) :skipped-on :win32)
159 (let ((time (1+ (get-universal-time))))
161 (schedule-timer (make-timer (lambda ())) time :absolute-p t))
163 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
165 (with-test (:name (:timer :stress2) :skipped-on :win32)
166 (let ((time (1+ (get-universal-time)))
168 (loop for time-n from time upto (+ 1/10 time) by (/ 1/10 200)
169 do (schedule-timer (make-timer (lambda ())) time-n :absolute-p t)
172 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
174 (with-test (:name (:with-timeout :timeout) :skipped-on :win32)
175 (assert (raises-timeout-p
176 (sb-ext:with-timeout 0.2
179 (with-test (:name (:with-timeout :fall-through) :skipped-on :win32)
180 (assert (not (raises-timeout-p
181 (sb-ext:with-timeout 0.3
184 (with-test (:name (:with-timeout :nested-timeout-smaller) :skipped-on :win32)
185 (assert(raises-timeout-p
186 (sb-ext:with-timeout 10
187 (sb-ext:with-timeout 0.5
190 (with-test (:name (:with-timeout :nested-timeout-bigger) :skipped-on :win32)
191 (assert(raises-timeout-p
192 (sb-ext:with-timeout 0.5
193 (sb-ext:with-timeout 2
196 (defun wait-for-threads (threads)
197 (loop while (some #'sb-thread:thread-alive-p threads) do (sleep 0.01)))
199 (with-test (:name (:with-timeout :many-at-the-same-time) :skipped-on '(not :sb-thread))
201 (let ((threads (loop repeat 10 collect
202 (sb-thread:make-thread
205 (sb-ext:with-timeout 0.5
208 (format t "~%not ok~%"))
211 (assert (not (raises-timeout-p
212 (sb-ext:with-timeout 20
213 (wait-for-threads threads)))))
216 (with-test (:name (:with-timeout :dead-thread) :skipped-on '(not :sb-thread))
217 (sb-thread:make-thread
219 (let ((timer (make-timer (lambda ()))))
220 (schedule-timer timer 3)
226 (defun random-type (n)
227 `(integer ,(random n) ,(+ n (random n))))
229 ;;; FIXME: Since timeouts do not work on Windows this would loop
231 (with-test (:name (:hash-cache :interrupt) :skipped-on :win32)
232 (let* ((type1 (random-type 500))
233 (type2 (random-type 500))
234 (wanted (subtypep type1 type2)))
237 (sb-ext:schedule-timer (sb-ext:make-timer
239 (assert (eq wanted (subtypep type1 type2)))
243 (assert (eq wanted (subtypep type1 type2))))))))
245 ;;; Used to hang occasionally at least on x86. Two bugs caused it:
246 ;;; running out of stack (due to repeating timers being rescheduled
247 ;;; before they ran) and dying threads were open interrupts.
248 (with-test (:name (:timer :parallel-unschedule)
249 :skipped-on '(not :sb-thread)
251 (let ((timer (sb-ext:make-timer (lambda () 42) :name "parallel schedulers"))
254 (sleep (random 0.01))
256 do (sb-ext:unschedule-timer timer))))
257 (sb-sys:with-deadline (:seconds 30)
259 do (mapcar #'sb-thread:join-thread
260 (loop for i from 1 upto 10
261 collect (let* ((thread (sb-thread:make-thread #'flop
262 :name (format nil "scheduler ~A" i)))
263 (ticker (make-limited-timer (lambda () 13)
265 :thread (or other thread)
266 :name (format nil "ticker ~A" i))))
268 (sb-ext:schedule-timer ticker 0 :repeat-interval 0.00001)
271 ;;;; FIXME: OS X 10.4 doesn't like these being at all, and gives us a SIGSEGV
272 ;;;; instead of using the Mach expection system! 10.5 on the other tends to
273 ;;;; lose() here with interrupt already pending. :/
275 ;;;; Used to have problems in genereal, see comment on (:TIMER
276 ;;;; :PARALLEL-UNSCHEDULE).
277 (with-test (:name (:timer :schedule-stress) :skipped-on :win32)
280 (loop for i from 1 upto 1
281 collect (make-limited-timer
284 :name (format nil "slow ~A" i))))
285 (fast-timer (make-limited-timer (lambda () 42) 1000
287 (sb-ext:schedule-timer fast-timer 0.0001 :repeat-interval 0.0001)
288 (dolist (timer slow-timers)
289 (sb-ext:schedule-timer timer (random 0.1)
290 :repeat-interval (random 0.1)))
291 (dolist (timer slow-timers)
292 (sb-ext:unschedule-timer timer))
293 (sb-ext:unschedule-timer fast-timer))))
295 (mapcar #'sb-thread:join-thread
296 (loop repeat 10 collect (sb-thread:make-thread #'test)))
298 (loop repeat 10 do (test))))
300 (with-test (:name (:timer :threaded-stress) :skipped-on '(not :sb-thread))
301 (let ((barrier (sb-thread:make-semaphore))
303 (flet ((wait-for-goal ()
305 (declare (special *n*))
306 (sb-thread:signal-semaphore barrier)
307 (loop until (eql *n* goal))))
309 (declare (special *n*))
311 (let ((threads (list (sb-thread:make-thread #'wait-for-goal)
312 (sb-thread:make-thread #'wait-for-goal)
313 (sb-thread:make-thread #'wait-for-goal))))
314 (sb-thread:wait-on-semaphore barrier)
315 (sb-thread:wait-on-semaphore barrier)
316 (sb-thread:wait-on-semaphore barrier)
317 (flet ((sched (thread)
318 (sb-thread:make-thread (lambda ()
320 do (sb-ext:schedule-timer (make-timer #'one :thread thread) 0.001))))))
321 (dolist (thread threads)
323 (mapcar #'sb-thread:join-thread threads)))))