From 9c4d2afaa5786107aa79728b4703296b9289bf0b Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Fri, 13 Jan 2017 16:13:30 +0200 Subject: [PATCH] Minor improvements in the new condvar test * test/src/thread-tests.el (threads-test-condvar-wait): Use with-mutex instead of emulating it inline. (threads-condvar-wait): Improve comments. Check that the new thread is alive before waiting for it to become blocked on the conditional variable. --- test/src/thread-tests.el | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/test/src/thread-tests.el b/test/src/thread-tests.el index 71b20185d76..61809e1681b 100644 --- a/test/src/thread-tests.el +++ b/test/src/thread-tests.el @@ -245,34 +245,37 @@ (should-not (thread-alive-p thread)))) (defvar threads-condvar nil) + (defun threads-test-condvar-wait () - ;; Wait for condvar to be notified - (mutex-lock (condition-mutex threads-condvar)) - (condition-wait threads-condvar) - (mutex-unlock (condition-mutex threads-condvar)) + ;; Wait for condvar to be notified. + (with-mutex (condition-mutex threads-condvar) + (condition-wait threads-condvar)) ;; Wait again, it will be signaled. (with-mutex (condition-mutex threads-condvar) (condition-wait threads-condvar))) (ert-deftest threads-condvar-wait () "test waiting on conditional variable" - (let* ((cv-mutex (make-mutex)) - (nthreads (length (all-threads))) - new-thread) + (let ((cv-mutex (make-mutex)) + (nthreads (length (all-threads))) + new-thread) (setq threads-condvar (make-condition-variable cv-mutex)) (setq new-thread (make-thread #'threads-test-condvar-wait)) - (while (not (eq (thread--blocker new-thread) threads-condvar)) - (thread-yield)) + + ;; Make sure new-thread is alive. (should (thread-alive-p new-thread)) (should (= (length (all-threads)) (1+ nthreads))) + ;; 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. (sleep-for 0.1) ;; Make sure the thread is still there. This used to fail due to - ;; a bug in condition_wait_callback. + ;; 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))) -- 2.11.4.GIT