X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/5fd8cc8409d0efadc07dfe8d6774ad9ff477663d..0989f6feac9c8e83847165c4abee5273463eaa63:/lock_tester.cc diff --git a/lock_tester.cc b/lock_tester.cc index dd7c07b..d063cdc 100644 --- a/lock_tester.cc +++ b/lock_tester.cc @@ -4,8 +4,7 @@ #include "lock_protocol.h" #include "lock_client.h" -#include "rpc.h" -#include "jsl_log.h" +#include "rpc/rpc.h" #include #include #include @@ -13,46 +12,49 @@ #include "lang/verify.h" #include "lock_client_cache_rsm.h" #include "tprintf.h" +#include +#include +#include "lock.h" char tprintf_thread_prefix = 'c'; // must be >= 2 -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. +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_cache_rsm **lc = new lock_client_cache_rsm * [nt]; -lock_protocol::lockid_t a = 1; -lock_protocol::lockid_t b = 2; -lock_protocol::lockid_t c = 3; +lock_protocol::lockid_t a = "1"; +lock_protocol::lockid_t b = "2"; +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. int ct[256]; -pthread_mutex_t count_mutex; +std::mutex count_mutex; void check_grant(lock_protocol::lockid_t lid) { - ScopedLock ml(&count_mutex); - int x = lid & 0xff; - if(ct[x] != 0){ - fprintf(stderr, "error: server granted %016llx twice\n", lid); - fprintf(stdout, "error: server granted %016llx twice\n", lid); - exit(1); - } - ct[x] += 1; + 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()); + exit(1); + } + ct[x] += 1; } void check_release(lock_protocol::lockid_t lid) { - ScopedLock ml(&count_mutex); - int x = lid & 0xff; - if(ct[x] != 1){ - fprintf(stderr, "error: client released un-held lock %016llx\n", lid); - exit(1); - } - 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()); + exit(1); + } + ct[x] -= 1; } void @@ -82,159 +84,151 @@ test1(void) void * test2(void *x) { - int i = * (int *) x; - - tprintf ("test2: client %d acquire a release a\n", i); - lc[i]->acquire(a); - tprintf ("test2: client %d acquire done\n", i); - check_grant(a); - sleep(1); - tprintf ("test2: client %d release\n", i); - check_release(a); - lc[i]->release(a); - tprintf ("test2: client %d release done\n", i); - return 0; -} + int i = * (int *) x; -void * -test3(void *x) -{ - int i = * (int *) x; - - tprintf ("test3: client %d acquire a release a concurrent\n", i); - for (int j = 0; j < 10; j++) { + tprintf ("test2: client %d acquire a release a\n", i); lc[i]->acquire(a); + tprintf ("test2: client %d acquire done\n", i); check_grant(a); - tprintf ("test3: client %d got lock\n", i); + sleep(1); + tprintf ("test2: client %d release\n", i); check_release(a); lc[i]->release(a); - } - return 0; + tprintf ("test2: client %d release done\n", i); + return 0; } void * -test4(void *x) +test3(void *x) { - int i = * (int *) x; + int i = * (int *) x; + + tprintf ("test3: client %d acquire a release a concurrent\n", i); + for (int j = 0; j < 10; j++) { + lc[i]->acquire(a); + check_grant(a); + tprintf ("test3: client %d got lock\n", i); + check_release(a); + lc[i]->release(a); + } + return 0; +} - tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i); - 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); - check_release(a); - lc[0]->release(a); - } - return 0; +void * +test4(void *x) +{ + int i = * (int *) x; + + tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i); + 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); + check_release(a); + lc[0]->release(a); + } + return 0; } void * test5(void *x) { - int i = * (int *) x; - - tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i); - 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); - check_release(a); - if (i < 5) lc[0]->release(a); - else lc[1]->release(a); - } - return 0; + int i = * (int *) x; + + tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i); + 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); + check_release(a); + if (i < 5) lc[0]->release(a); + else lc[1]->release(a); + } + return 0; } int main(int argc, char *argv[]) { - int r; - pthread_t th[nt]; + std::thread th[nt]; int test = 0; setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); srandom(getpid()); - //jsl_set_debug(2); - if(argc < 2) { - fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]); - exit(1); + fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]); + exit(1); } dst = argv[1]; if (argc > 2) { - test = atoi(argv[2]); - if(test < 1 || test > 5){ - tprintf("Test number must be between 1 and 5\n"); - exit(1); - } + test = atoi(argv[2]); + if(test < 1 || test > 5){ + tprintf("Test number must be between 1 and 5\n"); + exit(1); + } } - VERIFY(pthread_mutex_init(&count_mutex, NULL) == 0); tprintf("cache lock client\n"); for (int i = 0; i < nt; i++) lc[i] = new lock_client_cache_rsm(dst); if(!test || test == 1){ - test1(); + test1(); } if(!test || test == 2){ - // test2 - for (int i = 0; i < nt; i++) { - int *a = new int (i); - r = pthread_create(&th[i], NULL, test2, (void *) a); - VERIFY (r == 0); - } - for (int i = 0; i < nt; i++) { - pthread_join(th[i], NULL); - } + // test2 + for (int i = 0; i < nt; i++) { + int *a = new int (i); + th[i] = std::thread(test2, a); + } + for (int i = 0; i < nt; i++) { + th[i].join(); + } } if(!test || test == 3){ - tprintf("test 3\n"); - - // test3 - for (int i = 0; i < nt; i++) { - int *a = new int (i); - r = pthread_create(&th[i], NULL, test3, (void *) a); - VERIFY (r == 0); - } - for (int i = 0; i < nt; i++) { - pthread_join(th[i], NULL); - } + tprintf("test 3\n"); + + // test3 + for (int i = 0; i < nt; i++) { + int *a = new int (i); + th[i] = std::thread(test3, a); + } + for (int i = 0; i < nt; i++) { + th[i].join(); + } } if(!test || test == 4){ - tprintf("test 4\n"); - - // test 4 - for (int i = 0; i < 2; i++) { - int *a = new int (i); - r = pthread_create(&th[i], NULL, test4, (void *) a); - VERIFY (r == 0); - } - for (int i = 0; i < 2; i++) { - pthread_join(th[i], NULL); - } + tprintf("test 4\n"); + + // test 4 + for (int i = 0; i < 2; i++) { + int *a = new int (i); + th[i] = std::thread(test4, a); + } + for (int i = 0; i < 2; i++) { + th[i].join(); + } } if(!test || test == 5){ - tprintf("test 5\n"); - - // test 5 - - for (int i = 0; i < nt; i++) { - int *a = new int (i); - r = pthread_create(&th[i], NULL, test5, (void *) a); - VERIFY (r == 0); - } - for (int i = 0; i < nt; i++) { - pthread_join(th[i], NULL); - } + tprintf("test 5\n"); + + // test 5 + + for (int i = 0; i < nt; i++) { + int *a = new int (i); + th[i] = std::thread(test5, a); + } + for (int i = 0; i < nt; i++) { + th[i].join(); + } } tprintf ("%s: passed all tests successfully\n", argv[0]);