Fixed two major bugs in paxos.cc.
[invirt/third/libt4.git] / lock_client.cc
index 035d80b..0b071f5 100644 (file)
@@ -1,30 +1,11 @@
 // RPC stubs for clients to talk to lock_server, and cache the locks.
 
 #include "lock_client.h"
-#include "rpc/rpc.h"
-#include <sstream>
-#include <iostream>
-#include <algorithm>
-#include <stdio.h>
-#include "tprintf.h"
 #include <arpa/inet.h>
 
-#include "rsm_client.h"
-#include "lock.h"
-
-using std::ostringstream;
-
-lock_state::lock_state():
-    state(none)
-{
-}
-
-void lock_state::wait() {
-    auto self = std::this_thread::get_id();
-    {
-        adopt_lock ml(m);
-        c[self].wait(ml);
-    }
+void lock_state::wait(lock & mutex_lock) {
+    auto self = this_thread::get_id();
+    c[self].wait(mutex_lock);
     c.erase(self);
 }
 
@@ -34,46 +15,34 @@ void lock_state::signal() {
         c.begin()->second.notify_one();
 }
 
-void lock_state::signal(std::thread::id who) {
+void lock_state::signal(thread::id who) {
     if (c.count(who))
         c[who].notify_one();
 }
 
-unsigned int lock_client::last_port = 0;
+typedef map<lock_protocol::lockid_t, lock_state> lock_map;
+
+in_port_t lock_client::last_port = 0;
 
 lock_state & lock_client::get_lock_state(lock_protocol::lockid_t lid) {
     lock sl(lock_table_lock);
-    // by the semantics of std::map, this will create
-    // the lock if it doesn't already exist
-    return lock_table[lid];
+    return lock_table[lid]; // creates the lock if it doesn't already exist
 }
 
-lock_client::lock_client(string xdst, class lock_release_user *_lu) : lu(_lu) {
-    sockaddr_in dstsock;
-    make_sockaddr(xdst.c_str(), &dstsock);
-    cl = new rpcc(dstsock);
-    if (cl->bind() < 0) {
+lock_client::lock_client(string xdst, lock_release_user *_lu) : lu(_lu), next_xid(0) {
+    cl = new rpcc(xdst);
+    if (cl->bind() < 0)
         LOG("lock_client: call bind");
-    }
 
     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";
-    ostringstream host;
-    host << hname << ":" << rlock_port;
-    id = host.str();
+    id = "127.0.0.1:" + to_string(rlock_port);
     last_port = rlock_port;
     rpcs *rlsrpc = new rpcs(rlock_port);
     rlsrpc->reg(rlock_protocol::revoke, &lock_client::revoke_handler, this);
     rlsrpc->reg(rlock_protocol::retry, &lock_client::retry_handler, this);
-    {
-        lock sl(xid_mutex);
-        next_xid = 0;
-    }
     rsmc = new rsm_client(xdst);
-    releaser_thread = std::thread(&lock_client::releaser, this);
+    releaser_thread = thread(&lock_client::releaser, this);
 }
 
 void lock_client::releaser() [[noreturn]] {
@@ -103,7 +72,7 @@ void lock_client::releaser() [[noreturn]] {
 int lock_client::stat(lock_protocol::lockid_t lid) {
     VERIFY(0);
     int r;
-    lock_protocol::status ret = cl->call(lock_protocol::stat, r, cl->id(), lid);
+    auto ret = (lock_protocol::status)cl->call(lock_protocol::stat, r, cl->id(), lid);
     VERIFY (ret == lock_protocol::OK);
     return r;
 }
@@ -111,7 +80,7 @@ int lock_client::stat(lock_protocol::lockid_t lid) {
 lock_protocol::status lock_client::acquire(lock_protocol::lockid_t lid) {
     lock_state &st = get_lock_state(lid);
     lock sl(st.m);
-    auto self = std::this_thread::get_id();
+    auto self = this_thread::get_id();
 
     // check for reentrancy
     VERIFY(st.state != lock_state::locked || st.held_by != self);
@@ -134,7 +103,7 @@ lock_protocol::status lock_client::acquire(lock_protocol::lockid_t lid) {
             {
                 sl.unlock();
                 int r;
-                result = rsmc->call(lock_protocol::acquire, r, lid, id, st.xid);
+                result = (lock_protocol::status)rsmc->call(lock_protocol::acquire, r, lid, id, st.xid);
                 sl.lock();
             }
             LOG("acquire returned " << result);
@@ -165,7 +134,7 @@ lock_protocol::status lock_client::acquire(lock_protocol::lockid_t lid) {
         }
 
         LOG("waiting...");
-        st.wait();
+        st.wait(sl);
         LOG("wait ended");
     }
 
@@ -176,7 +145,7 @@ lock_protocol::status lock_client::acquire(lock_protocol::lockid_t lid) {
 lock_protocol::status lock_client::release(lock_protocol::lockid_t lid) {
     lock_state &st = get_lock_state(lid);
     lock sl(st.m);
-    auto self = std::this_thread::get_id();
+    auto self = this_thread::get_id();
     VERIFY(st.state == lock_state::locked && st.held_by == self);
     st.state = lock_state::free;
     LOG("Lock " << lid << ": free");
@@ -241,7 +210,7 @@ t4_status t4_lock_client_acquire(t4_lock_client *client, t4_lockid_t lid) {
 }
 
 t4_status t4_lock_client_release(t4_lock_client *client, t4_lockid_t lid) {
-    return ((lock_client *)client)->acquire(lid);
+    return ((lock_client *)client)->release(lid);
 }
 
 t4_status t4_lock_client_stat(t4_lock_client *client, t4_lockid_t lid) {