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"
14 lock_state::lock_state():
19 void lock_state::wait() {
20 pthread_t self = pthread_self();
25 void lock_state::signal() {
27 if (c.begin() != c.end())
28 c.begin()->second.signal();
31 void lock_state::signal(pthread_t who) {
36 static void * releasethread(void *x) {
37 lock_client_cache_rsm *cc = (lock_client_cache_rsm *) x;
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 ScopedLock 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);
65 ScopedLock sl(xid_mutex);
68 rsmc = new rsm_client(xdst);
69 int r = pthread_create(&releaser_thread, NULL, &releasethread, (void *) this);
73 void lock_client_cache_rsm::releaser() {
75 lock_protocol::lockid_t lid;
76 release_fifo.deq(&lid);
77 LOG("Releaser: " << lid);
79 lock_state &st = get_lock_state(lid);
81 VERIFY(st.state == lock_state::locked && st.held_by == releaser_thread);
82 st.state = lock_state::releasing;
84 ScopedUnlock su(st.m);
86 rsmc->call(lock_protocol::release, lid, id, st.xid, r);
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 pthread_t self = pthread_self();
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) {
111 ScopedLock sl(xid_mutex);
114 st.state = lock_state::acquiring;
115 LOG("Lock " << lid << ": acquiring");
116 lock_protocol::status result;
118 ScopedUnlock su(st.m);
120 result = rsmc->call(lock_protocol::acquire, lid, id, st.xid, r);
122 LOG("acquire returned " << result);
123 if (result == lock_protocol::OK) {
124 st.state = lock_state::free;
125 LOG("Lock " << lid << ": free");
129 VERIFY(st.wanted_by.size() != 0);
130 if (st.state == lock_state::free) {
132 pthread_t front = st.wanted_by.front();
133 if (front == releaser_thread) {
134 st.wanted_by.pop_front();
135 st.state = lock_state::locked;
136 st.held_by = releaser_thread;
137 LOG("Queuing " << lid << " for release");
138 release_fifo.enq(lid);
139 } else if (front == self) {
140 st.wanted_by.pop_front();
141 st.state = lock_state::locked;
154 LOG("Lock " << lid << ": locked");
155 return lock_protocol::OK;
158 lock_protocol::status lock_client_cache_rsm::release(lock_protocol::lockid_t lid) {
159 lock_state &st = get_lock_state(lid);
161 pthread_t self = pthread_self();
162 VERIFY(st.state == lock_state::locked && st.held_by == self);
163 st.state = lock_state::free;
164 LOG("Lock " << lid << ": free");
165 if (st.wanted_by.size()) {
166 pthread_t front = st.wanted_by.front();
167 if (front == releaser_thread) {
168 st.state = lock_state::locked;
169 st.held_by = releaser_thread;
170 st.wanted_by.pop_front();
171 LOG("Queuing " << lid << " for release");
172 release_fifo.enq(lid);
176 LOG("Finished signaling.");
177 return lock_protocol::OK;
180 rlock_protocol::status lock_client_cache_rsm::revoke_handler(lock_protocol::lockid_t lid, lock_protocol::xid_t xid, int &) {
181 LOG("Revoke handler " << lid << " " << xid);
182 lock_state &st = get_lock_state(lid);
185 if (st.state == lock_state::releasing || st.state == lock_state::none)
186 return rlock_protocol::OK;
188 if (st.state == lock_state::free &&
189 (st.wanted_by.size() == 0 || st.wanted_by.front() == releaser_thread)) {
191 st.state = lock_state::locked;
192 st.held_by = releaser_thread;
193 if (st.wanted_by.size())
194 st.wanted_by.pop_front();
195 release_fifo.enq(lid);
198 st.wanted_by.push_back(releaser_thread);
200 return rlock_protocol::OK;
203 rlock_protocol::status lock_client_cache_rsm::retry_handler(lock_protocol::lockid_t lid, lock_protocol::xid_t xid, int &) {
204 lock_state &st = get_lock_state(lid);
206 VERIFY(st.state == lock_state::acquiring);
207 st.state = lock_state::retrying;
208 LOG("Lock " << lid << ": none");
209 st.signal(); // only one thread needs to wake up
210 return rlock_protocol::OK;