-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_client_cache.cc
166 lines (129 loc) · 4.38 KB
/
lock_client_cache.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// RPC stubs for clients to talk to lock_server, and cache the locks
// see lock_client.cache.h for protocol details.
#include "lock_client_cache.h"
#include "rpc.h"
#include <sstream>
#include <iostream>
#include <stdio.h>
static void *
releasethread(void *x)
{
lock_client_cache *cc = (lock_client_cache *) x;
cc->releaser();
return 0;
}
int lock_client_cache::last_port = 0;
lock_client_cache::lock_client_cache(std::string xdst,
class lock_release_user *_lu)
: lu(_lu)
{
srand(time(NULL)^last_port);
rlock_port = ((rand()%32000) | (0x1 << 10));
const char *hname;
// assert(gethostname(hname, 100) == 0);
hname = "127.0.0.1";
std::ostringstream host;
host << hname << ":" << rlock_port;
id = host.str();
last_port = rlock_port;
rpcs *rlsrpc = new rpcs(rlock_port);
rsmc = new rsm_client(xdst);
/* register RPC handlers with rlsrpc */
rlsrpc->reg(rlock_protocol::revoke, this, &lock_client_cache::revoke_handler);
rlsrpc->reg(rlock_protocol::retry, this, &lock_client_cache::retry_handler);
// setup connection to server
// int _r; assert(cl->call(lock_protocol::subscribe, cl->id(), id, _r) == lock_protocol::OK);
pthread_t th;
int r = pthread_create(&th, NULL, &releasethread, (void *) this);
assert (r == 0);
}
void
lock_client_cache::releaser() {
// This method should be a continuous loop, waiting to be notified of
// freed locks that have been revoked by the server, so that it can
// send a release RPC.
while (true) {
auto req = release_queue.consume();
pthread_mutex_lock(&cache_mutex);
Lock &lock = cache[req.lid];
lock.status = Lock::RELEASING;
pthread_mutex_unlock(&cache_mutex);
// call dorelease from lock_release_user
lu->dorelease(req.lid);
int r; assert(rsmc->call(lock_protocol::release, id, req.lid, req.seq, r) == lock_protocol::OK);
pthread_mutex_lock(&cache_mutex);
lock.status = Lock::NONE;
pthread_mutex_unlock(&cache_mutex);
pthread_cond_broadcast(&acquire_signal);
}
}
lock_protocol::status
lock_client_cache::acquire(lock_protocol::lockid_t lid) {
ScopedLock guard(&cache_mutex);
Lock& lock = cache[lid];
while (true) {
if (lock.status == Lock::LOCKED || lock.status == Lock::ACQUIRING || lock.status == Lock::RELEASING) {
pthread_cond_wait(&acquire_signal, &cache_mutex);
continue;
}
if (lock.status == Lock::FREE) {
lock.status = Lock::LOCKED;
return lock_protocol::OK;
}
lock.status = Lock::ACQUIRING;
auto seq = ++lock.seqnum;
while (true) {
pthread_mutex_unlock(&cache_mutex);
int r; auto ret = rsmc->call(lock_protocol::acquire, id, lid, seq, r);
if (ret == lock_protocol::OK) break;
pthread_mutex_lock(&cache_mutex);
// retry activated by outdated messages should be ignored
while (lock.seqnum_at_retry < lock.seqnum) {
pthread_cond_wait(&retry_signal, &cache_mutex);
}
// if the call fails we need to wait for another retry request
// doing this to enter the signal wait again
lock.seqnum_at_retry -= 1;
}
pthread_mutex_lock(&cache_mutex);
lock.status = Lock::LOCKED;
lock.seqnum_at_retry = lock.seqnum;
return lock_protocol::OK;
}
}
lock_protocol::status
lock_client_cache::release(lock_protocol::lockid_t lid) {
ScopedLock guard(&cache_mutex);
Lock& lock = cache[lid];
// release to cache
if (lock.seqnum_at_revoke < lock.seqnum) {
lock.status = Lock::FREE;
pthread_cond_broadcast(&acquire_signal);
return lock_protocol::OK;
}
// release to server
lock.status = Lock::RELEASING;
release_queue.add(release_req(lid, lock.seqnum));
return lock_protocol::OK;
}
rlock_protocol::status
lock_client_cache::revoke_handler(lock_protocol::lockid_t lid, unsigned int seq, int& r) {
ScopedLock guard(&cache_mutex);
Lock& lock = cache[lid];
lock.seqnum_at_revoke = seq;
if (lock.status != Lock::FREE) {
// it will be released after the thread releases the lock - release method
return rlock_protocol::OK;
}
lock.status = Lock::RELEASING;
release_queue.add(release_req(lid, seq));
return rlock_protocol::OK;
}
rlock_protocol::status
lock_client_cache::retry_handler(lock_protocol::lockid_t lid, unsigned int seq, int& r) {
pthread_mutex_lock(&cache_mutex);
cache[lid].seqnum_at_retry = seq;
pthread_mutex_unlock(&cache_mutex);
pthread_cond_broadcast(&retry_signal);
return rlock_protocol::OK;
}