Major clean-ups. Migrating to C++11.
[invirt/third/libt4.git] / rpc / rpctest.cc
index 74c61d1..d90e494 100644 (file)
@@ -7,8 +7,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <getopt.h>
+#include <sys/types.h>
+#include <unistd.h>
 #include "jsl_log.h"
-#include "gettime.h"
 #include "lang/verify.h"
 
 #define NUM_CL 2
@@ -17,7 +18,6 @@ rpcs *server;  // server rpc object
 rpcc *clients[NUM_CL];  // client rpc object
 struct sockaddr_in dst; //server's ip address
 int port;
-pthread_attr_t attr;
 
 // server-side handlers. they must be methods of some class
 // to simplify rpcs::reg(). a server process can have handlers
@@ -110,12 +110,11 @@ testmarshall()
        VERIFY(i1==i && l1==l && s1==s);
 }
 
-void *
-client1(void *xx)
+void
+client1(int cl)
 {
-
        // test concurrency.
-       int which_cl = ((unsigned long) xx ) % NUM_CL;
+       int which_cl = ((unsigned long) cl ) % NUM_CL;
 
        for(int i = 0; i < 100; i++){
                int arg = (random() % 2000);
@@ -135,25 +134,22 @@ client1(void *xx)
                int arg = (random() % 1000);
                int rep;
 
-               struct timespec start,end;
-               clock_gettime(CLOCK_REALTIME, &start);
+               auto start = std::chrono::steady_clock::now();
 
                int ret = clients[which_cl]->call(which ? 23 : 24, arg, rep);
-               clock_gettime(CLOCK_REALTIME, &end);
-               int diff = diff_timespec(end, start);
+               auto end = std::chrono::steady_clock::now();
+               int diff = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
                if (ret != 0)
                        printf("%d ms have elapsed!!!\n", diff);
                VERIFY(ret == 0);
                VERIFY(rep == (which ? arg+1 : arg+2));
        }
-
-       return 0;
 }
 
-void *
-client2(void *xx)
+void
+client2(int cl)
 {
-       int which_cl = ((unsigned long) xx ) % NUM_CL;
+       int which_cl = ((unsigned long) cl ) % NUM_CL;
 
        time_t t1;
        time(&t1);
@@ -168,10 +164,9 @@ client2(void *xx)
                }
                VERIFY((int)rep.size() == arg);
        }
-       return 0;
 }
 
-void *
+void
 client3(void *xx)
 {
        rpcc *c = (rpcc *) xx;
@@ -181,7 +176,6 @@ client3(void *xx)
                int ret = c->call(24, i, rep, rpcc::to(3000));
                VERIFY(ret == rpc_const::timeout_failure || rep == i+2);
        }
-       return 0;
 }
 
 
@@ -265,18 +259,15 @@ concurrent_test(int nt)
        // create threads that make lots of calls in parallel,
        // to test thread synchronization for concurrent calls
        // and dispatches.
-       int ret;
-
        printf("start concurrent_test (%d threads) ...", nt);
 
-       pthread_t th[nt];
+    std::vector<std::thread> th(nt);
        for(int i = 0; i < nt; i++){
-               ret = pthread_create(&th[i], &attr, client1, (void *) (uintptr_t)i);
-               VERIFY(ret == 0);
+        th[i] = std::thread(client1, i);
        }
 
        for(int i = 0; i < nt; i++){
-               VERIFY(pthread_join(th[i], NULL) == 0);
+        th[i].join();
        }
        printf(" OK\n");
 }
@@ -284,8 +275,6 @@ concurrent_test(int nt)
 void 
 lossy_test()
 {
-       int ret;
-
        printf("start lossy_test ...");
        VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
 
@@ -301,13 +290,12 @@ lossy_test()
        }
 
        int nt = 1;
-       pthread_t th[nt];
+    std::vector<std::thread> th(nt);
        for(int i = 0; i < nt; i++){
-               ret = pthread_create(&th[i], &attr, client2, (void *) (uintptr_t)i);
-               VERIFY(ret == 0);
+        th[i] = std::thread(client2, i);
        }
        for(int i = 0; i < nt; i++){
-               VERIFY(pthread_join(th[i], NULL) == 0);
+        th[i].join();
        }
        printf(".. OK\n");
        VERIFY(setenv("RPC_LOSSY", "0", 1) == 0);
@@ -350,17 +338,15 @@ failure_test()
 
 
        int nt = 10;
-       int ret;
        printf("   -- concurrent test on new rpc client w/ %d threads ..", nt);
 
-       pthread_t th[nt];
+    std::vector<std::thread> th(nt);
        for(int i = 0; i < nt; i++){
-               ret = pthread_create(&th[i], &attr, client3, (void *) client);
-               VERIFY(ret == 0);
+        th[i] = std::thread(client3, client);
        }
 
        for(int i = 0; i < nt; i++){
-               VERIFY(pthread_join(th[i], NULL) == 0);
+        th[i].join();
        }
        printf("ok\n");
 
@@ -374,12 +360,11 @@ failure_test()
 
        printf("   -- concurrent test on new client and server w/ %d threads ..", nt);
        for(int i = 0; i < nt; i++){
-               ret = pthread_create(&th[i], &attr, client3, (void *)client);
-               VERIFY(ret == 0);
+        th[i] = std::thread(client3, client);
        }
 
        for(int i = 0; i < nt; i++){
-               VERIFY(pthread_join(th[i], NULL) == 0);
+        th[i].join();
        }
        printf("ok\n");
 
@@ -434,12 +419,8 @@ main(int argc, char *argv[])
 
        testmarshall();
 
-       pthread_attr_init(&attr);
-       // set stack size to 32K, so we don't run out of memory
-       pthread_attr_setstacksize(&attr, 32*1024);
-
        if (isserver) {
-               printf("starting server on port %d RPC_HEADER_SZ %d\n", port, RPC_HEADER_SZ);
+               printf("starting server on port %d RPC_HEADER_SZ %d\n", port, (int)RPC_HEADER_SZ);
                startserver();
        }