X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/5d99dbf06a14904944f5593c63705934bdfdcfb7..c279db4240a3a3c30f069ab9dea8055cf94280da:/lock_tester.cc diff --git a/lock_tester.cc b/lock_tester.cc index f8e2196..fbdeeb4 100644 --- a/lock_tester.cc +++ b/lock_tester.cc @@ -4,48 +4,42 @@ #include "lock_client.h" #include -#include #include -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. -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 lock_client *lc[nt]; +static const char * a = "1"; +static const char * b = "2"; // 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]; -mutex count_mutex; +static int ct[256]; +static std::mutex * count_mutex; -void check_grant(lock_protocol::lockid_t lid) { - lock ml(count_mutex); +static void check_grant(lock_protocol::lockid_t lid) { + lock ml(*count_mutex); int x = lid[0] & 0x0f; if (ct[x] != 0) { - cout << "error: server granted " << lid << " twice" << endl; - cerr << "error: server granted " << lid << " twice" << endl; + LOG_NONMEMBER << "error: server granted " << lid << " twice"; exit(1); } ct[x] += 1; } -void check_release(lock_protocol::lockid_t lid) { - lock ml(count_mutex); +static void check_release(lock_protocol::lockid_t lid) { + lock ml(*count_mutex); int x = lid[0] & 0x0f; if (ct[x] != 1) { - cerr << "error: client released un-held lock " << lid << endl; + LOG_NONMEMBER << "error: client released un-held lock " << lid; exit(1); } ct[x] -= 1; } -void test1(void) { - LOG_NONMEMBER("acquire a release a acquire a release a"); +static void test1(void) { + LOG_NONMEMBER << "acquire a release a acquire a release a"; lc[0]->acquire(a); check_grant(a); lc[0]->release(a); @@ -55,7 +49,7 @@ void test1(void) { lc[0]->release(a); check_release(a); - LOG_NONMEMBER("acquire a acquire b release b release a"); + LOG_NONMEMBER << "acquire a acquire b release b release a"; lc[0]->acquire(a); check_grant(a); lc[0]->acquire(b); @@ -66,47 +60,47 @@ void test1(void) { check_release(a); } -void test2(int i) { - LOG_NONMEMBER("test2: client " << i << " acquire a release a"); +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"); + LOG_NONMEMBER << "test2: client " << i << " acquire done"; check_grant(a); - sleep(1); - LOG_NONMEMBER("test2: client " << i << " release"); + std::this_thread::sleep_for(milliseconds(100)); + 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 done"; } -void test3(int i) { - LOG_NONMEMBER("test3: client " << i << " acquire a release a concurrent"); +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); check_grant(a); - LOG_NONMEMBER("test3: client " << i << " got lock"); + LOG_NONMEMBER << "test3: client " << i << " got lock"; check_release(a); lc[i]->release(a); } } -void test4(int i) { - LOG_NONMEMBER("test4: thread " << i << " acquire a release a concurrent; same clnt"); +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); check_grant(a); - LOG_NONMEMBER("test4: thread " << i << " on client 0 got lock"); + LOG_NONMEMBER << "test4: thread " << i << " on client 0 got lock"; check_release(a); lc[0]->release(a); } } -void test5(int i) { - LOG_NONMEMBER("test5: client " << i << " acquire a release a concurrent; same and diff clnt"); +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); else lc[1]->acquire(a); check_grant(a); - LOG_NONMEMBER("test5: client " << i << " got lock"); + LOG_NONMEMBER << "test5: client " << i << " got lock"; check_release(a); if (i < 5) lc[0]->release(a); else lc[1]->release(a); @@ -116,29 +110,31 @@ void test5(int i) { int main(int argc, char *argv[]) { + global = new t4_state('c'); + count_mutex = new std::mutex(); + thread th[nt]; int test = 0; setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); - srandom((uint32_t)getpid()); if (argc < 2) { - cerr << "Usage: " << argv[0] << " [host:]port [test]" << endl; + LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]"; exit(1); } - dst = argv[1]; + string dst = argv[1]; if (argc > 2) { test = atoi(argv[2]); if (test < 1 || test > 5) { - LOG_NONMEMBER("Test number must be between 1 and 5"); + LOG_NONMEMBER << "Test number must be between 1 and 5"; exit(1); } } - LOG_NONMEMBER("cache lock client"); + LOG_NONMEMBER << "cache lock client"; for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst); if (!test || test == 1) { @@ -154,7 +150,7 @@ main(int argc, char *argv[]) } if (!test || test == 3) { - LOG_NONMEMBER("test 3"); + LOG_NONMEMBER << "test 3"; for (int i = 0; i < nt; i++) th[i] = thread(test3, i); @@ -163,7 +159,7 @@ main(int argc, char *argv[]) } if (!test || test == 4) { - LOG_NONMEMBER("test 4"); + LOG_NONMEMBER << "test 4"; for (int i = 0; i < 2; i++) th[i] = thread(test4, i); @@ -172,7 +168,7 @@ main(int argc, char *argv[]) } if (!test || test == 5) { - LOG_NONMEMBER("test 5"); + LOG_NONMEMBER << "test 5"; for (int i = 0; i < nt; i++) th[i] = thread(test5, i); @@ -180,6 +176,8 @@ main(int argc, char *argv[]) th[i].join(); } - LOG_NONMEMBER(argv[0] << ": passed all tests successfully"); + LOG_NONMEMBER << argv[0] << ": passed all tests successfully"; + for (int i = 0; i < nt; i++) + delete lc[i]; }