X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Ftest%2Ftest-barrier.c;h=2d109a30e7f8ecf9ec3d6b3bb0ccfa2e0161c45b;hb=adfe5671ef794099068038dfccbf1eb5134433c8;hp=672a51e2c39f619068a9c2603a18b9afa2615d6a;hpb=7566e26721ee95d6fc864e9e6654fb61bd3cd603;p=elogind.git diff --git a/src/test/test-barrier.c b/src/test/test-barrier.c index 672a51e2c..2d109a30e 100644 --- a/src/test/test-barrier.c +++ b/src/test/test-barrier.c @@ -28,33 +28,31 @@ * increase it at the slightly cost of lengthen test-duration on other machines. */ -#include #include -#include #include #include #include #include "barrier.h" -#include "def.h" #include "util.h" /* 20ms to test deadlocks; All timings use multiples of this constant as * alarm/sleep timers. If this timeout is too small for slow machines to perform * the requested operations, we have to increase it. On an i7 this works fine * with 1ms base-time, so 20ms should be just fine for everyone. */ -#define BASE_TIME 20 +#define BASE_TIME (20 * USEC_PER_MSEC) -static void malarm(unsigned long msecs) { +static void set_alarm(usec_t usecs) { struct itimerval v = { }; - timeval_store(&v.it_value, msecs * USEC_PER_MSEC); + timeval_store(&v.it_value, usecs); assert_se(setitimer(ITIMER_REAL, &v, NULL) >= 0); } -static void msleep(unsigned long msecs) { - assert_se(msecs < MSEC_PER_SEC); - usleep(msecs * USEC_PER_MSEC); +static void sleep_for(usec_t usecs) { + /* stupid usleep() might fail if >1000000 */ + assert_se(usecs < USEC_PER_SEC); + usleep(usecs); } #define TEST_BARRIER(_FUNCTION, _CHILD_CODE, _WAIT_CHILD, _PARENT_CODE, _WAIT_PARENT) \ @@ -63,6 +61,10 @@ static void msleep(unsigned long msecs) { pid_t pid1, pid2; \ \ assert_se(barrier_create(&b) >= 0); \ + assert_se(b.me > 0); \ + assert_se(b.them > 0); \ + assert_se(b.pipe[0] > 0); \ + assert_se(b.pipe[1] > 0); \ \ pid1 = fork(); \ assert_se(pid1 >= 0); \ @@ -81,10 +83,10 @@ static void msleep(unsigned long msecs) { } \ \ barrier_destroy(&b); \ - malarm(999); \ + set_alarm(999999); \ { _WAIT_CHILD; } \ { _WAIT_PARENT; } \ - malarm(0); \ + set_alarm(0); \ } #define TEST_BARRIER_WAIT_SUCCESS(_pid) \ @@ -108,22 +110,22 @@ static void msleep(unsigned long msecs) { /* * Test basic sync points * This places a barrier in both processes and waits synchronously for them. - * The timeout makes sure the sync works as expected. The msleep() on one side + * The timeout makes sure the sync works as expected. The sleep_for() on one side * makes sure the exit of the parent does not overwrite previous barriers. Due - * to the msleep(), we know that the parent already exited, thus there's a + * to the sleep_for(), we know that the parent already exited, thus there's a * pending HUP on the pipe. However, the barrier_sync() prefers reads on the * eventfd, thus we can safely wait on the barrier. */ TEST_BARRIER(test_barrier_sync, ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); - msleep(BASE_TIME * 2); + sleep_for(BASE_TIME * 2); assert_se(barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_sync(&b)); }), @@ -138,15 +140,15 @@ TEST_BARRIER(test_barrier_sync, */ TEST_BARRIER(test_barrier_wait_next, ({ - msleep(100); - malarm(BASE_TIME * 10); + sleep_for(BASE_TIME); + set_alarm(BASE_TIME * 10); assert_se(barrier_wait_next(&b)); assert_se(barrier_place(&b)); assert_se(barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ - malarm(400); + set_alarm(BASE_TIME * 4); assert_se(barrier_place(&b)); assert_se(barrier_sync(&b)); }), @@ -164,18 +166,18 @@ TEST_BARRIER(test_barrier_wait_next, */ TEST_BARRIER(test_barrier_wait_next_twice, ({ - msleep(BASE_TIME); - malarm(BASE_TIME); + sleep_for(BASE_TIME); + set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_wait_next(&b)); assert_se(0); }), TEST_BARRIER_WAIT_ALARM(pid1), ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); - msleep(BASE_TIME * 2); + sleep_for(BASE_TIME * 4); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -187,8 +189,8 @@ TEST_BARRIER(test_barrier_wait_next_twice, */ TEST_BARRIER(test_barrier_wait_next_twice_local, ({ - msleep(BASE_TIME); - malarm(BASE_TIME); + sleep_for(BASE_TIME); + set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); @@ -197,10 +199,10 @@ TEST_BARRIER(test_barrier_wait_next_twice_local, }), TEST_BARRIER_WAIT_ALARM(pid1), ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); - msleep(BASE_TIME * 2); + sleep_for(BASE_TIME * 4); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -212,14 +214,14 @@ TEST_BARRIER(test_barrier_wait_next_twice_local, */ TEST_BARRIER(test_barrier_wait_next_twice_sync, ({ - msleep(BASE_TIME); - malarm(BASE_TIME); + sleep_for(BASE_TIME); + set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_sync_next(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); }), @@ -233,8 +235,8 @@ TEST_BARRIER(test_barrier_wait_next_twice_sync, */ TEST_BARRIER(test_barrier_wait_next_twice_local_sync, ({ - msleep(BASE_TIME); - malarm(BASE_TIME); + sleep_for(BASE_TIME); + set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); @@ -242,7 +244,7 @@ TEST_BARRIER(test_barrier_wait_next_twice_local_sync, }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); }), @@ -255,7 +257,7 @@ TEST_BARRIER(test_barrier_wait_next_twice_local_sync, */ TEST_BARRIER(test_barrier_sync_next, ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_sync_next(&b)); assert_se(barrier_sync(&b)); assert_se(barrier_place(&b)); @@ -266,8 +268,8 @@ TEST_BARRIER(test_barrier_sync_next, }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ - malarm(BASE_TIME * 10); - msleep(BASE_TIME); + set_alarm(BASE_TIME * 10); + sleep_for(BASE_TIME); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); assert_se(barrier_sync(&b)); @@ -281,14 +283,14 @@ TEST_BARRIER(test_barrier_sync_next, */ TEST_BARRIER(test_barrier_sync_next_local, ({ - malarm(BASE_TIME); + set_alarm(BASE_TIME); assert_se(barrier_place(&b)); assert_se(barrier_sync_next(&b)); assert_se(0); }), TEST_BARRIER_WAIT_ALARM(pid1), ({ - msleep(BASE_TIME * 2); + sleep_for(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -299,7 +301,7 @@ TEST_BARRIER(test_barrier_sync_next_local, */ TEST_BARRIER(test_barrier_sync_next_local_abort, ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(!barrier_sync_next(&b)); }), @@ -315,7 +317,7 @@ TEST_BARRIER(test_barrier_sync_next_local_abort, */ TEST_BARRIER(test_barrier_wait_abortion, ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_wait_abortion(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), @@ -331,13 +333,13 @@ TEST_BARRIER(test_barrier_wait_abortion, */ TEST_BARRIER(test_barrier_wait_abortion_unmatched, ({ - malarm(BASE_TIME); + set_alarm(BASE_TIME); assert_se(barrier_wait_abortion(&b)); assert_se(0); }), TEST_BARRIER_WAIT_ALARM(pid1), ({ - msleep(BASE_TIME * 2); + sleep_for(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -347,7 +349,7 @@ TEST_BARRIER(test_barrier_wait_abortion_unmatched, */ TEST_BARRIER(test_barrier_wait_abortion_local, ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_abort(&b)); assert_se(!barrier_wait_abortion(&b)); }), @@ -363,14 +365,14 @@ TEST_BARRIER(test_barrier_wait_abortion_local, */ TEST_BARRIER(test_barrier_wait_abortion_local_unmatched, ({ - malarm(BASE_TIME); + set_alarm(BASE_TIME); assert_se(barrier_abort(&b)); assert_se(!barrier_wait_abortion(&b)); assert_se(0); }), TEST_BARRIER_WAIT_ALARM(pid1), ({ - msleep(BASE_TIME * 2); + sleep_for(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -384,7 +386,7 @@ TEST_BARRIER(test_barrier_exit, }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ - malarm(BASE_TIME * 10); + set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(!barrier_sync(&b)); }), @@ -398,11 +400,11 @@ TEST_BARRIER(test_barrier_exit, */ TEST_BARRIER(test_barrier_no_exit, ({ - msleep(BASE_TIME * 2); + sleep_for(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ - malarm(BASE_TIME); + set_alarm(BASE_TIME); assert_se(barrier_place(&b)); assert_se(!barrier_sync(&b)); }), @@ -420,8 +422,8 @@ TEST_BARRIER(test_barrier_no_exit, */ TEST_BARRIER(test_barrier_pending_exit, ({ - malarm(BASE_TIME * 4); - msleep(BASE_TIME * 2); + set_alarm(BASE_TIME * 4); + sleep_for(BASE_TIME * 2); assert_se(barrier_wait_next(&b)); assert_se(barrier_sync_next(&b)); assert_se(barrier_place(&b));