(ert-deftest threads-condvar-wait ()
"test waiting on conditional variable"
(let ((cv-mutex (make-mutex))
- (nthreads (length (all-threads)))
new-thread)
+ ;; We could have spurious threads from the previous tests still
+ ;; running; wait for them to die.
+ (while (> (length (all-threads)) 1)
+ (thread-yield))
(setq threads-condvar (make-condition-variable cv-mutex))
(setq new-thread (make-thread #'threads-test-condvar-wait))
;; Make sure new-thread is alive.
(should (thread-alive-p new-thread))
- (should (= (length (all-threads)) (1+ nthreads)))
+ (should (= (length (all-threads)) 2))
;; Wait for new-thread to become blocked on the condvar.
(while (not (eq (thread--blocker new-thread) threads-condvar))
(thread-yield))
;; Notify the waiting thread.
(with-mutex cv-mutex
(condition-notify threads-condvar t))
- ;; Allow new-thread to process the notification. Sleeping for too
- ;; short time here will fail the length test below.
- (sleep-for 1)
+ ;; Allow new-thread to process the notification.
+ (sleep-for 0.1)
;; Make sure the thread is still there. This used to fail due to
;; a bug in thread.c:condition_wait_callback.
(should (thread-alive-p new-thread))
- (should (= (length (all-threads)) (1+ nthreads)))
- (should (memq new-thread (all-threads)))
- ;; Make sure the other thread waits at the condition variable again.
+ (should (= (length (all-threads)) 2))
(should (eq (thread--blocker new-thread) threads-condvar))
;; Signal the thread.
(thread-signal new-thread 'error '("Die, die, die!"))
(sleep-for 0.1)
;; Make sure the thread died.
- (should (= (length (all-threads)) nthreads))))
+ (should (= (length (all-threads)) 1))))
;;; threads.el ends here