chiark / gitweb /
udev: support ENV{}=="" global property matches
[elogind.git] / src / test / test-barrier.c
index 640e50867921fc6c396c0a6e2745d26998362140..cb75f7314d98d6d700c3c1a4b559ba64e63a3eeb 100644 (file)
  * 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);                     \
+                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 +86,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 +113,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 +143,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 +169,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 +192,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 +202,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 +217,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 +238,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 +247,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 +260,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 +271,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 +286,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 +304,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 +320,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 +336,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 +352,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 +368,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 +389,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 +403,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 +425,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));