X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/5fd8cc8409d0efadc07dfe8d6774ad9ff477663d..dfe8486473094c0769fd1922329c3f0dfd8f43c0:/lock_server_cache_rsm.cc diff --git a/lock_server_cache_rsm.cc b/lock_server_cache_rsm.cc index 5149049..0e43ec5 100644 --- a/lock_server_cache_rsm.cc +++ b/lock_server_cache_rsm.cc @@ -9,85 +9,53 @@ #include "handle.h" #include "tprintf.h" #include "rpc/marshall.h" +#include "lock.h" + +using std::ostringstream; +using std::istringstream; +using std::vector; lock_state::lock_state(): held(false) { } -template -ostringstream & operator<<(ostringstream &o, const pair &d) { - o << "<" << d.first << "," << d.second << ">"; - return o; -} - -template -marshall & operator<<(marshall &m, const list &d) { - m << vector(d.begin(), d.end()); - return m; +lock_state::lock_state(const lock_state &other) { + *this = other; } -template -unmarshall & operator>>(unmarshall &u, list &d) { - vector v; - u >> v; - d.assign(v.begin(), v.end()); - return u; +lock_state& lock_state::operator=(const lock_state& o) { + held = o.held; + held_by = o.held_by; + wanted_by = o.wanted_by; + old_requests = o.old_requests; + return *this; } - template -marshall & operator<<(marshall &m, const pair &d) { - m << d.first; - m << d.second; - return m; -} - -template -unmarshall & operator>>(unmarshall &u, pair &d) { - u >> d.first; - u >> d.second; - return u; +ostringstream & operator<<(ostringstream &o, const pair &d) { + o << "<" << d.first << "," << d.second << ">"; + return o; } marshall & operator<<(marshall &m, const lock_state &d) { - m << d.held; - m << d.held_by; - m << d.wanted_by; - return m; + return m << d.held << d.held_by << d.wanted_by; } unmarshall & operator>>(unmarshall &u, lock_state &d) { - u >> d.held; - u >> d.held_by; - u >> d.wanted_by; - return u; + return u >> d.held >> d.held_by >> d.wanted_by; } - lock_state & lock_server_cache_rsm::get_lock_state(lock_protocol::lockid_t lid) { - ScopedLock sl(lock_table_lock); + lock sl(lock_table_lock); // by the semantics of map, this will create // the lock if it doesn't already exist return lock_table[lid]; } -static void *revokethread(void *x) { - lock_server_cache_rsm *sc = (lock_server_cache_rsm *) x; - sc->revoker(); - return 0; -} - -static void *retrythread(void *x) { - lock_server_cache_rsm *sc = (lock_server_cache_rsm *) x; - sc->retryer(); - return 0; -} - lock_server_cache_rsm::lock_server_cache_rsm(class rsm *_rsm) : rsm (_rsm) { - pthread_t th; - VERIFY(pthread_create(&th, NULL, &revokethread, (void *)this) == 0); - VERIFY(pthread_create(&th, NULL, &retrythread, (void *)this) == 0); + std::thread(&lock_server_cache_rsm::revoker, this).detach(); + std::thread(&lock_server_cache_rsm::retryer, this).detach(); rsm->set_state_transfer(this); } @@ -102,7 +70,7 @@ void lock_server_cache_rsm::revoker() { lock_state &st = get_lock_state(lid); holder held_by; { - ScopedLock sl(st.m); + lock sl(st.m); held_by = st.held_by; } @@ -130,7 +98,7 @@ void lock_server_cache_rsm::retryer() { lock_state &st = get_lock_state(lid); holder front; { - ScopedLock sl(st.m); + lock sl(st.m); if (st.wanted_by.empty()) continue; front = st.wanted_by.front(); @@ -155,7 +123,7 @@ int lock_server_cache_rsm::acquire(lock_protocol::lockid_t lid, string id, lock_ LOG_FUNC_ENTER_SERVER; holder h = holder(id, xid); lock_state &st = get_lock_state(lid); - ScopedLock sl(st.m); + lock sl(st.m); // deal with duplicated requests if (st.old_requests.count(id)) { @@ -212,7 +180,7 @@ int lock_server_cache_rsm::acquire(lock_protocol::lockid_t lid, string id, lock_ int lock_server_cache_rsm::release(lock_protocol::lockid_t lid, callback id, lock_protocol::xid_t xid, int &r) { LOG_FUNC_ENTER_SERVER; lock_state &st = get_lock_state(lid); - ScopedLock sl(st.m); + lock sl(st.m); if (st.held && st.held_by == holder(id, xid)) { st.held = false; LOG("Lock " << lid << " not held"); @@ -223,7 +191,7 @@ int lock_server_cache_rsm::release(lock_protocol::lockid_t lid, callback id, loc } string lock_server_cache_rsm::marshal_state() { - ScopedLock sl(lock_table_lock); + lock sl(lock_table_lock); marshall rep; rep << nacquire; rep << lock_table; @@ -231,7 +199,7 @@ string lock_server_cache_rsm::marshal_state() { } void lock_server_cache_rsm::unmarshal_state(string state) { - ScopedLock sl(lock_table_lock); + lock sl(lock_table_lock); unmarshall rep(state); rep >> nacquire; rep >> lock_table;