#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
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
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);
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);
}
VERIFY((int)rep.size() == arg);
}
- return 0;
}
-void *
+void
client3(void *xx)
{
rpcc *c = (rpcc *) xx;
int ret = c->call(24, i, rep, rpcc::to(3000));
VERIFY(ret == rpc_const::timeout_failure || rep == i+2);
}
- return 0;
}
// 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");
}
void
lossy_test()
{
- int ret;
-
printf("start lossy_test ...");
VERIFY(setenv("RPC_LOSSY", "5", 1) == 0);
}
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);
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");
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");
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();
}