+
+(with-test (:name (:semaphore-notification :wait-on-semaphore)
+ :skipped-on '(not :sb-thread))
+ (let ((sem (make-semaphore))
+ (ok nil)
+ (n 0))
+ (flet ((critical ()
+ (let ((note (make-semaphore-notification)))
+ (sb-sys:without-interrupts
+ (unwind-protect
+ (progn
+ (sb-sys:with-local-interrupts
+ (wait-on-semaphore sem :notification note)
+ (sleep (random 0.1)))
+ (incf n))
+ ;; Re-increment on exit if we decremented it.
+ (when (semaphore-notification-status note)
+ (signal-semaphore sem))
+ ;; KLUDGE: Prevent interrupts after this point from
+ ;; unwinding us, so that we can reason about the counts.
+ #+sb-thread
+ (sb-thread::block-deferrable-signals))))))
+ (let* ((threads (loop for i from 1 upto 100
+ collect (make-join-thread #'critical :name (format nil "T~A" i))))
+ (safe nil)
+ (unsafe nil)
+ (interruptor (make-thread (lambda ()
+ (loop until ok)
+ (let (x)
+ (dolist (thread threads)
+ (cond (x
+ (push thread unsafe)
+ (sleep (random 0.1))
+ (ignore-errors
+ (terminate-thread thread)))
+ (t
+ (push thread safe)))
+ (setf x (not x))))))))
+ (signal-semaphore sem)
+ (setf ok t)
+ (join-thread interruptor)
+ (mapc #'join-thread safe)
+ (let ((k (count-if (lambda (th)
+ (join-thread th :default nil))
+ unsafe)))
+ (assert (= n (+ k (length safe))))
+ (assert unsafe))))))
+
+(with-test (:name (:semaphore-notification :try-sempahore)
+ :skipped-on '(not :sb-thread))
+ (let* ((sem (make-semaphore))
+ (note (make-semaphore-notification)))
+ (try-semaphore sem 1 note)
+ (assert (not (semaphore-notification-status note)))
+ (signal-semaphore sem)
+ (try-semaphore sem 1 note)
+ (assert (semaphore-notification-status note))))
+
+(with-test (:name (:return-from-thread :normal-thread)
+ :skipped-on '(not :sb-thread))
+ (let* ((thread (make-thread (lambda ()
+ (return-from-thread (values 1 2 3))
+ :foo)))
+ (values (multiple-value-list (join-thread thread))))
+ (unless (equal (list 1 2 3) values)
+ (error "got ~S, wanted (1 2 3)" values))))
+
+(with-test (:name (:return-from-thread :main-thread))
+ (assert (main-thread-p))
+ (assert (eq :oops
+ (handler-case
+ (return-from-thread t)
+ (thread-error ()
+ :oops)))))
+
+(with-test (:name (:abort-thread :normal-thread)
+ :skipped-on '(not :sb-thread))
+ (let ((thread (make-thread (lambda ()
+ (abort-thread)
+ :foo))))
+ (assert (eq :aborted! (join-thread thread :default :aborted!)))))
+
+(with-test (:name (:abort-thread :main-thread))
+ (assert (main-thread-p))
+ (assert (eq :oops
+ (handler-case
+ (abort-thread)
+ (thread-error ()
+ :oops)))))
+
+;; SB-THREAD:MAKE-THREAD used to lock SB-THREAD:*MAKE-THREAD-LOCK*
+;; before entering WITHOUT-INTERRUPTS. When a thread which was
+;; executing SB-THREAD:MAKE-THREAD was interrupted with code which
+;; also called SB-THREAD:MAKE-THREAD, it could happen that the first
+;; thread already owned SB-THREAD:*MAKE-THREAD-LOCK* and the
+;; interrupting code thus made a recursive lock attempt.
+;;
+;; See (:TIMER :DISPATCH-THREAD :MAKE-THREAD :BUG-1180102) in
+;; timer.impure.lisp.
+(with-test (:name (make-thread :interrupt-with make-thread :bug-1180102)
+ :skipped-on '(not :sb-thread))
+ (dotimes (i 100)
+ (let ((threads '())
+ (parent *current-thread*))
+ (dotimes (i 100)
+ (push (make-thread
+ (lambda ()
+ (interrupt-thread
+ parent
+ (lambda () (push (make-thread (lambda ())) threads)))))
+ threads)
+ (push (make-thread (lambda ())) threads))
+ (mapc #'join-thread threads))))