aboutsummaryrefslogtreecommitdiffstats
path: root/test/src
diff options
context:
space:
mode:
authorEli Zaretskii2017-01-13 16:13:30 +0200
committerEli Zaretskii2017-01-13 16:13:30 +0200
commit9c4d2afaa5786107aa79728b4703296b9289bf0b (patch)
tree343fdc17f15a6a5ad8ec940ce4abc34aa7cdb309 /test/src
parent03e4ab0d586069be65e4a17fbf4cd965a9984726 (diff)
downloademacs-9c4d2afaa5786107aa79728b4703296b9289bf0b.tar.gz
emacs-9c4d2afaa5786107aa79728b4703296b9289bf0b.zip
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.
Diffstat (limited to 'test/src')
-rw-r--r--test/src/thread-tests.el25
1 files 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 @@
245 (should-not (thread-alive-p thread)))) 245 (should-not (thread-alive-p thread))))
246 246
247(defvar threads-condvar nil) 247(defvar threads-condvar nil)
248
248(defun threads-test-condvar-wait () 249(defun threads-test-condvar-wait ()
249 ;; Wait for condvar to be notified 250 ;; Wait for condvar to be notified.
250 (mutex-lock (condition-mutex threads-condvar)) 251 (with-mutex (condition-mutex threads-condvar)
251 (condition-wait threads-condvar) 252 (condition-wait threads-condvar))
252 (mutex-unlock (condition-mutex threads-condvar))
253 ;; Wait again, it will be signaled. 253 ;; Wait again, it will be signaled.
254 (with-mutex (condition-mutex threads-condvar) 254 (with-mutex (condition-mutex threads-condvar)
255 (condition-wait threads-condvar))) 255 (condition-wait threads-condvar)))
256 256
257(ert-deftest threads-condvar-wait () 257(ert-deftest threads-condvar-wait ()
258 "test waiting on conditional variable" 258 "test waiting on conditional variable"
259 (let* ((cv-mutex (make-mutex)) 259 (let ((cv-mutex (make-mutex))
260 (nthreads (length (all-threads))) 260 (nthreads (length (all-threads)))
261 new-thread) 261 new-thread)
262 (setq threads-condvar (make-condition-variable cv-mutex)) 262 (setq threads-condvar (make-condition-variable cv-mutex))
263 (setq new-thread (make-thread #'threads-test-condvar-wait)) 263 (setq new-thread (make-thread #'threads-test-condvar-wait))
264 (while (not (eq (thread--blocker new-thread) threads-condvar)) 264
265 (thread-yield)) 265 ;; Make sure new-thread is alive.
266 (should (thread-alive-p new-thread)) 266 (should (thread-alive-p new-thread))
267 (should (= (length (all-threads)) (1+ nthreads))) 267 (should (= (length (all-threads)) (1+ nthreads)))
268 ;; Wait for new-thread to become blocked on the condvar.
269 (while (not (eq (thread--blocker new-thread) threads-condvar))
270 (thread-yield))
271
268 ;; Notify the waiting thread. 272 ;; Notify the waiting thread.
269 (with-mutex cv-mutex 273 (with-mutex cv-mutex
270 (condition-notify threads-condvar t)) 274 (condition-notify threads-condvar t))
271
272 ;; Allow new-thread to process the notification. 275 ;; Allow new-thread to process the notification.
273 (sleep-for 0.1) 276 (sleep-for 0.1)
274 ;; Make sure the thread is still there. This used to fail due to 277 ;; Make sure the thread is still there. This used to fail due to
275 ;; a bug in condition_wait_callback. 278 ;; a bug in thread.c:condition_wait_callback.
276 (should (thread-alive-p new-thread)) 279 (should (thread-alive-p new-thread))
277 (should (= (length (all-threads)) (1+ nthreads))) 280 (should (= (length (all-threads)) (1+ nthreads)))
278 (should (memq new-thread (all-threads))) 281 (should (memq new-thread (all-threads)))