From a25ff30c96e41273cb8ac292667195f1861d4f50 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Fri, 25 May 2018 16:38:23 -0700 Subject: fixes #488 pthread mutex initializer could be simpler The fallback logic was unnecessarily complicated, and found to be somewhat data-racy; on modern systems initializing these things never fails, and on BSD systems that only occurs under extreme memory shortage. --- tests/synch.c | 150 +--------------------------------------------------------- 1 file changed, 2 insertions(+), 148 deletions(-) (limited to 'tests') diff --git a/tests/synch.c b/tests/synch.c index de0d7e16..e767cad8 100644 --- a/tests/synch.c +++ b/tests/synch.c @@ -21,12 +21,6 @@ struct notifyarg { nng_cv * cv; }; -#ifdef NNG_PLATFORM_POSIX -#ifndef NDEBUG -#define SYNC_FALLBACK 1 -#endif -#endif - void notifyafter(void *arg) { @@ -65,117 +59,6 @@ test_sync(void) }); }); Convey("Things block properly", { - - So(nng_mtx_alloc(&arg.mx) == 0); - So(nng_cv_alloc(&arg.cv, arg.mx) == 0); - arg.did = 0; - arg.when = 0; - nng_mtx_lock(arg.mx); - So(nng_thread_create( - &thr, notifyafter, &arg) == 0); - nng_msleep(10); - So(arg.did == 0); - nng_mtx_unlock(arg.mx); - nng_msleep(10); - nng_mtx_lock(arg.mx); - while (!arg.did) { - nng_cv_wait(arg.cv); - } - So(arg.did != 0); - nng_mtx_unlock(arg.mx); - nng_thread_destroy(thr); - nng_cv_free(arg.cv); - nng_mtx_free(arg.mx); - }) - }); - }); - - Convey("Condition variables work", { - - So(nng_mtx_alloc(&arg.mx) == 0); - So(nng_cv_alloc(&arg.cv, arg.mx) == 0); - - Reset({ - nng_cv_free(arg.cv); - nng_mtx_free(arg.mx); - }); - - Convey("Notification works", { - arg.did = 0; - arg.when = 10; - So(nng_thread_create(&thr, notifyafter, &arg) == 0); - - nng_mtx_lock(arg.mx); - if (!arg.did) { - nng_cv_wait(arg.cv); - } - nng_mtx_unlock(arg.mx); - nng_thread_destroy(thr); - So(arg.did == 1); - }); - - Convey("Timeout works", { - arg.did = 0; - arg.when = 200; - So(nng_thread_create(&thr, notifyafter, &arg) == 0); - nng_mtx_lock(arg.mx); - if (!arg.did) { - nng_cv_until(arg.cv, nng_clock() + 10); - } - So(arg.did == 0); - nng_mtx_unlock(arg.mx); - nng_thread_destroy(thr); - }); - - Convey("Empty timeout is EAGAIN", { - nng_mtx_lock(arg.mx); - So(nng_cv_until(arg.cv, 0) == NNG_EAGAIN); - nng_mtx_unlock(arg.mx); - }); - - Convey("Not running works", { - arg.did = 0; - arg.when = 1; - nng_mtx_lock(arg.mx); - if (!arg.did) { - nng_cv_until(arg.cv, nng_clock() + 10); - } - So(arg.did == 0); - nng_mtx_unlock(arg.mx); - }); - }); -} - -#if SYNC_FALLBACK -extern int nni_plat_sync_fallback; - -#define ConveyFB(x, y) Convey(x, y) - -static void -test_sync_fallback(void) -{ - nni_plat_sync_fallback = 1; - Convey("Mutexes work", { - nng_mtx *mx; - - So(nng_mtx_alloc(&mx) == 0); - Reset({ nng_mtx_free(mx); }); - - Convey("We can lock a mutex", { - nng_mtx_lock(mx); - So(1); - Convey("And we can unlock it", { - nng_mtx_unlock(mx); - So(1); - Convey("And then lock it again", { - nng_mtx_lock(mx); - So(1); - nng_mtx_unlock(mx); - So(1); - }); - }); - Convey("Things block properly", { - So(nng_mtx_alloc(&arg.mx) == 0); So(nng_cv_alloc(&arg.cv, arg.mx) == 0); arg.did = 0; @@ -201,7 +84,6 @@ test_sync_fallback(void) }); Convey("Condition variables work", { - So(nng_mtx_alloc(&arg.mx) == 0); So(nng_cv_alloc(&arg.cv, arg.mx) == 0); @@ -255,34 +137,6 @@ test_sync_fallback(void) }); }); } -#else -#define ConveyFB(x, y) -#endif - -TestMain("Synchronization", { - Convey("Synchronization works", { test_sync(); }); - - ConveyFB("Fallback synchronization works", { test_sync_fallback(); }); - - ConveyFB("Transform works", { - nni_plat_sync_fallback = 0; - So(nng_mtx_alloc(&arg.mx) == 0); - nni_plat_sync_fallback = 1; - So(nng_cv_alloc(&arg.cv, arg.mx) == 0); - - arg.did = 0; - arg.when = 10; - So(nng_thread_create(&thr, notifyafter, &arg) == 0); - - nng_mtx_lock(arg.mx); - if (!arg.did) { - nng_cv_wait(arg.cv); - } - nng_mtx_unlock(arg.mx); - nng_thread_destroy(thr); - So(arg.did == 1); - nng_cv_free(arg.cv); - nng_mtx_free(arg.mx); - }); -}) +TestMain( + "Synchronization", { Convey("Synchronization works", { test_sync(); }); }) -- cgit v1.2.3-70-g09d2