diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-06-01 01:31:25 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-06-22 08:33:56 +0200 |
commit | 4251512ea9f70c1a53a56252c4edaf0295473e1a (patch) | |
tree | 5e3dad49985df5c5bd4fd28d9bcb0cc5e0efbf39 /src/test/test-barrier.c | |
parent | test: check size detections by meson (diff) | |
download | systemd-4251512ea9f70c1a53a56252c4edaf0295473e1a.tar.xz systemd-4251512ea9f70c1a53a56252c4edaf0295473e1a.zip |
time-util: introduce usleep_safe()
We use usec_t for storing time value, which is 64bit.
However, usleep() takes useconds_t that is (typically?) 32bit.
Also, usleep() may only support the range [0, 1000000].
This introduce usleep_safe() which takes usec_t.
Diffstat (limited to 'src/test/test-barrier.c')
-rw-r--r-- | src/test/test-barrier.c | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/src/test/test-barrier.c b/src/test/test-barrier.c index b255dba068..0538de9949 100644 --- a/src/test/test-barrier.c +++ b/src/test/test-barrier.c @@ -33,12 +33,6 @@ static void set_alarm(usec_t usecs) { assert_se(setitimer(ITIMER_REAL, &v, NULL) >= 0); } -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) \ TEST(_FUNCTION) { \ Barrier b = BARRIER_NULL; \ @@ -94,9 +88,9 @@ static void sleep_for(usec_t usecs) { /* * 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 sleep_for() on one side + * The timeout makes sure the sync works as expected. The usleep_safe() on one side * makes sure the exit of the parent does not overwrite previous barriers. Due - * to the sleep_for(), we know that the parent already exited, thus there's a + * to the usleep_safe(), 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. */ @@ -104,7 +98,7 @@ TEST_BARRIER(barrier_sync, ({ set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); - sleep_for(BASE_TIME * 2); + usleep_safe(BASE_TIME * 2); assert_se(barrier_sync(&b)); }), TEST_BARRIER_WAIT_SUCCESS(pid1), @@ -124,7 +118,7 @@ TEST_BARRIER(barrier_sync, */ TEST_BARRIER(barrier_wait_next, ({ - sleep_for(BASE_TIME); + usleep_safe(BASE_TIME); set_alarm(BASE_TIME * 10); assert_se(barrier_wait_next(&b)); assert_se(barrier_place(&b)); @@ -150,7 +144,7 @@ TEST_BARRIER(barrier_wait_next, */ TEST_BARRIER(barrier_wait_next_twice, ({ - sleep_for(BASE_TIME); + usleep_safe(BASE_TIME); set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_wait_next(&b)); @@ -161,7 +155,7 @@ TEST_BARRIER(barrier_wait_next_twice, set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); - sleep_for(BASE_TIME * 4); + usleep_safe(BASE_TIME * 4); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -173,7 +167,7 @@ TEST_BARRIER(barrier_wait_next_twice, */ TEST_BARRIER(barrier_wait_next_twice_local, ({ - sleep_for(BASE_TIME); + usleep_safe(BASE_TIME); set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_place(&b)); @@ -186,7 +180,7 @@ TEST_BARRIER(barrier_wait_next_twice_local, set_alarm(BASE_TIME * 10); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); - sleep_for(BASE_TIME * 4); + usleep_safe(BASE_TIME * 4); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -198,7 +192,7 @@ TEST_BARRIER(barrier_wait_next_twice_local, */ TEST_BARRIER(barrier_wait_next_twice_sync, ({ - sleep_for(BASE_TIME); + usleep_safe(BASE_TIME); set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_sync_next(&b)); @@ -219,7 +213,7 @@ TEST_BARRIER(barrier_wait_next_twice_sync, */ TEST_BARRIER(barrier_wait_next_twice_local_sync, ({ - sleep_for(BASE_TIME); + usleep_safe(BASE_TIME); set_alarm(BASE_TIME); assert_se(barrier_wait_next(&b)); assert_se(barrier_place(&b)); @@ -253,7 +247,7 @@ TEST_BARRIER(barrier_sync_next, TEST_BARRIER_WAIT_SUCCESS(pid1), ({ set_alarm(BASE_TIME * 10); - sleep_for(BASE_TIME); + usleep_safe(BASE_TIME); assert_se(barrier_place(&b)); assert_se(barrier_place(&b)); assert_se(barrier_sync(&b)); @@ -274,7 +268,7 @@ TEST_BARRIER(barrier_sync_next_local, }), TEST_BARRIER_WAIT_ALARM(pid1), ({ - sleep_for(BASE_TIME * 2); + usleep_safe(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -323,7 +317,7 @@ TEST_BARRIER(barrier_wait_abortion_unmatched, }), TEST_BARRIER_WAIT_ALARM(pid1), ({ - sleep_for(BASE_TIME * 2); + usleep_safe(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -356,7 +350,7 @@ TEST_BARRIER(barrier_wait_abortion_local_unmatched, }), TEST_BARRIER_WAIT_ALARM(pid1), ({ - sleep_for(BASE_TIME * 2); + usleep_safe(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid2)); @@ -379,12 +373,12 @@ TEST_BARRIER(barrier_exit, /* * Test child exit with sleep * Same as test_barrier_exit but verifies the test really works due to the - * child-exit. We add a usleep() which triggers the alarm in the parent and + * child-exit. We add a usleep_safe() which triggers the alarm in the parent and * causes the test to time out. */ TEST_BARRIER(barrier_no_exit, ({ - sleep_for(BASE_TIME * 2); + usleep_safe(BASE_TIME * 2); }), TEST_BARRIER_WAIT_SUCCESS(pid1), ({ @@ -407,7 +401,7 @@ TEST_BARRIER(barrier_no_exit, TEST_BARRIER(barrier_pending_exit, ({ set_alarm(BASE_TIME * 4); - sleep_for(BASE_TIME * 2); + usleep_safe(BASE_TIME * 2); assert_se(barrier_wait_next(&b)); assert_se(barrier_sync_next(&b)); assert_se(barrier_place(&b)); |