Clean-ups and fixes to compile with more warnings enabled and with g++.
[invirt/third/libt4.git] / lock_tester.cc
index ac9175b..5e615ca 100644 (file)
@@ -2,57 +2,47 @@
 // Lock server tester
 //
 
 // Lock server tester
 //
 
-#include "lock_protocol.h"
 #include "lock_client.h"
 #include "lock_client.h"
-#include "rpc/rpc.h"
 #include <arpa/inet.h>
 #include <arpa/inet.h>
-#include <vector>
-#include <stdlib.h>
-#include <stdio.h>
-#include "lang/verify.h"
-#include "threaded_log.h"
-#include <sys/types.h>
 #include <unistd.h>
 #include <unistd.h>
-#include "lock.h"
 
 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.
 
 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.
-std::string dst;
-lock_client **lc = new lock_client * [nt];
-lock_protocol::lockid_t a = "1";
-lock_protocol::lockid_t b = "2";
-lock_protocol::lockid_t c = "3";
+static string dst;
+static lock_client **lc = new lock_client * [nt];
+static lock_protocol::lockid_t a = "1";
+static lock_protocol::lockid_t b = "2";
+static lock_protocol::lockid_t c = "3";
 
 // check_grant() and check_release() check that the lock server
 // doesn't grant the same lock to both clients.
 // it assumes that lock names are distinct in the first byte.
 
 // check_grant() and check_release() check that the lock server
 // doesn't grant the same lock to both clients.
 // it assumes that lock names are distinct in the first byte.
-int ct[256];
-std::mutex count_mutex;
+static int ct[256];
+static mutex count_mutex;
 
 
-void check_grant(lock_protocol::lockid_t lid) {
+static void check_grant(lock_protocol::lockid_t lid) {
     lock ml(count_mutex);
     int x = lid[0] & 0x0f;
     if (ct[x] != 0) {
     lock ml(count_mutex);
     int x = lid[0] & 0x0f;
     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());
+        LOG_NONMEMBER("error: server granted " << lid << " twice");
         exit(1);
     }
     ct[x] += 1;
 }
 
         exit(1);
     }
     ct[x] += 1;
 }
 
-void check_release(lock_protocol::lockid_t lid) {
+static void check_release(lock_protocol::lockid_t lid) {
     lock ml(count_mutex);
     int x = lid[0] & 0x0f;
     if (ct[x] != 1) {
     lock ml(count_mutex);
     int x = lid[0] & 0x0f;
     if (ct[x] != 1) {
-        fprintf(stderr, "error: client released un-held lock %s\n",  lid.c_str());
+        LOG_NONMEMBER("error: client released un-held lock " << lid);
         exit(1);
     }
     ct[x] -= 1;
 }
 
         exit(1);
     }
     ct[x] -= 1;
 }
 
-void test1(void) {
+static void test1(void) {
     LOG_NONMEMBER("acquire a release a acquire a release a");
     lc[0]->acquire(a);
     check_grant(a);
     LOG_NONMEMBER("acquire a release a acquire a release a");
     lc[0]->acquire(a);
     check_grant(a);
@@ -74,19 +64,19 @@ void test1(void) {
     check_release(a);
 }
 
     check_release(a);
 }
 
-void test2(int i) {
+static void test2(int i) {
     LOG_NONMEMBER("test2: client " << i << " acquire a release a");
     lc[i]->acquire(a);
     LOG_NONMEMBER("test2: client " << i << " acquire done");
     check_grant(a);
     LOG_NONMEMBER("test2: client " << i << " acquire a release a");
     lc[i]->acquire(a);
     LOG_NONMEMBER("test2: client " << i << " acquire done");
     check_grant(a);
-    sleep(1);
+    usleep(100000);
     LOG_NONMEMBER("test2: client " << i << " release");
     check_release(a);
     lc[i]->release(a);
     LOG_NONMEMBER("test2: client " << i << " release done");
 }
 
     LOG_NONMEMBER("test2: client " << i << " release");
     check_release(a);
     lc[i]->release(a);
     LOG_NONMEMBER("test2: client " << i << " release done");
 }
 
-void test3(int i) {
+static 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);
     LOG_NONMEMBER("test3: client " << i << " acquire a release a concurrent");
     for (int j = 0; j < 10; j++) {
         lc[i]->acquire(a);
@@ -97,7 +87,7 @@ void test3(int i) {
     }
 }
 
     }
 }
 
-void test4(int i) {
+static 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);
     LOG_NONMEMBER("test4: thread " << i << " acquire a release a concurrent; same clnt");
     for (int j = 0; j < 10; j++) {
         lc[0]->acquire(a);
@@ -108,7 +98,7 @@ void test4(int i) {
     }
 }
 
     }
 }
 
-void test5(int i) {
+static 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);
     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);
@@ -124,7 +114,7 @@ void test5(int i) {
 int
 main(int argc, char *argv[])
 {
 int
 main(int argc, char *argv[])
 {
-    std::thread th[nt];
+    thread th[nt];
     int test = 0;
 
     setvbuf(stdout, NULL, _IONBF, 0);
     int test = 0;
 
     setvbuf(stdout, NULL, _IONBF, 0);
@@ -132,7 +122,7 @@ main(int argc, char *argv[])
     srandom((uint32_t)getpid());
 
     if (argc < 2) {
     srandom((uint32_t)getpid());
 
     if (argc < 2) {
-        fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]);
+        LOG_NONMEMBER("Usage: " << argv[0] << " [host:]port [test]");
         exit(1);
     }
 
         exit(1);
     }
 
@@ -156,7 +146,7 @@ main(int argc, char *argv[])
     if (!test || test == 2) {
         // test2
         for (int i = 0; i < nt; i++)
     if (!test || test == 2) {
         // test2
         for (int i = 0; i < nt; i++)
-            th[i] = std::thread(test2, i);
+            th[i] = thread(test2, i);
         for (int i = 0; i < nt; i++)
             th[i].join();
     }
         for (int i = 0; i < nt; i++)
             th[i].join();
     }
@@ -165,7 +155,7 @@ main(int argc, char *argv[])
         LOG_NONMEMBER("test 3");
 
         for (int i = 0; i < nt; i++)
         LOG_NONMEMBER("test 3");
 
         for (int i = 0; i < nt; i++)
-            th[i] = std::thread(test3, i);
+            th[i] = thread(test3, i);
         for (int i = 0; i < nt; i++)
             th[i].join();
     }
         for (int i = 0; i < nt; i++)
             th[i].join();
     }
@@ -174,7 +164,7 @@ main(int argc, char *argv[])
         LOG_NONMEMBER("test 4");
 
         for (int i = 0; i < 2; i++)
         LOG_NONMEMBER("test 4");
 
         for (int i = 0; i < 2; i++)
-            th[i] = std::thread(test4, i);
+            th[i] = thread(test4, i);
         for (int i = 0; i < 2; i++)
             th[i].join();
     }
         for (int i = 0; i < 2; i++)
             th[i].join();
     }
@@ -183,7 +173,7 @@ main(int argc, char *argv[])
         LOG_NONMEMBER("test 5");
 
         for (int i = 0; i < nt; i++)
         LOG_NONMEMBER("test 5");
 
         for (int i = 0; i < nt; i++)
-            th[i] = std::thread(test5, i);
+            th[i] = thread(test5, i);
         for (int i = 0; i < nt; i++)
             th[i].join();
     }
         for (int i = 0; i < nt; i++)
             th[i].join();
     }