]> git.eshelyaron.com Git - emacs.git/commitdiff
Minor improvements in the new condvar test
authorEli Zaretskii <eliz@gnu.org>
Fri, 13 Jan 2017 14:13:30 +0000 (16:13 +0200)
committerEli Zaretskii <eliz@gnu.org>
Fri, 13 Jan 2017 14:13:30 +0000 (16:13 +0200)
* 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

index 71b20185d76c5c7e44964dfe9ecdc3980baae35f..61809e1681b93b7131273eb12792586be652eafe 100644 (file)
     (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)))