* 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) \
static void _FUNCTION(void) { \
- Barrier b; \
+ Barrier b = BARRIER_NULL; \
pid_t pid1, pid2; \
\
- assert_se(barrier_init(&b) >= 0); \
+ assert_se(barrier_create(&b) >= 0); \
\
pid1 = fork(); \
assert_se(pid1 >= 0); \
} \
\
barrier_destroy(&b); \
- malarm(999); \
+ set_alarm(999999); \
{ _WAIT_CHILD; } \
{ _WAIT_PARENT; } \
- malarm(0); \
+ set_alarm(0); \
}
#define TEST_BARRIER_WAIT_SUCCESS(_pid) \
/*
* 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));
}),
*/
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));
}),
*/
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));
*/
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));
}),
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));
*/
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));
}),
*/
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));
}),
TEST_BARRIER_WAIT_SUCCESS(pid1),
({
- malarm(BASE_TIME * 10);
+ set_alarm(BASE_TIME * 10);
assert_se(barrier_place(&b));
assert_se(barrier_place(&b));
}),
*/
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));
}),
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));
*/
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));
*/
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));
}),
*/
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),
*/
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));
*/
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));
}),
*/
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));
}),
TEST_BARRIER_WAIT_SUCCESS(pid1),
({
- malarm(BASE_TIME * 10);
+ set_alarm(BASE_TIME * 10);
assert_se(barrier_place(&b));
assert_se(!barrier_sync(&b));
}),
*/
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));
}),
*/
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));