forked from bohaoist/yfs2012
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock_client_cache.cc
More file actions
186 lines (171 loc) · 5.4 KB
/
lock_client_cache.cc
File metadata and controls
186 lines (171 loc) · 5.4 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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>
#include <utility>
#include "tprintf.h"
int lock_client_cache::last_port = 0;
lock_client_cache::lock_client_cache(std::string xdst,
class lock_release_user *_lu)
: lock_client(xdst), lu(_lu)
{
srand(time(NULL)^last_port);
rlock_port = ((rand()%32000) | (0x1 << 10));
const char *hname;
// VERIFY(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);
rlsrpc->reg(rlock_protocol::revoke, this, &lock_client_cache::revoke_handler);
rlsrpc->reg(rlock_protocol::retry, this, &lock_client_cache::retry_handler);
}
lock_protocol::status
lock_client_cache::acquire(lock_protocol::lockid_t lid)
{
int ret = lock_protocol::OK;
pthread_t threadId = pthread_self();
std::unique_lock<std::mutex> lk(mutex_);
int r = 0;
auto it = lockInfos_.find(lid);
if(it == lockInfos_.end() || it->second.cachestate_ == CacheState::NONE)
{
//insert
lockInfos_[lid].cachestate_ = CacheState::ACQUIRING;
lk.unlock();
tprintf("client %s acquiring...\n",id.c_str());
ret = cl->call(lock_protocol::acquire,lid,id,r);
if(ret == lock_protocol::OK)
{
lk.lock();
auto it = lockInfos_.find(lid);
if(r)
it->second.retryR_ = 1;
goto success;
}
retry: //receive RETRY
assert(ret == lock_protocol::RETRY);
lk.lock();
auto it = lockInfos_.find(lid);
lockInfos_[lid].threadWaiterNum_++;
tprintf("client %s RETRY wait \n",id.c_str());
if(it->second.cachestate_ != CacheState::FREE)
it->second.cond_.wait(lk,[&](){ return lockInfos_[lid].cachestate_ == CacheState::FREE; });
lockInfos_[lid].threadWaiterNum_--;
goto success;
}
else
{
if(it->second.cachestate_ == CacheState::LOCKED || it->second.cachestate_ == CacheState::ACQUIRING)
{
it->second.threadWaiterNum_++;
tprintf("client %s acquiring/locked wait \n",id.c_str());
it->second.cond_.wait(lk,[&](){ return lockInfos_[lid].cachestate_ == CacheState::FREE; });
lockInfos_[lid].threadWaiterNum_--;
goto success;
}
else if (it->second.cachestate_ == CacheState::FREE)
{
goto success;
}
else
{
assert(it->second.cachestate_ == CacheState::RELEASING);
tprintf("client %s releasing wait \n",id.c_str());
it->second.threadWaiterNum_++;
it->second.cond_.wait(lk,[&](){ return lockInfos_[lid].cachestate_ != CacheState::RELEASING; });
it->second.threadWaiterNum_--;
lk.unlock();
return acquire(lid);
}
}
success:
tprintf("client %s got in %ld \n",id.c_str(),threadId);
it = lockInfos_.find(lid);
it->second.cachestate_ = CacheState::LOCKED;
it->second.threadOwner_ = threadId;
lk.unlock();
return lock_protocol::OK;
}
lock_protocol::status
lock_client_cache::release(lock_protocol::lockid_t lid)
{
int ret = lock_protocol::OK;
int r;
std::unique_lock<std::mutex> lk(mutex_);
auto it = lockInfos_.find(lid);
assert(it != lockInfos_.end());
it->second.cachestate_ = CacheState::FREE;
if(it->second.threadWaiterNum_ > 0 || it->second.isRevoked)
{
it->second.cond_.notify_one();
return ret;
}
if(it->second.retryR_)
{
it->second.cachestate_ = CacheState::RELEASING;
// lk.unlock();
tprintf("client %s releasing... %lld \n",id.c_str(),lid);
lu->dorelease(lid);
ret = cl->call(lock_protocol::release,lid,id,r);
assert(ret == lock_protocol::OK);
{
// lk.lock();
it = lockInfos_.find(lid);
it->second.cachestate_ = CacheState::NONE;
it->second.retryR_ = 0;
it->second.cond_.notify_one();
lk.unlock();
}
return lock_protocol::OK;
}
return ret;
}
rlock_protocol::status
lock_client_cache::revoke_handler(lock_protocol::lockid_t lid,
int &)
{
int ret = lock_protocol::OK;
int r;
std::unique_lock<std::mutex> lk(mutex_);
auto it = lockInfos_.find(lid);
assert(it != lockInfos_.end());
it->second.isRevoked = true;
tprintf("client %s revoke wait \n",id.c_str());
it->second.cond_.wait(lk,[&](){ return lockInfos_[lid].threadWaiterNum_ == 0 &&
lockInfos_[lid].cachestate_ == CacheState::FREE
&& lockInfos_[lid].threadOwner_ != 0; });
tprintf("client %s revoke finish %lld \n",id.c_str(),lid);
it->second.cachestate_ = CacheState::RELEASING;
it->second.isRevoked = false;
it->second.retryR_ = 0;
// lk.unlock();
lu->dorelease(lid);
// lk.lock();
lockInfos_[lid].cachestate_ = CacheState::NONE;
lockInfos_[lid].cond_.notify_one();
return lock_protocol::OK;
}
rlock_protocol::status
lock_client_cache::retry_handler(lock_protocol::lockid_t lid,int retryR,
int & r)
{
int ret = rlock_protocol::OK;
tprintf("client %s retry receive %lld \n",id.c_str(),lid);
std::unique_lock<std::mutex> lk(mutex_);
auto it = lockInfos_.find(lid);
assert(it != lockInfos_.end());
it->second.cachestate_ = CacheState::FREE;
if(retryR)
{
it->second.retryR_ = 1;
// tprintf("client %s set retryR %lld \n",id.c_str(),lid);
}
it->second.cond_.notify_one();
return ret;
}