Cleanups
[invirt/third/libt4.git] / lock_client.cc
index d996b40..035d80b 100644 (file)
@@ -39,7 +39,7 @@ void lock_state::signal(std::thread::id who) {
         c[who].notify_one();
 }
 
-int lock_client::last_port = 0;
+unsigned int lock_client::last_port = 0;
 
 lock_state & lock_client::get_lock_state(lock_protocol::lockid_t lid) {
     lock sl(lock_table_lock);
@@ -53,11 +53,11 @@ lock_client::lock_client(string xdst, class lock_release_user *_lu) : lu(_lu) {
     make_sockaddr(xdst.c_str(), &dstsock);
     cl = new rpcc(dstsock);
     if (cl->bind() < 0) {
-        printf("lock_client: call bind\n");
+        LOG("lock_client: call bind");
     }
 
-    srand(time(NULL)^last_port);
-    rlock_port = ((rand()%32000) | (0x1 << 10));
+    srandom((uint32_t)time(NULL)^last_port);
+    rlock_port = ((random()%32000) | (0x1 << 10));
     const char *hname;
     // VERIFY(gethostname(hname, 100) == 0);
     hname = "127.0.0.1";
@@ -70,13 +70,13 @@ lock_client::lock_client(string xdst, class lock_release_user *_lu) : lu(_lu) {
     rlsrpc->reg(rlock_protocol::retry, &lock_client::retry_handler, this);
     {
         lock sl(xid_mutex);
-        xid = 0;
+        next_xid = 0;
     }
     rsmc = new rsm_client(xdst);
     releaser_thread = std::thread(&lock_client::releaser, this);
 }
 
-void lock_client::releaser() {
+void lock_client::releaser() [[noreturn]] {
     while (1) {
         lock_protocol::lockid_t lid;
         release_fifo.deq(&lid);
@@ -90,6 +90,8 @@ void lock_client::releaser() {
             sl.unlock();
             int r;
             rsmc->call(lock_protocol::release, r, lid, id, st.xid);
+            if (lu)
+                lu->dorelease(lid);
             sl.lock();
         }
         st.state = lock_state::none;
@@ -123,8 +125,8 @@ lock_protocol::status lock_client::acquire(lock_protocol::lockid_t lid) {
 
         if (st.state == lock_state::none || st.state == lock_state::retrying) {
             if (st.state == lock_state::none) {
-                lock sl(xid_mutex);
-                st.xid = xid++;
+                lock l(xid_mutex);
+                st.xid = next_xid++;
             }
             st.state = lock_state::acquiring;
             LOG("Lock " << lid << ": acquiring");
@@ -216,7 +218,7 @@ rlock_protocol::status lock_client::revoke_handler(int &, lock_protocol::lockid_
     return rlock_protocol::OK;
 }
 
-rlock_protocol::status lock_client::retry_handler(int &, lock_protocol::lockid_t lid, lock_protocol::xid_t xid) {
+rlock_protocol::status lock_client::retry_handler(int &, lock_protocol::lockid_t lid, lock_protocol::xid_t) {
     lock_state &st = get_lock_state(lid);
     lock sl(st.m);
     VERIFY(st.state == lock_state::acquiring);