#include <arpa/inet.h>
#include <unistd.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.
-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";
+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.
static int ct[256];
-static std::mutex count_mutex;
+static std::mutex * count_mutex;
static void check_grant(lock_protocol::lockid_t lid) {
- lock ml(count_mutex);
+ lock ml(*count_mutex);
int x = lid[0] & 0x0f;
if (ct[x] != 0) {
LOG_NONMEMBER << "error: server granted " << lid << " twice";
}
static void check_release(lock_protocol::lockid_t lid) {
- lock ml(count_mutex);
+ lock ml(*count_mutex);
int x = lid[0] & 0x0f;
if (ct[x] != 1) {
LOG_NONMEMBER << "error: client released un-held lock " << lid;
lc[i]->acquire(a);
LOG_NONMEMBER << "test2: client " << i << " acquire done";
check_grant(a);
- usleep(100000);
+ std::this_thread::sleep_for(milliseconds(100));
LOG_NONMEMBER << "test2: client " << i << " release";
check_release(a);
lc[i]->release(a);
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) {
LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]";
exit(1);
}
- dst = argv[1];
+ string dst = argv[1];
if (argc > 2) {
test = atoi(argv[2]);
LOG_NONMEMBER << argv[0] << ": passed all tests successfully";
+ for (int i = 0; i < nt; i++)
+ delete lc[i];
}