1 // RPC stubs for clients to talk to lock_server, and cache the locks.
3 #include "lock_client.h"
10 #include <arpa/inet.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::last_port = 0;
44 lock_state & lock_client::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::lock_client(string xdst, class lock_release_user *_lu) : lu(_lu) {
53 make_sockaddr(xdst.c_str(), &dstsock);
54 cl = new rpcc(dstsock);
56 printf("lock_client: call bind\n");
59 srand(time(NULL)^last_port);
60 rlock_port = ((rand()%32000) | (0x1 << 10));
62 // VERIFY(gethostname(hname, 100) == 0);
65 host << hname << ":" << rlock_port;
67 last_port = rlock_port;
68 rpcs *rlsrpc = new rpcs(rlock_port);
69 rlsrpc->reg(rlock_protocol::revoke, &lock_client::revoke_handler, this);
70 rlsrpc->reg(rlock_protocol::retry, &lock_client::retry_handler, this);
75 rsmc = new rsm_client(xdst);
76 releaser_thread = std::thread(&lock_client::releaser, this);
79 void lock_client::releaser() {
81 lock_protocol::lockid_t lid;
82 release_fifo.deq(&lid);
83 LOG("Releaser: " << lid);
85 lock_state &st = get_lock_state(lid);
87 VERIFY(st.state == lock_state::locked && st.held_by == releaser_thread.get_id());
88 st.state = lock_state::releasing;
92 rsmc->call(lock_protocol::release, r, lid, id, st.xid);
95 st.state = lock_state::none;
96 LOG("Lock " << lid << ": none");
101 int lock_client::stat(lock_protocol::lockid_t lid) {
104 lock_protocol::status ret = cl->call(lock_protocol::stat, r, cl->id(), lid);
105 VERIFY (ret == lock_protocol::OK);
109 lock_protocol::status lock_client::acquire(lock_protocol::lockid_t lid) {
110 lock_state &st = get_lock_state(lid);
112 auto self = std::this_thread::get_id();
114 // check for reentrancy
115 VERIFY(st.state != lock_state::locked || st.held_by != self);
116 VERIFY(find(st.wanted_by.begin(), st.wanted_by.end(), self) == st.wanted_by.end());
118 st.wanted_by.push_back(self);
121 if (st.state != lock_state::free)
122 LOG("Lock " << lid << ": not free");
124 if (st.state == lock_state::none || st.state == lock_state::retrying) {
125 if (st.state == lock_state::none) {
129 st.state = lock_state::acquiring;
130 LOG("Lock " << lid << ": acquiring");
131 lock_protocol::status result;
135 result = rsmc->call(lock_protocol::acquire, r, lid, id, st.xid);
138 LOG("acquire returned " << result);
139 if (result == lock_protocol::OK) {
140 st.state = lock_state::free;
141 LOG("Lock " << lid << ": free");
145 VERIFY(st.wanted_by.size() != 0);
146 if (st.state == lock_state::free) {
148 auto front = st.wanted_by.front();
149 if (front == releaser_thread.get_id()) {
150 st.wanted_by.pop_front();
151 st.state = lock_state::locked;
152 st.held_by = releaser_thread.get_id();
153 LOG("Queuing " << lid << " for release");
154 release_fifo.enq(lid);
155 } else if (front == self) {
156 st.wanted_by.pop_front();
157 st.state = lock_state::locked;
170 LOG("Lock " << lid << ": locked");
171 return lock_protocol::OK;
174 lock_protocol::status lock_client::release(lock_protocol::lockid_t lid) {
175 lock_state &st = get_lock_state(lid);
177 auto self = std::this_thread::get_id();
178 VERIFY(st.state == lock_state::locked && st.held_by == self);
179 st.state = lock_state::free;
180 LOG("Lock " << lid << ": free");
181 if (st.wanted_by.size()) {
182 auto front = st.wanted_by.front();
183 if (front == releaser_thread.get_id()) {
184 st.state = lock_state::locked;
185 st.held_by = releaser_thread.get_id();
186 st.wanted_by.pop_front();
187 LOG("Queuing " << lid << " for release");
188 release_fifo.enq(lid);
192 LOG("Finished signaling.");
193 return lock_protocol::OK;
196 rlock_protocol::status lock_client::revoke_handler(int &, lock_protocol::lockid_t lid, lock_protocol::xid_t xid) {
197 LOG("Revoke handler " << lid << " " << xid);
198 lock_state &st = get_lock_state(lid);
201 if (st.state == lock_state::releasing || st.state == lock_state::none)
202 return rlock_protocol::OK;
204 if (st.state == lock_state::free &&
205 (st.wanted_by.size() == 0 || st.wanted_by.front() == releaser_thread.get_id())) {
207 st.state = lock_state::locked;
208 st.held_by = releaser_thread.get_id();
209 if (st.wanted_by.size())
210 st.wanted_by.pop_front();
211 release_fifo.enq(lid);
214 st.wanted_by.push_back(releaser_thread.get_id());
216 return rlock_protocol::OK;
219 rlock_protocol::status lock_client::retry_handler(int &, lock_protocol::lockid_t lid, lock_protocol::xid_t xid) {
220 lock_state &st = get_lock_state(lid);
222 VERIFY(st.state == lock_state::acquiring);
223 st.state = lock_state::retrying;
224 LOG("Lock " << lid << ": none");
225 st.signal(); // only one thread needs to wake up
226 return rlock_protocol::OK;
229 t4_lock_client *t4_lock_client_new(const char *dst) {
230 return (t4_lock_client *)new lock_client(dst);
233 void t4_lock_client_delete(t4_lock_client *client) {
234 delete (lock_client *)client;
237 t4_status t4_lock_client_acquire(t4_lock_client *client, t4_lockid_t lid) {
238 return ((lock_client *)client)->acquire(lid);
241 t4_status t4_lock_client_release(t4_lock_client *client, t4_lockid_t lid) {
242 return ((lock_client *)client)->acquire(lid);
245 t4_status t4_lock_client_stat(t4_lock_client *client, t4_lockid_t lid) {
246 return ((lock_client *)client)->stat(lid);