improve tester
[~madcoder/pwqr.git] / test / test.c
index b4f2f67..242ba50 100644 (file)
@@ -27,6 +27,7 @@
 #include <sched.h>
 #include <stddef.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <sys/ioctl.h>
 #include <sys/poll.h>
 #include <sys/time.h>
@@ -74,15 +75,34 @@ static int pwqr_ctl(int fd, int op, int val, void *uaddr)
 
 static int pwqr_fd_g = -1;
 static int pi_g;
+static int state_g;
 
-static void *thr_main(void *unused)
+#define trace(fmt, ...)   fprintf(stderr, fmt"\n", ##__VA_ARGS__)
+
+static void *thr_main(void *_who)
 {
+    int who = (long)_who;
+
     if (pwqr_ctl(pwqr_fd_g, PWQR_CTL_REGISTER, 0, NULL) < 0)
-        err(-1, "pwqr_ctl(PWQR_CTL_REGISTER)");
+        err(-1, "%d: pwqr_ctl(PWQR_CTL_REGISTER)", who);
+
+    /* busy-loop */
+    trace("%d: loop1", who);
+    do {
+        pthread_testcancel();
+    } while (!state_g);
+
+    trace("%d: tries to park", who);
+    if (pwqr_ctl(pwqr_fd_g, PWQR_CTL_PARK, 0, NULL) < 0)
+        trace("pwqr_ctl(PWQR_CTL_PARK)");
+    trace("%d: unparked", who);
 
     /* busy-loop */
-    for (;;)
+    trace("%d: loop2", who);
+    do {
         pthread_testcancel();
+    } while (state_g);
+
     return NULL;
 }
 
@@ -98,6 +118,8 @@ int main(void)
     struct timeval start, tv;
     int rc;
 
+    setlinebuf(stderr);
+
     pwqr_fd_g = pwqr_create(PWQR_FL_NONBLOCK);
     if (pwqr_fd_g < 0)
         err(-1, "pwqr_create");
@@ -107,7 +129,7 @@ int main(void)
         err(-1, "pwqr_ctl(PWQR_CTL_GET_CONC)");
 
     for (int i = 0; i < pi_g + 1; i++) {
-        pthread_create(&tids[i], NULL, thr_main, NULL);
+        pthread_create(&tids[i], NULL, thr_main, (void *)(long)i);
     }
 
     pfd.fd     = pwqr_fd_g;
@@ -126,10 +148,11 @@ int main(void)
                     warn("read");
                 } else {
                     gettimeofday(&tv, NULL);
-                    warnx("[%06jd.%06jd] overcommit of %d",
+                    trace("[%06jd.%06jd] overcommit of %d",
                           tv_diff(&tv, &start) / 1000000,
                           tv_diff(&tv, &start) % 1000000, rc);
                 }
+                state_g = 1;
             }
             if (pfd.revents & POLLHUP) {
                 errx(-1, "poll returned POLLHUP");