More clean-ups and cool template stuff
[invirt/third/libt4.git] / lock_tester.cc
index f4e68bd..ac9175b 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include "lang/verify.h"
-#include "tprintf.h"
+#include "threaded_log.h"
 #include <sys/types.h>
 #include <unistd.h>
 #include "lock.h"
 
-char tprintf_thread_prefix = 'c';
+char log_thread_prefix = 'c';
 
 // must be >= 2
 const int nt = 6; //XXX: lab1's rpc handlers are blocking. Since rpcs uses a thread pool of 10 threads, we cannot test more than 10 blocking rpc.
@@ -31,12 +31,10 @@ lock_protocol::lockid_t c = "3";
 int ct[256];
 std::mutex count_mutex;
 
-void
-check_grant(lock_protocol::lockid_t lid)
-{
+void check_grant(lock_protocol::lockid_t lid) {
     lock ml(count_mutex);
     int x = lid[0] & 0x0f;
-    if(ct[x] != 0){
+    if (ct[x] != 0) {
         fprintf(stderr, "error: server granted %s twice\n", lid.c_str());
         fprintf(stdout, "error: server granted %s twice\n", lid.c_str());
         exit(1);
@@ -44,22 +42,18 @@ check_grant(lock_protocol::lockid_t lid)
     ct[x] += 1;
 }
 
-void
-check_release(lock_protocol::lockid_t lid)
-{
+void check_release(lock_protocol::lockid_t lid) {
     lock ml(count_mutex);
     int x = lid[0] & 0x0f;
-    if(ct[x] != 1){
+    if (ct[x] != 1) {
         fprintf(stderr, "error: client released un-held lock %s\n",  lid.c_str());
         exit(1);
     }
     ct[x] -= 1;
 }
 
-void
-test1(void)
-{
-    tprintf ("acquire a release a acquire a release a\n");
+void test1(void) {
+    LOG_NONMEMBER("acquire a release a acquire a release a");
     lc[0]->acquire(a);
     check_grant(a);
     lc[0]->release(a);
@@ -69,7 +63,7 @@ test1(void)
     lc[0]->release(a);
     check_release(a);
 
-    tprintf ("acquire a acquire b release b release a\n");
+    LOG_NONMEMBER("acquire a acquire b release b release a");
     lc[0]->acquire(a);
     check_grant(a);
     lc[0]->acquire(b);
@@ -80,63 +74,51 @@ test1(void)
     check_release(a);
 }
 
-void *
-test2(int i) 
-{
-    tprintf ("test2: client %d acquire a release a\n", i);
+void test2(int i) {
+    LOG_NONMEMBER("test2: client " << i << " acquire a release a");
     lc[i]->acquire(a);
-    tprintf ("test2: client %d acquire done\n", i);
+    LOG_NONMEMBER("test2: client " << i << " acquire done");
     check_grant(a);
     sleep(1);
-    tprintf ("test2: client %d release\n", i);
+    LOG_NONMEMBER("test2: client " << i << " release");
     check_release(a);
     lc[i]->release(a);
-    tprintf ("test2: client %d release done\n", i);
-    return 0;
+    LOG_NONMEMBER("test2: client " << i << " release done");
 }
 
-void *
-test3(int i)
-{
-    tprintf ("test3: client %d acquire a release a concurrent\n", i);
+void test3(int i) {
+    LOG_NONMEMBER("test3: client " << i << " acquire a release a concurrent");
     for (int j = 0; j < 10; j++) {
         lc[i]->acquire(a);
         check_grant(a);
-        tprintf ("test3: client %d got lock\n", i);
+        LOG_NONMEMBER("test3: client " << i << " got lock");
         check_release(a);
         lc[i]->release(a);
     }
-    return 0;
 }
 
-void *
-test4(int i)
-{
-    tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i);
+void test4(int i) {
+    LOG_NONMEMBER("test4: thread " << i << " acquire a release a concurrent; same clnt");
     for (int j = 0; j < 10; j++) {
         lc[0]->acquire(a);
         check_grant(a);
-        tprintf ("test4: thread %d on client 0 got lock\n", i);
+        LOG_NONMEMBER("test4: thread " << i << " on client 0 got lock");
         check_release(a);
         lc[0]->release(a);
     }
-    return 0;
 }
 
-void *
-test5(int i)
-{
-    tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i);
+void test5(int i) {
+    LOG_NONMEMBER("test5: client " << i << " acquire a release a concurrent; same and diff clnt");
     for (int j = 0; j < 10; j++) {
         if (i < 5)  lc[0]->acquire(a);
         else  lc[1]->acquire(a);
         check_grant(a);
-        tprintf ("test5: client %d got lock\n", i);
+        LOG_NONMEMBER("test5: client " << i << " got lock");
         check_release(a);
         if (i < 5) lc[0]->release(a);
         else lc[1]->release(a);
     }
-    return 0;
 }
 
 int
@@ -149,7 +131,7 @@ main(int argc, char *argv[])
     setvbuf(stderr, NULL, _IONBF, 0);
     srandom((uint32_t)getpid());
 
-    if(argc < 2) {
+    if (argc < 2) {
         fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]);
         exit(1);
     }
@@ -158,20 +140,20 @@ main(int argc, char *argv[])
 
     if (argc > 2) {
         test = atoi(argv[2]);
-        if(test < 1 || test > 5){
-            tprintf("Test number must be between 1 and 5\n");
+        if (test < 1 || test > 5) {
+            LOG_NONMEMBER("Test number must be between 1 and 5");
             exit(1);
         }
     }
 
-    tprintf("cache lock client\n");
+    LOG_NONMEMBER("cache lock client");
     for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
 
-    if(!test || test == 1){
+    if (!test || test == 1) {
         test1();
     }
 
-    if(!test || test == 2){
+    if (!test || test == 2) {
         // test2
         for (int i = 0; i < nt; i++)
             th[i] = std::thread(test2, i);
@@ -179,36 +161,33 @@ main(int argc, char *argv[])
             th[i].join();
     }
 
-    if(!test || test == 3){
-        tprintf("test 3\n");
+    if (!test || test == 3) {
+        LOG_NONMEMBER("test 3");
 
-        // test3
         for (int i = 0; i < nt; i++)
             th[i] = std::thread(test3, i);
         for (int i = 0; i < nt; i++)
             th[i].join();
     }
 
-    if(!test || test == 4){
-        tprintf("test 4\n");
+    if (!test || test == 4) {
+        LOG_NONMEMBER("test 4");
 
-        // test 4
         for (int i = 0; i < 2; i++)
             th[i] = std::thread(test4, i);
         for (int i = 0; i < 2; i++)
             th[i].join();
     }
 
-    if(!test || test == 5){
-        tprintf("test 5\n");
+    if (!test || test == 5) {
+        LOG_NONMEMBER("test 5");
 
-        // test 5
         for (int i = 0; i < nt; i++)
             th[i] = std::thread(test5, i);
         for (int i = 0; i < nt; i++)
             th[i].join();
     }
 
-    tprintf ("%s: passed all tests successfully\n", argv[0]);
+    LOG_NONMEMBER(argv[0] << ": passed all tests successfully");
 
 }