diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-05-25 16:38:23 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-05-29 16:02:37 -0700 |
| commit | a25ff30c96e41273cb8ac292667195f1861d4f50 (patch) | |
| tree | 5741a11b901b3e396913ac4f2c07c6123c8e04ec /tests | |
| parent | 301de3ac5c7cf8a5eaaf3c58157251db781841d6 (diff) | |
| download | nng-a25ff30c96e41273cb8ac292667195f1861d4f50.tar.gz nng-a25ff30c96e41273cb8ac292667195f1861d4f50.tar.bz2 nng-a25ff30c96e41273cb8ac292667195f1861d4f50.zip | |
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.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/synch.c | 150 |
1 files changed, 2 insertions, 148 deletions
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(); }); }) |
