1 // RPC stubs for clients to talk to lock_server, and cache the locks
2 // see lock_client.cache.h for protocol details.
4 #include "lock_client_cache_rsm.h"
12 #include "rsm_client.h"
15 using std::ostringstream;
17 lock_state::lock_state():
22 void lock_state::wait() {
23 auto self = std::this_thread::get_id();
31 void lock_state::signal() {
33 if (c.begin() != c.end())
34 c.begin()->second.notify_one();
37 void lock_state::signal(std::thread::id who) {
42 int lock_client_cache_rsm::last_port = 0;
44 lock_state & lock_client_cache_rsm::get_lock_state(lock_protocol::lockid_t lid) {
45 lock sl(lock_table_lock);
46 // by the semantics of std::map, this will create
47 // the lock if it doesn't already exist
48 return lock_table[lid];
51 lock_client_cache_rsm::lock_client_cache_rsm(string xdst, class lock_release_user *_lu) : lock_client(xdst), lu(_lu) {
52 srand(time(NULL)^last_port);
53 rlock_port = ((rand()%32000) | (0x1 << 10));
55 // VERIFY(gethostname(hname, 100) == 0);
58 host << hname << ":" << rlock_port;
60 last_port = rlock_port;
61 rpcs *rlsrpc = new rpcs(rlock_port);
62 rlsrpc->reg(rlock_protocol::revoke, this, &lock_client_cache_rsm::revoke_handler);
63 rlsrpc->reg(rlock_protocol::retry, this, &lock_client_cache_rsm::retry_handler);
68 rsmc = new rsm_client(xdst);
69 releaser_thread = std::thread(&lock_client_cache_rsm::releaser, this);
72 void lock_client_cache_rsm::releaser() {
74 lock_protocol::lockid_t lid;
75 release_fifo.deq(&lid);
76 LOG("Releaser: " << lid);
78 lock_state &st = get_lock_state(lid);
80 VERIFY(st.state == lock_state::locked && st.held_by == releaser_thread.get_id());
81 st.state = lock_state::releasing;
85 rsmc->call(lock_protocol::release, r, lid, id, st.xid);
88 st.state = lock_state::none;
89 LOG("Lock " << lid << ": none");
94 lock_protocol::status lock_client_cache_rsm::acquire(lock_protocol::lockid_t lid) {
95 lock_state &st = get_lock_state(lid);
97 auto self = std::this_thread::get_id();
99 // check for reentrancy
100 VERIFY(st.state != lock_state::locked || st.held_by != self);
101 VERIFY(find(st.wanted_by.begin(), st.wanted_by.end(), self) == st.wanted_by.end());
103 st.wanted_by.push_back(self);
106 if (st.state != lock_state::free)
107 LOG("Lock " << lid << ": not free");
109 if (st.state == lock_state::none || st.state == lock_state::retrying) {
110 if (st.state == lock_state::none) {
114 st.state = lock_state::acquiring;
115 LOG("Lock " << lid << ": acquiring");
116 lock_protocol::status result;
120 result = rsmc->call(lock_protocol::acquire, r, lid, id, st.xid);
123 LOG("acquire returned " << result);
124 if (result == lock_protocol::OK) {
125 st.state = lock_state::free;
126 LOG("Lock " << lid << ": free");
130 VERIFY(st.wanted_by.size() != 0);
131 if (st.state == lock_state::free) {
133 auto front = st.wanted_by.front();
134 if (front == releaser_thread.get_id()) {
135 st.wanted_by.pop_front();
136 st.state = lock_state::locked;
137 st.held_by = releaser_thread.get_id();
138 LOG("Queuing " << lid << " for release");
139 release_fifo.enq(lid);
140 } else if (front == self) {
141 st.wanted_by.pop_front();
142 st.state = lock_state::locked;
155 LOG("Lock " << lid << ": locked");
156 return lock_protocol::OK;
159 lock_protocol::status lock_client_cache_rsm::release(lock_protocol::lockid_t lid) {
160 lock_state &st = get_lock_state(lid);
162 auto self = std::this_thread::get_id();
163 VERIFY(st.state == lock_state::locked && st.held_by == self);
164 st.state = lock_state::free;
165 LOG("Lock " << lid << ": free");
166 if (st.wanted_by.size()) {
167 auto front = st.wanted_by.front();
168 if (front == releaser_thread.get_id()) {
169 st.state = lock_state::locked;
170 st.held_by = releaser_thread.get_id();
171 st.wanted_by.pop_front();
172 LOG("Queuing " << lid << " for release");
173 release_fifo.enq(lid);
177 LOG("Finished signaling.");
178 return lock_protocol::OK;
181 rlock_protocol::status lock_client_cache_rsm::revoke_handler(lock_protocol::lockid_t lid, lock_protocol::xid_t xid, int &) {
182 LOG("Revoke handler " << lid << " " << xid);
183 lock_state &st = get_lock_state(lid);
186 if (st.state == lock_state::releasing || st.state == lock_state::none)
187 return rlock_protocol::OK;
189 if (st.state == lock_state::free &&
190 (st.wanted_by.size() == 0 || st.wanted_by.front() == releaser_thread.get_id())) {
192 st.state = lock_state::locked;
193 st.held_by = releaser_thread.get_id();
194 if (st.wanted_by.size())
195 st.wanted_by.pop_front();
196 release_fifo.enq(lid);
199 st.wanted_by.push_back(releaser_thread.get_id());
201 return rlock_protocol::OK;
204 rlock_protocol::status lock_client_cache_rsm::retry_handler(lock_protocol::lockid_t lid, lock_protocol::xid_t xid, int &) {
205 lock_state &st = get_lock_state(lid);
207 VERIFY(st.state == lock_state::acquiring);
208 st.state = lock_state::retrying;
209 LOG("Lock " << lid << ": none");
210 st.signal(); // only one thread needs to wake up
211 return rlock_protocol::OK;