1 // RPC stubs for clients to talk to lock_server, and cache the locks.
3 #include "lock_client.h"
6 void lock_state::wait(lock & mutex_lock) {
7 auto self = this_thread::get_id();
8 c[self].wait(mutex_lock);
12 void lock_state::signal() {
14 if (c.begin() != c.end())
15 c.begin()->second.notify_one();
18 void lock_state::signal(thread::id who) {
23 typedef map<lock_protocol::lockid_t, lock_state> lock_map;
25 in_port_t lock_client::last_port = 0;
27 lock_state & lock_client::get_lock_state(lock_protocol::lockid_t lid) {
28 lock sl(lock_table_lock);
29 return lock_table[lid]; // creates the lock if it doesn't already exist
32 lock_client::lock_client(string xdst, lock_release_user *_lu) : lu(_lu), next_xid(0) {
33 cl = unique_ptr<rpcc>(new rpcc(xdst));
35 LOG("lock_client: call bind");
37 srandom((uint32_t)time(NULL)^last_port);
38 rlock_port = ((random()%32000) | (0x1 << 10));
39 id = "127.0.0.1:" + to_string(rlock_port);
40 last_port = rlock_port;
41 rlsrpc = unique_ptr<rpcs>(new rpcs(rlock_port));
42 rlsrpc->reg(rlock_protocol::revoke, &lock_client::revoke_handler, this);
43 rlsrpc->reg(rlock_protocol::retry, &lock_client::retry_handler, this);
44 rsmc = unique_ptr<rsm_client>(new rsm_client(xdst));
45 releaser_thread = thread(&lock_client::releaser, this);
49 void lock_client::releaser() {
51 lock_protocol::lockid_t lid;
52 release_fifo.deq(&lid);
53 LOG("Releaser: " << lid);
55 lock_state & st = get_lock_state(lid);
57 VERIFY(st.state == lock_state::locked && st.held_by == releaser_thread.get_id());
58 st.state = lock_state::releasing;
62 rsmc->call(lock_protocol::release, r, lid, id, st.xid);
67 st.state = lock_state::none;
68 LOG("Lock " << lid << ": none");
73 int lock_client::stat(lock_protocol::lockid_t lid) {
76 auto ret = (lock_protocol::status)cl->call(lock_protocol::stat, r, lid, id);
77 VERIFY (ret == lock_protocol::OK);
81 lock_protocol::status lock_client::acquire(lock_protocol::lockid_t lid) {
82 lock_state & st = get_lock_state(lid);
84 auto self = this_thread::get_id();
86 // check for reentrancy
87 VERIFY(st.state != lock_state::locked || st.held_by != self);
88 VERIFY(find(st.wanted_by.begin(), st.wanted_by.end(), self) == st.wanted_by.end());
90 st.wanted_by.push_back(self);
93 if (st.state != lock_state::free)
94 LOG("Lock " << lid << ": not free");
96 if (st.state == lock_state::none || st.state == lock_state::retrying) {
97 if (st.state == lock_state::none) {
101 st.state = lock_state::acquiring;
102 LOG("Lock " << lid << ": acquiring");
103 lock_protocol::status result;
107 result = (lock_protocol::status)rsmc->call(lock_protocol::acquire, r, lid, id, st.xid);
110 LOG("acquire returned " << result);
111 if (result == lock_protocol::OK) {
112 st.state = lock_state::free;
113 LOG("Lock " << lid << ": free");
117 VERIFY(st.wanted_by.size() != 0);
118 if (st.state == lock_state::free) {
120 auto front = st.wanted_by.front();
121 if (front == releaser_thread.get_id()) {
122 st.wanted_by.pop_front();
123 st.state = lock_state::locked;
124 st.held_by = releaser_thread.get_id();
125 LOG("Queuing " << lid << " for release");
126 release_fifo.enq(lid);
127 } else if (front == self) {
128 st.wanted_by.pop_front();
129 st.state = lock_state::locked;
142 LOG("Lock " << lid << ": locked");
143 return lock_protocol::OK;
146 lock_protocol::status lock_client::release(lock_protocol::lockid_t lid) {
147 lock_state & st = get_lock_state(lid);
149 auto self = this_thread::get_id();
150 VERIFY(st.state == lock_state::locked && st.held_by == self);
151 st.state = lock_state::free;
152 LOG("Lock " << lid << ": free");
153 if (st.wanted_by.size()) {
154 auto front = st.wanted_by.front();
155 if (front == releaser_thread.get_id()) {
156 st.state = lock_state::locked;
157 st.held_by = releaser_thread.get_id();
158 st.wanted_by.pop_front();
159 LOG("Queuing " << lid << " for release");
160 release_fifo.enq(lid);
164 LOG("Finished signaling.");
165 return lock_protocol::OK;
168 rlock_protocol::status lock_client::revoke_handler(int &, lock_protocol::lockid_t lid, lock_protocol::xid_t xid) {
169 LOG("Revoke handler " << lid << " " << xid);
170 lock_state & st = get_lock_state(lid);
173 if (st.state == lock_state::releasing || st.state == lock_state::none)
174 return rlock_protocol::OK;
176 if (st.state == lock_state::free &&
177 (st.wanted_by.size() == 0 || st.wanted_by.front() == releaser_thread.get_id())) {
179 st.state = lock_state::locked;
180 st.held_by = releaser_thread.get_id();
181 if (st.wanted_by.size())
182 st.wanted_by.pop_front();
183 release_fifo.enq(lid);
186 st.wanted_by.push_back(releaser_thread.get_id());
188 return rlock_protocol::OK;
191 rlock_protocol::status lock_client::retry_handler(int &, lock_protocol::lockid_t lid, lock_protocol::xid_t) {
192 lock_state & st = get_lock_state(lid);
194 VERIFY(st.state == lock_state::acquiring);
195 st.state = lock_state::retrying;
196 LOG("Lock " << lid << ": none");
197 st.signal(); // only one thread needs to wake up
198 return rlock_protocol::OK;
201 t4_lock_client *t4_lock_client_new(const char *dst) {
202 return (t4_lock_client *)new lock_client(dst);
205 void t4_lock_client_delete(t4_lock_client *client) {
206 delete (lock_client *)client;
209 t4_status t4_lock_client_acquire(t4_lock_client *client, t4_lockid_t lid) {
210 return ((lock_client *)client)->acquire(lid);
213 t4_status t4_lock_client_release(t4_lock_client *client, t4_lockid_t lid) {
214 return ((lock_client *)client)->release(lid);
217 t4_status t4_lock_client_stat(t4_lock_client *client, t4_lockid_t lid) {
218 return ((lock_client *)client)->stat(lid);